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

Last reviewed: June 21, 1995
Article ID: Q94700
The information in this article applies to:

- Standard and Professional Editions of Microsoft Visual Basic for

  Windows, versions 2.0 and 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.

  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 reference words: 1.00 2.00 3.00
KBCategory: kbprg kbcode
KBSubcategory: APrgOther


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: June 21, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.