-
Nov 29th, 2021, 06:35 AM
#1
Thread Starter
Fanatic Member
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.
-
Nov 29th, 2021, 07:29 AM
#2
Re: CopyMemory to copy function address
Originally Posted by JAAFAR
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.
Last edited by Zvoni; Tomorrow at 31:69 PM.
----------------------------------------------------------------------------------------
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
-
Nov 29th, 2021, 08:22 AM
#3
Thread Starter
Fanatic Member
Re: CopyMemory to copy function address
Originally Posted by Zvoni
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 ?
-
Nov 29th, 2021, 09:37 AM
#4
Re: CopyMemory to copy function address
Originally Posted by JAAFAR
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.
Last edited by Zvoni; Tomorrow at 31:69 PM.
----------------------------------------------------------------------------------------
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
-
Nov 30th, 2021, 03:09 AM
#5
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.
Last edited by Zvoni; Tomorrow at 31:69 PM.
----------------------------------------------------------------------------------------
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
-
Nov 30th, 2021, 01:28 PM
#6
Re: CopyMemory to copy function address
Originally Posted by JAAFAR
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|