How to get the address of the variable in memory in the Visual Basic (Address of variables)

zhaozj2021-02-08  224

How to get the memory address of the variable in Visual Basic (Address of Variables)

This article is suitable for

Microsoft Visual Basic Learning Edition for Windows, Versions 5.0, 6.0

Microsoft Visual Basic Professional Edition for Windows, Versions 5.0, 6.0

Microsoft Visual Basic Enterprise Edition for Windows, Versions 5.0, 6.0

Summary

Little VB programmers need to get low-level information of a variable, such as memory addresses. However, some API functions require this

Information, the methods described herein can help VB programmers get this information

VarPtr - Returns the address of a variable return address of a variable VarPtrArray -.. Returns the address of an array return array address StrPtr -. Returns the address of the UNICODE string buffer returns UNICODE string buffer address VarPtrStringArray - Returns the address of an Array of strings. Returns a string array address Objptr - returns the pointer to the interface referened by an Object variable. Returns an object's interface reference pointer

text

Varptr

This function is used to get a variable or an array of addresses, pass a variable name or array and return the address. However, you have to understand that the dynamic array may be reassigned by VB, so you must be careful when you use Varptr to get an array of addresses.

Dim lngvariableaddress as long

DIM DBLMYVARIABLE AS DOUBLE

LNGVARIABLEADDRESS = VARPTR (dblmyvariable)

Dim LngelementAddress as long

Dim LNGARRAYOFLONGS (9) As long

'Following Will Get Address of 4th Element

LNGELEMENTADDRESS = Varptr (LNGARRAYOFLONGS (3))

Limit: This function cannot be used to obtain an address of the array

Varptrarray

When the array is in the Visual Basic to save the SafeArRays structure, you have to get the address of the SafearRays structure, you want to use the Varptrarray function, below the definition of VB5, VB6, respectively:

DECLARE FUNCTION VARPTRARRAY LIB "msvbvm50.dll" Alias ​​"Varptr" _

(Var () as any) as long

DECLARE FUNCTION VARPTRARRAY LIB "msvbvm60.dll" Alias ​​"Varptr" _

(Var () as any) as long

Dim LngsafeArrayaddress as long

Dim LNGARRAYOFLONGS (9) As long

LNGSAFEARRAYADDRESS = VARPTRARRAY (LNGARRAYOFLONGS ())

Limitation: The Varptrarray function cannot be used to obtain an address of a string array because VB will make Unicode / ANSI conversion for the string. If you use Varptrarray for the string array, you will get an address of a temporary ANSI array copy. Strptr

String first character of the first character DIM LNGCHARADDRESS AS Long

DIM STRMYVARIABLE AS STRING

Strmyvariable = "some string"

LNGCharaddress = strptr (strmyvariable)

This function can be used when you need to pass a Unicode string pointer in the API call

VarptrstringArray

VarptrstringArray is used to obtain the address of the string array. In order to avoid VB for Unicode / ANSI conversion,

Its statement must be defined in the type library

You can also use the MIDL compiler to compile the following .odl files into your own type library :) If it is Visual Basic 5.0, create a text file name to vb6ptrlib.odl, the content is as follows:

#define rtcall_stdcall

[

UUID (C6799410-4431-11D2-A7F1-00A0C91110C3),

LCID (0), Version (6.0), Helpstring ("VarptrstringArray Support for VB6)

]

Library Ptrlib

{

Importlib ("stdole2.tlb");

[Dllname ("MSVBVM60.DLL")]]]]

Module ArrayPtr

{

[Entry ("Varptr")]]]]

Long RTCall VarptrstringArray ([in] SafeArray (BSTR) * PTR);

}

}

If it is Visual Basic 5.0, create a text file name is VB5Ptrlib.odl, the content is as follows:

#define rtcall_stdcall

[

UUID (6E814F00-7439-11D2-98D2-00C04FAD90E7),

LCID (0), Version (5.0), Helpstring ("VarptrstringArray Support for VB5)

]

Library Ptrlib

{

Importlib ("stdole2.tlb");

[Dllname ("msvbvm50.dll")]]]]

Module ArrayPtr

{

[Entry ("Varptr")]]]]

Long RTCall VarptrstringArray ([in] SafeArray (BSTR) * PTR);

}

}

Compile the .odl file to build a VB5 or VB6 type library (.tlb) in the command line.

MIDL / T VB6PTRLIB.ODL

MIDL / T VB5PTRLIB.ODL

In order to use VarptrstringArray, you need to reference this type library.

example:

Dim MyArrayofstrings (2) AS STRING

Dim AddressofArray As Long

MyArrayofstrings (0) = "AAA"

MyArrayofstrings (1) = "BBB"

AddressofArray = VarptrstringArray (MyArrayofstrings ())

Objptr

Objptr returns an address of an Object variable

Objcollection.add myobj1, cstr (Objptr (myobj1)) ...

Objcollection.remove CSTR (Objptr (myobj1))

转载请注明原文地址:https://www.9cbs.com/read-3388.html

New Post(0)