PDA

View Full Version : Problem storing method types as longwords



cronodragon
19-09-2007, 09:50 PM
I want to store the pointer of a method inside a longword, like this:

type
TMyMethod = procedure of object;

var
Storage: LongWord;

begin
Storage := LongWord(@AnObject.ItsMethod);
..

Later I want to retrieve it like this:

var
Method: TMyMethod;

begin
LongWord(@Method) := Storage;

I have tested a code like this, and the method is called. The problem is that Self = nil. I suppose Delphi stores more than just the pointer to the method, it should also store the corresponding Self. How can I store both pointers in a type which is not TMyMethod, and how can I call the function to let it know which Self it should use? :?

JernejL
19-09-2007, 10:21 PM
I want to store the pointer of a method inside a longword, like this:

type
TMyMethod = procedure of object;

var
Storage: LongWord;

begin
Storage := LongWord(@AnObject.ItsMethod);
..

Later I want to retrieve it like this:

var
Method: TMyMethod;

begin
LongWord(@Method) := Storage;

I have tested a code like this, and the method is called. The problem is that Self = nil. I suppose Delphi stores more than just the pointer to the method, it should also store the corresponding Self. How can I store both pointers in a type which is not TMyMethod, and how can I call the function to let it know which Self it should use? :?

Are you out of your mind mixing pointers with 32 bit unsigned integer types? that code WILL break on any non-32 bit platform, what's wrong with just using a pointer type and use that???

cronodragon
19-09-2007, 10:55 PM
Are you out of your mind mixing pointers with 32 bit unsigned integer types? that code WILL break on any non-32 bit platform, what's wrong with just using a pointer type and use that???

I don't think there is something wrong about it, both types have the same size SizeOf(Pointer) = SizeOf(LongWord). Also I'm compiling it with Delphi for Win32, and I don't intend the code to be multiplatform... there would be a lot of things I'd have to change, starting with DirectX 9.

The method is being called correctly (I tested it), the problem is Self is not being passed. Any ideas?

EDIT: HAHAH! Thanks Delfi, I just tested the sizes of those types to make sure my post was right, and found the size of a method type is 8, not 4. Using Int64 to store it should fix the problem. :lol:

Mirage
20-09-2007, 05:24 AM
A method pointer contains not only a pointer to a method but also a pointer to an object (self). If you cast it to Int64 you can get it to work but it's all implementation specific and may not work with other compilers and other versions of Delphi.

marcov
28-10-2007, 02:47 PM
FPC is compatible with this (methodvars). But FPC also has 64-bits versions.

Note that 64-bits Windows is here, and Free Pascal runs on Win64, and that also has DirectX.

In this day of age this kind of typecasts is not smart, specially because there are afaik no speed benefits to be gained.

Pyrogine
28-10-2007, 04:34 PM
Take a look at the TMethod type in Delphi. As Mirage mentioned, a method consist of pointers to data (pointer to self) and code (pointer to method entry). Using TMethod makes it easier to do the type casting. Using it you can do something like this if you wish:function TMyClass.ExecProc(aProcName: string): Boolean;
Var
Proc : procedure of object;
Begin
TMethod(Proc).data := Self;
TMethod(Proc).code := MethodAddress(aProcName);
Result := True;
if not Assigned(Proc) Then
Result := False
else
Proc;
end;You can call published methods by name for example.

Hope this helps.