There are many kinds of communication between DLL and EXE, this paper uses the method of callback function, this article will also study multi-threaded, multi-module case, the callback function where the thread, do not say, the first attached code:
The following is the DLL module, the DLL's project file:
[Delphi]View Plaincopy
- Library Dllapp;
- Uses
- Windows
- Sysutils,
- Classes,
- Dllclass in ' Dllclass.pas ';
- {$R *.res}
- var
- Gdllserver:tdllserver;
- function Addserver (adispatchfunc:tdispatchfunc): HRESULT; stdcall;
- begin
- Result: = error_invalid_function;
- If not Assigned (gdllserver) Then
- Exit;
- Gdllserver. Addserver (Adispatchfunc);
- Result: = ERROR_SUCCESS;
- end;
- function Datadispatch (acommand:integer): HRESULT; stdcall;
- begin
- Result: = error_invalid_function;
- Gdllserver. Datadispatch (Acommand);
- Result: = 0;
- end;
- function Dllinitialize:hresult;
- begin
- Result: = 1;
- Gdllserver: = Tdllserver. Create;
- Result: = ERROR_SUCCESS;
- end;
- function Dllfinalize:hresult;
- begin
- Result: = error_invalid_function;
- Gdllserver. Free;
- Gdllserver: = nil;
- Result: = ERROR_SUCCESS;
- end;
- Exports
- Addserver,
- Datadispatch,
- Dllinitialize,
- Dllfinalize;
- Begin
- End.
The type file in the DLL
[Delphi]View Plaincopy
- Unit Dllclass;
- Interface
- Uses
- Windows, Messages, sysutils, variants, Classes, Graphics, Controls, Forms,
- Dialogs, Stdctrls;
- Type
- Tdispatchfunc = function (acommand:integer): Hresult;stdcall;
- Type
- Tdllserver = CLAss;
- Tdllthread = Class (TThread)
- Private
- Fcount:integer;
- Fdllserver:tdllserver;
- Public
- procedure Execute; override;
- Public
- constructor Create;
- end;
- Tdllserver = class
- Private
- Fdispatchfunc:tdispatchfunc;
- Fdllthread:tdllthread;
- Public
- procedure Addserver (Adispatchfunc:tdispatchfunc);
- procedure Datadispatch (Acommand:integer);
- Public
- constructor Create;
- destructor Destroy;
- end;
- Implementation
- {Tdllserver}
- Save EXE's callback function pointer
- Procedure Tdllserver. Addserver (Adispatchfunc:tdispatchfunc);
- Begin
- Fdispatchfunc: = Adispatchfunc;
- End
- Constructor Tdllserver. Create;
- Var
- lthreadid:cardinal;
- Begin
- ///
- Lthreadid: = GetCurrentThreadID;
- ShowMessage (' dll,create: ' +inttostr (Lthreadid));
- Fdllthread: = Tdllthread. Create;
- Fdllthread. Fdllserver: = self;
- Fdllthread. Resume;
- End
- DLL accepts EXE pass-through instruction, DLL leaves EXE to invoke an excuse
- Procedure Tdllserver. Datadispatch (Acommand:integer);
- Var
- lthreadid:cardinal;
- Begin
- If not Assigned (fdispatchfunc) Then
- Exit;
- Fdispatchfunc (Acommand);
- Lthreadid: = GetCurrentThreadID;
- ShowMessage (' Dll,datedispatch: ' +inttostr (Lthreadid));
- End
- destructor Tdllserver. Destroy;
- Begin
- ////
- End
- {Tdllthread}
- Constructor Tdllthread. Create;
- Var
- lthreadid:cardinal;
- Begin
- Fcount: = 0;
- inherited Create (True);
- End
- The purpose of this function is to verify the execution thread of the callback function under different threads under different modules.
- Procedure Tdllthread. Execute;
- Var
- lthreadid:cardinal;
- Begin
- inherited;
- While not Terminated do
- begin
- INC (Fcount);
- if Fcount = 2 Then
- begin
- Lthreadid: = GetCurrentThreadID;
- ShowMessage (' dll,thread--' +inttostr (Lthreadid));
- end;
- If Fcount = 5 Then
- begin
- if assigned (fdllserver) Then
- begin
- Fdllserver. Fdispatchfunc (2);
- end;
- end;
- Sleep (1000);
- end;
- End
- End.
EXE's files
[C-sharp]View Plaincopy
- Unit Execlass;
- Interface
- Uses
- Windows, Messages, sysutils, variants, Classes, Graphics, Controls, Forms,
- Dialogs, Stdctrls;
- Type
- Tdispatchfunc = function (Acommand:integer): Hresult;stdcall;
- Tserverfunc = function (adispatchfunc:tdispatchfunc): Hresult;stdcall;
- Tproc = Function:hresult;
- Type
- TForm2 = Class (Tform)
- Button1:tbutton;
- Memo1:tmemo;
- Procedure Formdestroy (Sender:tobject);
- Procedure Button1Click (Sender:tobject);
- Procedure Formcreate (Sender:tobject);
- Private
- {Private declarations}
- Fserverdispatch:tserverfunc; ///exe--->dll
- Fdatadispatch:tdispatchfunc; ///exe--->dll
- Fdllinitialize:tproc;
- Fdllfinalize:tproc;
- Public
- {Public declarations}
- End
- Var
- Form2:tform2;
- Ghandlelibrary:thandle;
- Gcount:integer;
- Implementation
- {$R *.DFM}
- DLL--->exe, the callback function left for DLL calls
- function Datadispatch (Acommand:integer): HRESULT; stdcall;
- Var
- lthreadid:cardinal;
- Begin
- Lthreadid: = GetCurrentThreadID;
- ShowMessage (' EXE: ' +inttostr (Lthreadid));
- End
- Procedure Tform2.button1click (Sender:tobject);
- Begin
- INC (Gcount);
- Fdatadispatch (Gcount);
- End
- Procedure Tform2.formdestroy (Sender:tobject);
- Begin
- Fdllfinalize;
- Fserverdispatch: = nil;
- Fdatadispatch: = nil;
- Fdllinitialize: = nil;
- Fdllfinalize: = nil;
- FreeLibrary (ghandlelibrary);
- End
- Procedure Tform2.formcreate (Sender:tobject);
- Var
- lthreadid:cardinal;
- Begin
- Ghandlelibrary: = LoadLibrary (PChar (' DLLAPP.dll '));
- @FDllInitialize: = GetProcAddress (ghandlelibrary, ' dllinitialize ');
- @FDllFinalize: = GetProcAddress (ghandlelibrary, ' dllfinalize ');
- @FServerDispatch: = GetProcAddress (ghandlelibrary, ' addserver ');
- @FDataDispatch: = GetProcAddress (ghandlelibrary,' Datadispatch ');
- Fdllinitialize;
- Fserverdispatch (Datadispatch);
- Lthreadid: = GetCurrentThreadID;
- MEMO1.LINES.ADD (IntToStr (Lthreadid));
- End
- End.
1, the simple implementation of the DLL and the communication between the EXE, in fact, the use of the DLL's export function, first think of the DLL to pass the address of a callback function, for the DLL-oriented EXE communication. EXE DLL-oriented communication directly execute the DLL export function can be
2, this article is the most basic implementation, of course, the callback function, as well as the handler function of the DLL, can be implemented at the level of the packet, that is: define different packets, according to the command to execute different functions, so long as the export of a function, retain a callback function can achieve a large number of functions
3, on the thread execution space of the callback function, depending on the caller's thread, such as the callback EXE function in the DLL thread, the callback function is executed on the DLL thread. If it is the main thread that executes the callback, then the main thread. The above code Gdllclass creation process is the export function, so the main thread of Gdllclass is the main thread of EXE, they are in a threaded space
http://blog.csdn.net/procedure1984/article/details/3985127
The communication call between the DLL and the EXE and the thread execution space of the callback function