OMG! Memory operations are dangerous

Source: Internet
Author: User

Copyright statement: You can reprint it at will.Hyperlink formIndicate the source and author information of the following articles andThis statement

Author: Xixi

Source: http://blog.csdn.net/slowgrace/archive/2009/04/24/4105426.aspx

The difference between declaring an API parameter as any and declaring it as long is mentioned in this article. I tried it in this post yesterday. ThanksMyjianPatiently explain.

There is an API function called rtlmovememory. We can have the following two declarations:

Private declare sub copymemory lib "Kernel32" alias "rtlmovememory" (byval destination as long, byval source as long, byval length as long)
Private declare sub copymemory lib "Kernel32" alias "rtlmovememory" (destination as any, source as any, byval length as long)

 

Then we use the following test code:

Option explicit

Private type mystruct
X as byte
Y as byte
Z as long
End type

Private sub form_load ()
Dim t as mystruct
With T
. X = 12
. Y = 34
. Z = & h5678 'stored in memory as & h78 & h56 & H00 & H00
End

Dim P as long
Dim B as byte
P = varptr (t)

Call copymemory (B, byval P + 2, 1)
Debug. Print hex (B)

Call copymemory (B, byval P + 4, 1)
Debug. Print hex (B)
End sub

This code roughly means to verify the High-byte alignment storage mode of the structure in VB6.

If the first statement is used, it will cause memory overflow (the consequence of my attempt yesterday is that access is stopped and restarted. The following statement is used:

Call copymemory (B, byval P + 2, 1)

The second parameter of this statement isValues in the P variable(That is, the memory address of the variable t) + the "value" after 2 is passed in, instead ofVariableSo add byval, because byref is used by default in the Declaration. When the Declaration is not declared as byref as any but byval as long, this call can be P + 2, which saves the byval description.

The first parameter of this statement is to pass the address of the variable B over. If the byval as long statement is used, the value of B is passed in (0 at the time ), if you copy data to the address 0, an error will occur. In fact, if we use the byval as long Declaration, we should pass vatptr (B.

 

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.