Using Lstrcpy() API Function to Get Far Address of a Variable

ID: Q94700


The information in this article applies to:


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.

  1. 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.


  2. Enter the following code into the general declarations section:
    
       Declare Function Lstrcpy Lib "kernel" (p1 As Any, p2 As Any) As Long
     


  3. 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
     


  4. 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        : 

Last Reviewed: June 21, 1999