Development History
The Varptr function already exists before the Basic language evolved into QBasic, then before Visual Basic. Start, this function exists in the VB Runtime 1.0. This function can be called by a statement:
Declare Function Varptr LiB "VBRUN100.DLL" (VAR AS ANY) AS Long
After years, Vbrun100.dll became MSVBVM50.DLL, but the entry point of the function was still there. In order to obtain the address of the variable, you only have to pass the variable name to the function. E.g:
DIM L As Long
Debug.print Varptr (L)
Similarly, in order to obtain a pointer of the string, not the pointer to the variable of the string, only before the variable is preceded to BYVAL. Such as:
Debug.print Varptr (s), Varptr (Byval S)
Before VB3, this method is used to get a string buffer pointer is very common. But in VB4, I encountered a little trouble.
ANSI / UNIDCODE problem
With the arrival of 32-bit world and VB4, half of us is Unicode, half is ANSI's Windows World. Before this, it was an ANSI one uniform world. In VB, all strings are saved in Unicode, but all API calls still use the ANSI string. This requires that the string is converted from Unicode into ansi before calling the API function, and the returned string is converted into Unicode from the ANSI. Although most of this conversion is transparent to the user, this makes it possible to pass a string type parameter to the DLL from VB in a Unicode mode. Similarly, any structure containing a string must also pass this dual conversion when the API call is executed.
How does this difference affect the Varptr function? When a string is passed to the Varptr function, the address returned after execution is the address of the temporary ANSI string or variable that saves the temporary ANSI string. In other words, this address is not the true address of the variable you declare. Therefore, this function is not used for string variables and structures including strings.
VB5 to solve the problem
In order to be Varptr to re-play, VB5 (and Office 97) has joined three entry points for VBA type libraries. These entry points provide a built-in statement for the Varptr function. The role of these three functions is:
Varptr: Return to Variable Address
Strptr: Returns the address of the real Unicode string buffer
Objptr: Returns the address of any object variable reference
Here are the specific usage of these three functions.
Strptr
This function is mainly used to generate an efficient Unicode API call. The call to the API function in the form of VB4, Unicode must be by means of byte arrays, for example:
Declare Sub MyunicodeCall LIB "Myunicodedll.dll" (PSTR AS BYTE)
Sub makecall (MyStr As String)
DIM btmp () as byte
BTMP = mystr & vbnullchar
MyunicodeCall BTMP (0)
MyStr = BTMP
MyStr = Left (mystr, len (mystr) -1)
End Sub
If you use strptr, the above code is streamlined:
Declare Sub MyunicodeCall LIB "Myunicodedll.dll" (PSTR AS BYTE)
Sub makecall (MyStr As String) MyunicodeCall Strptr (MySTR)
End Sub
The execution speed of Varptr / StrPtr / Objptr is very fast, so call the Unicode function as the system burden is actually less than the ANSI function corresponding to the call. Because the former does not need to convert.
StrPtr can also be used to optimize the call of the ANSI API function. Using strConv and strptr while calling to avoid transmitting a string variable to a function and performing a system burden caused by performing conversion operations for each call. For example, the original:
Declare Sub myansicall lib "myansidll.dll" (byval pstr as string)
Myansicall mystr
Now become:
Declare Sub myansicall lib "myansidll.dll" (Byval Pstr as long)
MyStr = STRCONV (MySTR, VBFROMUNICODE)
MyansicalL Strptr (MyStr)
MyStr = STRCONV (MYSTR, VBUNICODE) Note: Not always required
Strptr is the only way to intuitively tell you a different approach to a null string and NULL string. For NULL strings (VBnullString), the return value of StrPtr is 0, and for the empty string, the return value of the function is non-zero.
Varptr
This function can be used with API calls that require structures containing a Unicode string. If a MyudtVariable variable (a variable of a custom type) is passed to a parameter defined by byref udtparam as myudt, the conversion between ANSI / Unicode occurs. However, such a conversion will not occur if Varptr (MyudtVariable) is passed to the parameters defined by byval udtparam as long.
One thing to pay special attention is required, and the algorithm of the pointer in VB is very important. In addition, you must calculate the size of the elements, because VB will not help you complete this work. You must also deal with the lack of problematic issues of unsigned long-scale data types. The following function implements unsigned algorithm
Function unsingedd (Byval Start As Long, BYVAL INCR AS long) As long
Const Signbit as long = & h80000000
Unsignedadd = (Start Xor Signbit) Incr xor Signbit
END FUNCTION
Objptr
This function returns the interface pointer referenced by the object variable. Since most objects support multiple interfaces, it is very important to find that the image corresponds to which one of the objects is very important. Usually a function is used to place an object in a collection. By creating a keyword based on an object address, you can easily remove objects from a collection without having to open all elements throughout the collection. In many cases, the object address is the only thing that is reliable as a keyword, and examples are as follows:
Objcol.add myobj1, cstr (Objptr (myobj1))
.....
Objcol.remove cstr (Objptr (myobj1))