开发者

Delphi DLL / Form communication

I have embedded a form in a DLL and can call the DLL and show the form and return various functions from the DLL back to the main app, however I cannot figure out how to get the DLL to trigger events in the main applications form.

For example in the main app I have a dataset and I want to have a button on the form i开发者_如何学Gon the DLL to goto a certain record in the dataset but cannot see how this is done.

Can anybody could point me to an example or give me some pointers on how to to this?


If a DLL needs to invoke behavior in the host application, then the host should provide a callback function to the DLL that the DLL stores and calls when appropriate.

Your DLL exports a function that tells it to display the form, right? Add a couple of parameters to that function for the EXE to provide a pointer to a callback function. The callback function should accept at least one parameter, which should be of type Pointer. The caller (the EXE) will use that parameter as a context parameter, some way for it to be reminded why the DLL is calling the EXE's function. Your DLL will store the function pointer and the context pointer, and when it's time for the DLL to tell the EXE something, it will call that function and pass the context value back. The DLL won't do anything with the context value; it's just something to store and pass back to the EXE verbatim.

The DLL's interface will look like this:

type
  TDllCallback = function(Context: Pointer): DWord; stdcall;

function DisplayForm(Parent: HWnd; Callback: TDllCallback; Context: Pointer): DWord; stdcall; external Dll;

The EXE will define a callback function like this:

function CallbackFunction(Context: Pointer): DWord; stdcall;
begin
  TMainForm(Context).DoSomething;
  Result := 0;
end;

It will call the DLL function like this:

procedure TMainForm.DoDllTaskClick(Sender: TObject);
begin
  DisplayForm(Handle, CallbackFunction, Pointer(Self));
end;

Notice how the signature of CallbackFunction matches the TDllcallback type defined earlier. Tey both use the stdcall calling convention, and they're both standalone functions, not methods. Avoid methods since method pointers are particular to Delphi, and you shouldn't require your DLL to be used only by Delphi hosts, if possible.


Because DLL's code being executed in the same address space of main application there are a plenty ways of doing communications.

  1. Define a callback function in the main app and give its address to the DLL.
  2. Define a message handler in the main app and send a message from DLL in response to some event.
  3. etc.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜