Using Lstrcpy() API Function to Get Far Address of a Variable
ID: Q94700
|
The information in this article applies to:
-
Microsoft Visual Basic Standard and Professional Editions for Windows, versions 2.0, 3.0
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 :
Version :
Platform :
Issue type :