B.2 The Undocumented VBA Functions

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

Visual Basic Shell Programming
Visual Basic Shell Programming
ISBN: B00007FY99
Year: 2000
Pages: 128

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net