Article ID: 94700
Article Last Modified on 12/12/2003
APPLIES TO
- Microsoft Visual Basic 2.0 Standard Edition
- Microsoft Visual Basic 3.0 Professional Edition
- Microsoft Visual Basic 2.0 Professional Edition
- Microsoft Visual Basic 3.0 Professional Edition
This article was previously published under Q94700
SUMMARY
You can use the Windows API function Lstrcpy() to get the far address of a variable as a Long integer.
The Lstrcpy() function returns the same value as its first argument, which is the address of a variable. Usually you would use the Lstrcpy() function to copy strings that are terminated by a zero byte. However, if you pass the same variable as both the source and the destination, Lstrcpy() copies the variable to itself, which has no effect.
MORE INFORMATION
Basic cannot deal with pointers directly. All Basic can do with a pointer is pass it as a parameter to a DLL function.
Basic variables may move in memory. You should take the address of a variable immediately before you use it.
The following steps demonstrate how to get the address of an integer and a variable-length string.
- Run Visual Basic, or from the File menu, choose New Project (ALT, F, N) if Visual Basic is already running. Form1 is created by default.
Enter the following code into the general declarations section:
Declare Function Lstrcpy Lib "kernel" (p1 As Any, p2 As Any) As Long
Enter the following code into the Click event handler:
Sub Form_Click () Dim ptr As Long ' pointer value Dim x1 As Integer ' variable to take address of Dim x2 As String ' variable to take address of x1 = 123 ptr = Lstrcpy(x1, x1) MsgBox "The address of x1 is: " + Hex$(ptr) x2 = "x2" ' must use ByVal on variable length strings ptr = Lstrcpy(ByVal x2, ByVal x2) MsgBox "The address of x2 is: " + Hex$(ptr) End Sub
- Press the F5 key to run the program. It displays the address of the variable in hexadecimal.
Additional query words: 1.00 2.00 3.00
Keywords: KB94700