Results 1 to 6 of 6

Thread: CopyMemory to copy function address

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2013
    Posts
    658

    CopyMemory to copy function address

    Code:
    Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory"  _
         Destination As Any, Source As Any, ByVal Length As Long)
    
    
    Sub Test()
    
        Dim Dest As Long
        Dim Pnt As Long
    
        Pnt = Choose(1, AddressOf Test)
        CopyMemory Dest, ByVal Pnt, 4
    
        Debug.Print Dest , Pnt
    
    End Sub
    Hi,

    This is the logic:

    * Pnt is storing the address of the Test sub
    * Passing Pnt ByVal in the CopyMemory function should copy the actual value of Pnt into Dest (ie: Should copy the address of the Test Sub).
    * Output-Print should now display Dest equal to Pnt

    But the output prints two different values !
    Passing ByRef gives the expected result but isn't ByRef passing in this case the address of te address ?
    What am I doing\understanding incorrectly ?

    Thanks
    Last edited by JAAFAR; Nov 29th, 2021 at 06:44 AM.

  2. #2
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    2,785

    Re: CopyMemory to copy function address

    Quote Originally Posted by JAAFAR View Post
    Passing ByRef gives the expected result but isn't ByRef passing in this case the address of te address ?
    What am I doing\understanding incorrectly ?

    Thanks
    No. With ByRef you pass the Address of the Variable pnt, which has a Value of a Procedure-Address.
    CopyMemory doesn't care that the Value of pnt is an address. It's a Long which is 4 Bytes long

    Code:
        CopyMemory ByVal VarPtr(Dest), ByVal VarPtr(Pnt), LenB(Pnt)
    Gives the the correct values, too

    IOW:
    A call like
    CopyMemory Dest, ByVal 123456&, 4 is reading like
    "Take the Value at Memory-Address 123456 and copy it to Dest. By the way: It's 4 Bytes long"
    And the Value is different to the Address itself.
    With CopyMemory you pass the Address for Source and Dest, never values itself
    Last edited by Zvoni; Nov 29th, 2021 at 07:45 AM.
    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2013
    Posts
    658

    Re: CopyMemory to copy function address

    Quote Originally Posted by Zvoni View Post
    No. With ByRef you pass the Address of the Variable pnt, which has a Value of a Procedure-Address.
    CopyMemory doesn't care that the Value of pnt is an address. It's a Long which is 4 Bytes long

    Code:
        CopyMemory ByVal VarPtr(Dest), ByVal VarPtr(Pnt), LenB(Pnt)
    Gives the the correct values, too

    IOW:
    A call like
    CopyMemory Dest, ByVal 123456&, 4 is reading like
    "Take the Value at Memory-Address 123456 and copy it to Dest. By the way: It's 4 Bytes long"
    And the Value is different to the Address itself.
    With CopyMemory you pass the Address for Source and Dest, never values itself
    Thanks.. That is what I always do.

    With (CopyMemory Dest, ByVal Pnt, 4), what does the value in Dest represent, memory wise ?

  4. #4
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    2,785

    Re: CopyMemory to copy function address

    Quote Originally Posted by JAAFAR View Post
    Thanks.. That is what I always do.

    With (CopyMemory Dest, ByVal Pnt, 4), what does the value in Dest represent, memory wise ?
    If the Value of Pnt is e.g. "pnt = 123456", Dest receives the Value stored at Memory 123456

    Usually, in context of CopyMemory, you use ByVal always either with a Pointer-Function (VarPtr, StrPtr, ObjPtr), or with a Zero-Value ("0&").
    In case of Zero-Value, it always being Source, never Dest (You cannot copy anything to Memory-Address "0")
    Last edited by Zvoni; Nov 29th, 2021 at 09:42 AM.
    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  5. #5
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    2,785

    Re: CopyMemory to copy function address

    it's one of the things i really like about FreePascal: I can declare different variables (Not Pointers!) to point to the same memory-block
    Code:
    program Project1;
    
    Var
      p:Integer;
      i:Integer absolute p;   //This makes sure that i and p point to the same Memory-Block
    
    begin
      i:=5;
      Writeln(p);   //Returns 5 for p
      p:=10;
      Writeln(i);   //Returns 10 for i
    end.
    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  6. #6
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Posts
    3,589

    Re: CopyMemory to copy function address

    Quote Originally Posted by JAAFAR View Post
    With (CopyMemory Dest, ByVal Pnt, 4), what does the value in Dest represent, memory wise ?
    In Dest you get the opcodes of the first few instructions at the beginning of Test function.

    In the IDE this is usually a stub which calls into the interpreter passing the address of the p-code of the routine taking care of transferring the params from real stack to VM stack etc.

    cheers,
    </wqw>

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