Can I call a method which is placed in main application from dll code?
-
See this example: [How to implement a callback method within DLL (Delphi / TJVPluginManager + TJvPlugin)](http://stackoverflow.com/a/1839294/576719). – LU RD Mar 16 '12 at 11:49
1 Answers
seems there is only one way to do it - create a callback object. in your application you have to declare interface, wich describes your method, for example:
IMyMethodInterface = interface(IInterface)
procedure MyMethod(); stdcall;
end;
next you have to create class, wich implements this interface (and your method):
TMyMethodObject = class(TInterfacedObject, IMyMethodInterface)
public
procedure MyMethod(); stdcall;
end;
when you load DLL, you have to create TMyMethodObject
instance and pass its IMyMethodInterface
to dll; of course dll has to have corresponding method and export it (wich takes interface as parameter) SetMethodCallback
wich stores interface reference:
vars:
var mmo : IMyMethodInterface;
dllHandle : THandle;
smc : procedure (mmi : IMyMethodInterface); stdcall;
code:
mmo := TMyMethodObject.Create();
dllHandle := LoadLibrary('mydll.dll');
smc := GetProcAddress(dllHandle, 'SetMethodCallback');
if assigned(smc) then
smc(mmo);
now, you can use IMyMethodInterface reference in your dll to call method.
of course you can statically link dll and use it directly:
procedure SetMethodInteface(mmi : IMyMethodInterface); stdcall; external 'mydll.dll';
here is an DLL sample code:
library Project3;
// uses YourMethodIntf.pas
{$R *.res}
var AppMethod : IMyMethodInterface;
procedure SetAppMethodCallback(mmi : IMyMethodInterface); stdcall;
begin
AppMethod := mmi;
end;
procedure AnotherDllMethod();
begin
//here you can use AppMethod.MyMethod();
end;
exports
SetAppMethodCallback name 'SetMethodcallback';
begin
end.
take into account that your mmo
object (TMyMethodInterface
) will not be destroyed until you set AppMethod
in dll to nil
(or FreeLibrary dll ), so be careful

- 3,214
- 1
- 21
- 30
-
I know it's much to ask but could you post me complete skeleton of such dll? – Jacek Kwiecień Mar 16 '12 at 13:00
-
thanks a lot, I'll be looking into it from Monday, since my work hours are almost done for the week :) – Jacek Kwiecień Mar 16 '12 at 14:21
-
@JacekKwiecień you have to declare your interface in separate pas-file and then include it into both dll and app projects. – teran Mar 19 '12 at 07:03
-
figured it out :) what should I implement so functions LoadLibary and GetProcAddress are recognised by program?: EDIT: Figured that out too: Its Windows :) – Jacek Kwiecień Mar 19 '12 at 07:42
-
when I'm trying to execute AppMethod.MyMethod(); it throws exception - obviously because my AppMethod is nil - how and when should I initialize it? – Jacek Kwiecień Mar 19 '12 at 08:36
-
@JacekKwiecień I've uploaded sample project group (app + dll) to http://file.karelia.ru/zk458v/ (appCallback.zip) – teran Mar 19 '12 at 09:22
-
thanks you helped me a lot. I've made it working. inbelieveable :) – Jacek Kwiecień Mar 19 '12 at 13:35