I've had to do a lot of work with pointers when working with the API. Hopefully some of you might find these items useful when you have to do the same.
Obviously, you will need StrPtr, VarPtr, and ObjPtr when passing pointers to API calls.
Be aware that when calling the API, VB pays close attention to whether or not you specified Byref (default) or Byval. When using Byref, VB actually generates a pointer to the variable in question and tosses that to the API instead. When you use Byval, the actual value is passed.
StrPtr gets the pointer to VB's unicode representation of a string. VB assumes that all API calls are ANSI, so when calling an API which requires Unicode strings, you are sort of out in the cold. By using this, you can get around such things. Typically, if your API call had a parameter declared as "Byref Var1 as string", you'd change that to "ByVal Var1 as Long". In either case, the API gets a pointer to a string... but in the former, VB is automatically creating a temporary ANSI string and passing that pointer. In the latter case, you bypass all that and pass a pointer directly to your Unicode string.
VarPtr has a similar use as StrPtr for the most part when dealing with UDTs (user defined datatypes, aka structures). When you pass an argument "Byref Var1 as MYTYPE" to an API call, you are really telling VB to make a copy of your structure, go through that copy and convert any strings to ANSI, then pass a pointer to that temporary copy.
When you specify "Byval Var1 as Long" and use VarPtr(var) (where var is of MYTYPE), you are giving the API a pointer directly to the UDT stored in your program's heap. No conversions of any kind are performed.
UDTs are stored (essentially) the same in memory as C/C++ structures. Each integer/value type is stored as the actual sequence of bytes depending on the size of the field. Others are stored as pointers to their respective objects.
One thing to note here is that C/C++ strings are often null-terminated. If you need to have some strings of that type in a UDT that gets passed to an API call in unicode, simply specify the string in the UDT as LONG, then use StrPtr to assign the LONG variable. The C/C++ API call will known enough to deref the pointer and get at the string itself. You may need to manually pad your string with CHR(0) though.
It is handy to note that VB sees int as 2-bytes (16-bit) and long as 4-bytes (32-bit).
If you need to receive a 64-bit value from an API call, you can use this handy little structure:
Public Type Int64
HighWord as Long
LowWord as Long
End Type
Then setup the parameter as "ByRef var1 as Int64". You can then do something ala:
Dim myBigInt as Int64
myBigInt.HighWord = 1
myBigInt.LowWord = 1
lRet = APICall(myBigInt)
ObjPtr gives you the pointer to the interface referenced by an object variable. This one can get really messy, and I recommend only using this for indexing objects in some sort of collection. When used that way, you can remove objects from a collection without walking the whole ball o' wax and using "IS" on every one, etc.
Lastly, when you need to pass a pointer to a FUNCTION to an API call, simply use the AddressOf operator.
Now we have a general idea about passing pointers to API calls... but that begs the question: what happens when APIs pass pointers back to us?
Its actually rather simple. There are two general cases: structures/UDTs and simple integer values, etc.
Use this function when dereferencing a pointer given to you by an API call:
Public Declare Sub DeRefPtr Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, ByVal PtrSource As Long, ByVal Length As Long)
In the latter case, simply call DeRefPtr myLongVar, ptrFromAPI, 4
It is very important to remember the sizes here... 2 = 16-bit int, 4 = 32-bit long. 99% of the time you'll get back a Long because the majority of Windows API calls operate on 32-bits. (32-bit OS, makes sense right?)
In the former case, you are actually getting a pointer to a structure of some kind. This is by far the most common use.
Simply do as such:
Dim Values as MYTYPE
DeRefPtr Values, ptrFromAPI, Len(Values)
Viola... pointer dereferenced (essentially.) What is actually happening is that the API is dereferencing the pointer, then copying the data contained therein to the location pointed to by your UDT. If you remember, the internal structure of VB and C structs/UDTs are the same, this works quite well.
Often, strings will be returned from APIs as an array of bytes (in the eyes of VB. In actuality, they are just null-terminated strings to the C/C++ world.) Use this function to deal with those:
Good luck, and remember to save often as a wrong move may net you a GPF rather quickly.
Obviously, you will need StrPtr, VarPtr, and ObjPtr when passing pointers to API calls.
Be aware that when calling the API, VB pays close attention to whether or not you specified Byref (default) or Byval. When using Byref, VB actually generates a pointer to the variable in question and tosses that to the API instead. When you use Byval, the actual value is passed.
StrPtr gets the pointer to VB's unicode representation of a string. VB assumes that all API calls are ANSI, so when calling an API which requires Unicode strings, you are sort of out in the cold. By using this, you can get around such things. Typically, if your API call had a parameter declared as "Byref Var1 as string", you'd change that to "ByVal Var1 as Long". In either case, the API gets a pointer to a string... but in the former, VB is automatically creating a temporary ANSI string and passing that pointer. In the latter case, you bypass all that and pass a pointer directly to your Unicode string.
VarPtr has a similar use as StrPtr for the most part when dealing with UDTs (user defined datatypes, aka structures). When you pass an argument "Byref Var1 as MYTYPE" to an API call, you are really telling VB to make a copy of your structure, go through that copy and convert any strings to ANSI, then pass a pointer to that temporary copy.
When you specify "Byval Var1 as Long" and use VarPtr(var) (where var is of MYTYPE), you are giving the API a pointer directly to the UDT stored in your program's heap. No conversions of any kind are performed.
UDTs are stored (essentially) the same in memory as C/C++ structures. Each integer/value type is stored as the actual sequence of bytes depending on the size of the field. Others are stored as pointers to their respective objects.
One thing to note here is that C/C++ strings are often null-terminated. If you need to have some strings of that type in a UDT that gets passed to an API call in unicode, simply specify the string in the UDT as LONG, then use StrPtr to assign the LONG variable. The C/C++ API call will known enough to deref the pointer and get at the string itself. You may need to manually pad your string with CHR(0) though.
It is handy to note that VB sees int as 2-bytes (16-bit) and long as 4-bytes (32-bit).
If you need to receive a 64-bit value from an API call, you can use this handy little structure:
Public Type Int64
HighWord as Long
LowWord as Long
End Type
Then setup the parameter as "ByRef var1 as Int64". You can then do something ala:
Dim myBigInt as Int64
myBigInt.HighWord = 1
myBigInt.LowWord = 1
lRet = APICall(myBigInt)
ObjPtr gives you the pointer to the interface referenced by an object variable. This one can get really messy, and I recommend only using this for indexing objects in some sort of collection. When used that way, you can remove objects from a collection without walking the whole ball o' wax and using "IS" on every one, etc.
Lastly, when you need to pass a pointer to a FUNCTION to an API call, simply use the AddressOf operator.
Now we have a general idea about passing pointers to API calls... but that begs the question: what happens when APIs pass pointers back to us?
Its actually rather simple. There are two general cases: structures/UDTs and simple integer values, etc.
Use this function when dereferencing a pointer given to you by an API call:
Public Declare Sub DeRefPtr Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, ByVal PtrSource As Long, ByVal Length As Long)
In the latter case, simply call DeRefPtr myLongVar, ptrFromAPI, 4
It is very important to remember the sizes here... 2 = 16-bit int, 4 = 32-bit long. 99% of the time you'll get back a Long because the majority of Windows API calls operate on 32-bits. (32-bit OS, makes sense right?)
In the former case, you are actually getting a pointer to a structure of some kind. This is by far the most common use.
Simply do as such:
Dim Values as MYTYPE
DeRefPtr Values, ptrFromAPI, Len(Values)
Viola... pointer dereferenced (essentially.) What is actually happening is that the API is dereferencing the pointer, then copying the data contained therein to the location pointed to by your UDT. If you remember, the internal structure of VB and C structs/UDTs are the same, this works quite well.
Often, strings will be returned from APIs as an array of bytes (in the eyes of VB. In actuality, they are just null-terminated strings to the C/C++ world.) Use this function to deal with those:
Code:
Private Function GetNameFromByteArray(src() As Byte) As String
Dim t As String
Dim zeropos As Long
t = StrConv(CStr(src), vbUnicode)
zeropos = InStr(t, Chr(0))
If zeropos > 1 Then t = Left(t, zeropos - 1)
GetNameFromByteArray = t
End Function
Good luck, and remember to save often as a wrong move may net you a GPF rather quickly.