Results 1 to 2 of 2

Thread: Memory Locations

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2000
    Posts
    84

    Question

    Does any one know how to return the memory location
    (longword) to which a variable refers?

    Since all variables are actually references to data in
    memory they are actually longword pointers.

    Also once this value is obtained how do you retrieve the
    byte value at this location?

  2. #2
    Frenzied Member
    Join Date
    Mar 2000
    Posts
    1,089
    varptr(MyVar)

    returns a pointer to the variable MyVar

    you can't access the memory directly but you can copy it to another variable

    put this at the top of your code

    Code:
    Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
    you have to use the Byval keyword to do the pointers

    Code:
    'a long variable
    Dim lngMyLong As Long
    
    ' an array of bytes
    Dim bytBytes(1 to 4) As Byte
    
    
    'and some pointers
    Dim ptrLong As Long
    Dim ptrBytes As Long
    
    ptrBytes = VarPtr(bytBytes(1))
    ptrLong = VarPtr(lngMyLong)
    
    'copy the bytes to the memory address at ptrLong
    CopyMemory ByVal ptrLong, bytBytes(1), 4
    
    'copy the memory at ptrBytes to the Long
    CopyMemory lngMyLong, ByVal ptyBytes, 4
    
    'copy the bytes to the long
    CopyMemory lngMyLong, bytBytes(1), 4
    
    
    'copy the memory at ptrBytes to the memory as ptrLong
    CopyMemory ByVal ptrLong, ByVal ptyBytes, 4
    all the copymemory lines do the same thing, when calling an API you can insert the ByVal argument infront of any argument (unless it's declared ByVal origianaly) this means that you are passing a pointer to a variable into the API instead of the variable itself

    hope this helps
    If it wasn't for this sentence I wouldn't have a signature at all.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width