Article ID: 199824 - View products that this article applies to.
This article was previously published under Q199824
It is uncommon for a Visual Basic programmer to need to obtain low level information on a variable, such as its memory address. However, there are some API functions that require such information. This article describes the following Visual Basic functions that may help a Visual Basic programmer obtain this information:
VarPtr - Returns the address of a variable.
VarPtrArray - Returns the address of an array.
StrPtr - Returns the address of the UNICODE string buffer.
VarPtrStringArray - Returns the address of an array of strings.
ObjPtr - Returns the pointer to the interface referenced by an object variable.
WARNING: One or more of the following functions are discussed in this article; VarPtr, VarPtrArray, VarPtrStringArray, StrPtr, ObjPtr. These functions are not supported by Microsoft Technical Support. They are not documented in the Visual Basic documentation and are provided in this Knowledge Base article "as is." Microsoft does not guarantee that they will be available in future releases of Visual Basic.
VarPtrThis function can be used to get the address of a variable or an array element. It takes the variable name or the array element as the parameter and returns the address. However, you should be aware that unlocked Dynamic Arrays may be reallocated by Visual Basic, so you must be very careful when you use VarPtr to get the address of an array element.
The following example gets the address of a variable:
This example gets the address of the fourth element of an array:
Limitations: The VarPtr function cannot be used to get the address of an array. For more information, see the VarPtrArray function.
VarPtrArrayArrays in Visual Basic are stored as SAFEARRAYs. To get the address of the SAFEARRAY structure, you need to use the VarPtrArray function. The following are the Visual Basic 5.0 and Visual Basic 6.0 declarations respectively:
To get the address of a SAFEARRAY, pass the SAFEARRAY name (including the parenthesis) to the VarPtrArray function:
Limitations: The VarPtrArray function cannot be used to get the address of an array of Strings, because Visual Basic does UNICODE/ANSI conversion for Strings. If you use VarPtrArray on an array of Strings, you will get the address of a temporary ANSI copy of the array. For more information, see the VarPtrStringArray function.
StrPtrStrings in Visual Basic are stored as BSTR's. If you use the VarPtr on a variable of type String, you will get the address of the BSTR, which is a pointer to a pointer of the string. To get the address of the string buffer itself, you need to use the StrPtr function. This function returns the address of the first character of the string. Take into account that Strings are stored as UNICODE in Visual Basic.
To get the address of the first character of a String, pass the String variable to the StrPtr function.
You can use this function when you need to pass a pointer to a UNIOCODE string to an API call.
VarPtrStringArrayVarPtrStringArray gets the address of an array of Strings. To avoid the intrinsic UNICODE/ANSI conversion performed by Visual Basic, the declaration has to be defined in a type library.
Alternatively, you could use the MIDL compiler to compile your own type library from the following .odl files.
For Visual Basic 6.0, create a text file named VB6ptrlib.odl with the content below:
For Visual Basic 5.0, create a text file named VB5ptrlib.odl with the content below:
Use the following command lines to compile the preceding .odl files with the MIDL compiler to create a Visual Basic 6.0 or Visual Basic 5.0 type library (.tlb) file respectively:
MIDL /t VB6ptrlib.odl MIDL /t VB5ptrlib.odl
To use the VarPtrStringArray function in your project, you need to create a reference to the type library you just created.
ObjPtrObjPtr takes an object variable name as a parameter and obtains the address of the interface referenced by this object variable.
One scenario of using this function is when you need to do a collection of objects. By indexing the object using its address as the key, you can get faster access to the object than walking the collection and using the Is operator. In many cases, the address of an object is the only reliable thing to use as a key.
Article ID: 199824 - Last Review: July 1, 2004 - Revision: 2.1
Contact us for more help
Connect with Answer Desk for expert help.