o nie widać teraz rozwiązania ;(
skopiuję więc najważniejsze posty:
I'm writing a plugin architecture, each plugin object has an interface to each dll, the interface is defined as a class function in the plugin object, so I have to pass the entrance pointer of the class function to a dll to callback.
Thanks for the reply, I've solved the problem which a little trick of embeded asm, I'll close the question. This is my solution, hope useful for others.
========================================
procedure FreeClassProcInstance(ProcInstance: Pointer);
begin
// free memory
FreeMem(ProcInstance, 15);
end;
function MakeClassProcInstance(M: TMethod): Pointer;
begin
// allocate memory
GetMem(Result, 15);
asm
// MOV ECX,
MOV BYTE PTR [EAX], $B9
MOV ECX, M.Data
MOV DWORD PTR [EAX+$1], ECX
// POP EDX
MOV BYTE PTR [EAX+$5], $5A
// PUSH ECX
MOV BYTE PTR [EAX+$6], $51
// PUSH EDX
MOV BYTE PTR [EAX+$7], $52
// MOV ECX,
MOV BYTE PTR [EAX+$8], $B9
MOV ECX, M.Code
MOV DWORD PTR [EAX+$9], ECX
// JMP ECX
MOV BYTE PTR [EAX+$D], $FF
MOV BYTE PTR [EAX+$E], $E1
end;
end;
=================================
Simply call MakeClassProcInstance(TMethod(AClassFunctionPointer)) to retrive the pointer of the class function, and remember to call FreeClassProcInstance to reclaim memory when the pointer is no longer to use.
Slick812, here I give you a example of calling the two function:
=================
TPlatformInterface = function (const Msg, ParamA, ParamB:integer):integer of object;stdcall;
TPlugin = class
private
FPlatformInterface : TPlatformInterface;
FPlatformInterfaceInstance : Pointer;
public
constructor Create;
destructor Destroy; override;
function PlatformInterface(const Msg, ParamA, ParamB:integer):integer; stdcall;
end;
constructor TPlugin.Create;
begin
FPlatformInterface := PlatformInterface;
FPlatformInterfaceInstance := MakeClassProcInstance(TMethod(FPlatformInterface));
end;
destructor TPlugin.Destroy;
begin
FreeClassProcInstance(FPlatformInterfaceInstance);
end;
==============
now the FPlatformInterfaceInstance is a pure function pointer, you may use it as the same as other function pointers.
If you need more pointers to point to your other class methods, you have to declare more pointer variables for each class method, each instance pointer must be pointed to only one class method instance, and clear up them after you don't need them (or it will be causing memory leaks).