dll 与 exe 之间的通讯方式有很多种, 本文采用回调函数的方法实现, 本文也将研究多线程,多模块的情况下,回调函数所在的线程, 啥也不说了,先附上代码:
下面的是dll模块的的, dll的工程文件:
- 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.
dll中的类型文件
- 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 }
- ///保存exe的回调函数指针
- 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接受exe传过来的指令, dll留给exe的调用借口
- 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;
- //这个函数的目的就是检验不同模块下的不同线程下,回调函数的执行线程
- 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的文件
- 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,留给dll调用的回调函数
- 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,简单实现的dll与exe之间的通讯, 其实就是利用了dll的导出函数, 先想dll传递一个回调函数的地址,供dll面向exe的通讯。 exe面向dll 的通讯直接执行dll导出函数即可
2, 本文是最基本的实现,当然其中的回调函数,以及dll的处理函数,可以在数据包的级别上实现, 即: 定义不同数据包,根据命令执行不同的函数, 这样只要导出一个函数,保留一个回调函数 就可以实现大量的功能
3, 关于回调函数的线程执行空间,取决于调用者所在的线程, 比如dll线程中回调exe的函数, 则回调函数是执行在该dll线程。 如果是主线程执行回调,则在主线程。 上述代码GDLLclass的创建过程是导出函数,所以GDLLclass的主线程就是exe的主线程,他们在一个线程空间
http://blog.csdn.net/procedure1984/article/details/3985127