DLL与EXE之间的通讯调用 以及 回调函数的线程执行空间
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
DLL与EXE之间的通讯调用 以及 回调函数的线程执行空间的更多相关文章
- Python并发编程06 /阻塞、异步调用/同步调用、异步回调函数、线程queue、事件event、协程
Python并发编程06 /阻塞.异步调用/同步调用.异步回调函数.线程queue.事件event.协程 目录 Python并发编程06 /阻塞.异步调用/同步调用.异步回调函数.线程queue.事件 ...
- [Effective JavaScript 笔记]第67条:绝不要同步地调用异步的回调函数
设想有downloadAsync函数的一种变种,它持有一个缓存(实现为一个Dict)来避免多次下载同一个文件.在文件已经被缓存的情况下,立即调用回调函数是最优选择. var cache=new Dic ...
- setInterval调用ajax回调函数不执行的问题
setInterval调用ajax回调函数不执行 1.首先检查你的setInterval()函数写法是否正确 参考写法 // 检查是否支付成功 var isPayRequest=false; var ...
- frameset,iframe框架之间如何互相调用变量、函数
以往一直在编写的都是前台的UI,很少使用到frameset.iframe,对其了解也是十分有限,只是知道其可以为其当前页面引入html文件成为当前页的一部分,但是这两天在做后台UI界面的时候,发现这样 ...
- C#调用C++回调函数的问题
C++的回调函数中有一个参数是,是返回一个字符串,原则如下: typedef void (*TDataEvent)(char *AData ,int ALen); 其中char ...
- (转载)spring 之间的远程调用-Spring Http调用的实现
原文:https://www.cnblogs.com/lewisat/p/6132082.html 1:Spring Http设计思想 最近在研究公司自己的一套rpc远程调用框架,看到其内部实现的设计 ...
- python 调用dll 动态链接库 结构体参数及回调函数等示例
结构体示例: 这里是 C 代码的部分,主要是结构体的声明和回调函数定义. // 新版本定义 typedef enum { DevCard, DevLocator, DevReader } DevTyp ...
- python处理多线程之间事件通讯方法
一.什么是事件 每执行一个事情,肯定有该事情的执行后状态,那事件就是该事情发生的信号 在程序中,多线程之间需要通讯,而事件就是方便线程之间的通讯 案例: 1.服务器启动需要5秒 2.客服端启动后去链接 ...
- Silverlight中异步调用WCF服务,传入回调函数
以前学的ASP.NET,调用的都是同步方法,同步方法的好处就是,一步一步走,完成这步才会走下一步.然而,WCF使用的都是异步方法,调用之后不管有没有获得结果就直接往下走,最可恶的是异步函数都是Void ...
随机推荐
- spring mvc 的基本注解
刚开始学习spring mvc 有很多东西不是很了解 spring mvc 四个基本注解 annotation(控制层,业务层,持久层) -- @Component.@Repository @Se ...
- 数据库分页【Limt与Limt..OFFSET 】
数据起始 SELECT * from xiaoyao_blogs_essay limit 20 , 15;解释:20是起始位置,15是页容量.因为id是从15开始的 SELECT * from xi ...
- mongodb数据库调试问题:‘db object already connecting, open cannot be called multiple times’
在微博小系统的调试过程中: (1)登入登出可以正常显示,就是在注册的时候网络连接突然停止,但是用户名和密码已经存入数据库中,报错为:undefined is not a function 错误主要指向 ...
- poj 3270 置换
poj 置换的应用 黑书原题P248 /** 题意: 给定序列, 将其按升序排列, 每次交换的代价是两个数之和, 问代价最小是多少 思路:1.对于同一个循环节之内的,肯定是最小的与别的交换代价最小 2 ...
- 在实体对象中访问导航属性里的属性值出现异常“There is already an open DataReader associated with this Command which must be closed first”
在实体对象中访问导航属性里的属性值出现异常“There is already an open DataReader associated with this Command which must be ...
- Minix
[1] MINIX是一种基于微内核架构的类UNIX计算机操作系统,由Andrew S. Tanenbaum发明.MINIX最初发布于1987年,开放全部源代码给大学教学和研究工作.2000年重新改为 ...
- Spring jdbctemplate学习笔记
/*List<?> config = getDB(" select t.datavalue from sys_config t where t.configid = '15' & ...
- 再探Delphi2010 Class的构造和析构顺序
发了上一篇博客.盒子上有朋友认为Class的构造和析构延迟加载.是在Unit的初始化后调用的Class的构造.在Unit的反初始化前调用的Class的析构函数. 为了证明一下我又做了个试验 unit ...
- 将 SQL Server 实例设置为自动启动(SQL Server 配置管理器)
本主题说明如何使用 SQL Server 配置管理器在 SQL Server 2012 中将 SQL Server 实例设置为自动启动. 在安装过程中,SQL Server 通常配置为自动启动. 如果 ...
- sicily-1029 Rabbit
一. 题意(0.04s) 每一对成熟的兔子可以生一对兔子,兔子在m个月之后成熟,假设兔子都不会死,计算d个月后一共有多少只兔子. 二. 要高精度加法(用string) 三. ...