Appendix B. Pointers

only for RuBoard - do not distribute or recompile

Appendix B. Pointers

CopyMemory is used a great deal throughout the course of this book to copy memory from one location to another. Without this function, it would be impossible to use pointers in Visual Basic. Since the function does exist, it is possible to write some very dangerous VB code. A short course on CopyMemory and pointers is definitely in order.

only for RuBoard - do not distribute or recompile
only for RuBoard - do not distribute or recompile

B.1 CopyMemory

There is really no function called CopyMemory . CopyMemory is merely an alias for the Win32 function RtlMoveMemory . Bruce McKinney, the author of Hardcore Visual Basic (Microsoft Press), was probably the first to use the alias CopyMemory . Now it is quite common to see RtlMoveMemory declared in this fashion. Its syntax (as reflected in a Visual Basic Declare statement) is:

 Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _ 
    (pDest As Any, pSource As Any, ByVal ByteLen As Long) 

where pDest is a pointer to the starting target address to which data is to be copied , pSource is a pointer to the starting address from which data is to be copied, and ByteLen is the number of bytes to be copied.

only for RuBoard - do not distribute or recompile
only for RuBoard - do not distribute or recompile

B.2 The Undocumented VBA Functions

CopyMemory does not do the job alone. Without the help of three undocumented functions VarPtr , StrPtr , and ObjPtr , this function would be useless. These functions return pointers to variables , strings, and objects, respectively. One or more of these functions usually gets the source or destination address that make up the arguments to CopyMemory . All three of these functions are located in the VB runtime DLL. Incidentally, all three functions are internally mapped to the same function, VarPtr , but you should use each function as it was designed to be used or you will have some problems.

B.2.1 VarPtr

This function is used to return a pointer to a variable. Not only does this include variables of all native VB datatypes, but UDTs as well. The function returns a Long value, which is the address of the variable. Do not use this function to get pointers to Strings; you will not get the value that you expect. Use StrPtr instead.

The following code fragment uses VarPtr to return the starting address of a user -defined type:

 Dim ft As FILETIME
Dim pft As Long
pft = VarPtr(ft) 

B.2.2 StrPtr

This function is used exclusively to return pointers to Strings. Never use VarPtr when you want the address of a String, since it returns a pointer to the ANSI buffer VB creates when passing Strings to API functions.

The following code fragment uses StrPtr to return the starting address of a Visual Basic string:

 Dim str As String
    str = "Hello, Kara!"

Dim pstr As Long
    pstr = StrPtr(str) 

B.2.3 ObjPtr

Use this function when you need to return the address of an Object. This is useful if you need to get at the vtable for a class (and quite dangerous, too). You can also use this function to determine whether an instance of a class is valid.

For example, the following code fragment determines whether adoConnection is a valid object reference and, if it is, calls its Close method and releases the object reference:

 If ObjPtr(adoConnection) <> 0 Then
    adoConnection.Close
    Set adoConnection = Nothing
End If 
only for RuBoard - do not distribute or recompile