The related problems of copymemory in VB

Source: Internet
Author: User

Dim A () as Long
Dim B () As Byte
CopyMemory B (0), ByVal "1234", 4
CopyMemory ByVal VarPtr (A (0)), 1234,4


In VB in security considerations, can not directly access memory,but you can use the CopyMemory (rtlmovememory) API to replicate the memory of the specified area,
the specific statements are as follows:
Declare Sub copymemory Lib "kernel32" Alias "RtlMoveMemory" (Destination as any, Source as any, ByVal Length As Long)
among them,
Destination is a pointer to a copy target that can be specified directly as a variable or an array (if the array should use MyArray (0) or MyArray (n) to specify the starting point of the overlay, cannot use MyArray directly), or use the ByVal Keyword plus memory address
Source is a pointer to a copy source, using the same as above
length is the size of the copy, in bytes.

VB 6 does not expose the three functions:varptr,strptr,objptr
They return variables, strings, pointers to objects (that is, memory addresses ), respectively.

Therefore, in the execution:
CopyMemory B (0), ByVal "1234", 4
, VB first delimits the area in memory,
used to hold a string with a value of "1234".
then pass the pointer of this string to CopyMemory,
copymemory Copy the data from the address of the memory where the string data was just saved to array B,
Overwrite from B (0), overwrite 4 bytes

VB 6 When calling the API, the string is typically passed by value (ByVal),
encoded in ANSI at delivery time.
In other words, the above code is equivalent to (I personally recommend this approach for security purposes):
Erase b
B = StrConv ("1234", vbFromUnicode)

and
CopyMemory ByVal VarPtr (A (0)), 1234,4
is actually
CopyMemory A (0), 1234,4
at the time of execution,
VB will first draw 2 bytes in memory (1234 is equivalent to 1234% or CInt (1234))
to hold an Integer with a value of 1234,
then when the copymemory is executed,
Copy the data that was stored in memory just 1234 to array B,
overwrites from a (0), overwriting 4 bytes.
The problem is that
when you give the pointer to someone, only 2 bytes are drawn in the corresponding position .
you want someone to read 4 bytes from your position ...
since the latter two bytes are not assigned,
Therefore, it is possible to return an arbitrary value,
do not rule out the possibility of collapse,
and your program crashes with the IDE with the development environment.
presumably you should have written this (I think ByVal varptr (...) is a liability):
CopyMemory A (0), 1234&,4
In this way, a 4-byte (Long) is drawn.
But if so, you might as well use:
A (0) = 1234
it's convenient and safe ...

Also,
CopyMemory is only responsible for replication and is not responsible for allocating memory .
so you're putting A and b arrays in front of the target of replication coverage,
at a minimum, you need to allocate memory (if you omit it in the question ...). )
For example:
ReDim A (0) ' 4 x 1 = 4 bytes
ReDim B (3) ' 1 x 4 = 4 bytes
But if you're ready to use
B = StrConv ("1234", vbFromUnicode)
such a code,
then B must be empty, if not, you need to use Erase to empty

The related problems of copymemory in VB

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.