Delphi静态加载DLL和动态加载DLL示例
下面以Delphi调用触摸屏动态库xtkutility.dll为例子,说明如何静态加载DLL和动态加载DLL.
直接上代码。
1、静态加载示例
unit Unit1; interface uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls; type
TForm1 = class(TForm)
btnEnableTouch: TButton;
btnDisEnableTouch: TButton;
Label1: TLabel;
Memo1: TMemo;
procedure btnEnableTouchClick(Sender: TObject);
procedure btnDisEnableTouchClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end; { 声明一个回调函数类型 }
type
XTOUCH_ENUM_CALLBACK_PROC = function(pContext:Pointer;szSymbloicName:PChar;nType:Word):Boolean;stdcall; function EnumerateTouchInstance(hWnd:THandle;pContext:Pointer;pCallback:XTOUCH_ENUM_CALLBACK_PROC):DWORD;stdcall;external 'xtkutility.dll';
//功能:枚举系统中的所有触摸设备 function CreateDevice(szSymbolicName: PChar): THandle;stdcall;external 'xtkutility.dll';
//打开触摸设备
function CloseDevice(hFile: THandle): Boolean;stdcall;external 'xtkutility.dll';
//关闭触摸设备
procedure EnableTouch(hFile: THandle;bEnable: Boolean);stdcall;external 'xtkutility.dll';
//触摸控制 bEnable为true时允许触摸 bEnable为false时禁止触摸 function DisEnableTouchscreenCallback(pContext:Pointer;szSymbloicName:PChar;nType:Word): Boolean;stdcall;
//声明一个回调函数,禁止触摸所有触摸设备
function EnableTouchscreenCallback(pContext:Pointer;szSymbloicName:PChar;nType:Word): Boolean;stdcall;
//声明一个回调函数,允许触摸所有触摸设备
var
Form1: TForm1; implementation {$R *.dfm}
function DisEnableTouchscreenCallback(pContext:Pointer;szSymbloicName:PChar;nType:Word): Boolean;stdcall;
var
hDevice: THandle;
begin
hDevice := CreateDevice(szSymbloicName);
EnableTouch(hDevice,False);
CloseDevice(hDevice);
Result := True;
//显示触摸设备标识符
form1.Memo1.Clear;
Form1.Memo1.Lines.Add(szSymbloicName);
end; function EnableTouchscreenCallback(pContext:Pointer;szSymbloicName:PChar;nType:Word): Boolean;stdcall;
var
hDevice: THandle;
begin
hDevice := CreateDevice(szSymbloicName);
EnableTouch(hDevice,True);
CloseDevice(hDevice);
Result := True;
//显示触摸设备标识符
form1.Memo1.Clear;
Form1.Memo1.Lines.Add(szSymbloicName);
end;
procedure TForm1.btnEnableTouchClick(Sender: TObject);
var
dwNumsOfDevices: Word;
begin
dwNumsOfDevices := EnumerateTouchInstance(, nil , EnableTouchscreenCallback);
end; procedure TForm1.btnDisEnableTouchClick(Sender: TObject);
var
dwNumsOfDevices: Word;
begin
dwNumsOfDevices := EnumerateTouchInstance(, nil , DisEnableTouchscreenCallback);
end; end.
2、动态加载示例
unit Unit1; interface uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls; type
TForm1 = class(TForm)
btnEnableTouch: TButton;
btnDisEnableTouch: TButton;
Label1: TLabel;
Memo1: TMemo;
procedure btnEnableTouchClick(Sender: TObject);
procedure btnDisEnableTouchClick(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end; { 声明一个回调函数类型 }
type
XTOUCH_ENUM_CALLBACK_PROC = function(pContext:Pointer;szSymbloicName:PChar;nType:Word):Boolean;stdcall; procedure loadDll(dllName: PChar);
function DisEnableTouchscreenCallback(pContext:Pointer;szSymbloicName:PChar;nType:Word): Boolean;stdcall;
//声明一个回调函数,禁止触摸所有触摸设备
function EnableTouchscreenCallback(pContext:Pointer;szSymbloicName:PChar;nType:Word): Boolean;stdcall;
//声明一个回调函数,允许触摸所有触摸设备 //动态调用dll
type
TEnumerateTouchInstance = function(hWnd:THandle;pContext:Pointer;pCallback:XTOUCH_ENUM_CALLBACK_PROC):DWORD;stdcall;
TCreateDevice = function(szSymbolicName: PChar): THandle;stdcall;
TCloseDevice = function(hFile: THandle): Boolean;stdcall;
TEnableTouch = procedure(hFile: THandle;bEnable: Boolean);stdcall; var
Form1: TForm1;
DllHandle: THandle;
EnumerateTouchInstance: TEnumerateTouchInstance;
CreateDevice: TCreateDevice;
CloseDevice: TCloseDevice;
EnableTouch: TEnableTouch; implementation {$R *.dfm} procedure loadDll(DllName: PChar);
begin
try
if FileExists(DllName) then
begin
DllHandle := LoadLibrary(DllName);
if DllHandle = then
begin
raise Exception.Create('加载dll文件:' + DllName + '失败!');
end
else
begin
EnumerateTouchInstance := GetProcAddress(DllHandle,PChar('EnumerateTouchInstance'));
if @EnumerateTouchInstance = nil then
raise Exception.Create('定义函数EnumerateTouchInstance失败!'); CreateDevice := GetProcAddress(DllHandle,PChar('CreateDevice'));
if @CreateDevice = nil then
raise Exception.Create('定义函数CreateDevice失败!'); CloseDevice := GetProcAddress(DllHandle,PChar('CloseDevice'));
if @CloseDevice = nil then
raise Exception.Create('定义函数CloseDevice失败!'); EnableTouch := GetProcAddress(DllHandle,PChar('EnableTouch'));
if @EnableTouch = nil then
raise Exception.Create('定义函数EnableTouch失败!');
end;
end
else
begin
ShowMessage(DllName + '不存在!');
end;
except
on e: Exception do
begin
ShowMessage(e.Message);
end;
end;
end;
function DisEnableTouchscreenCallback(pContext:Pointer;szSymbloicName:PChar;nType:Word): Boolean;stdcall;
var
hDevice: THandle;
begin
hDevice := CreateDevice(szSymbloicName);
EnableTouch(hDevice,False);
CloseDevice(hDevice);
Result := True;
//显示触摸设备标识符
form1.Memo1.Clear;
Form1.Memo1.Lines.Add(szSymbloicName);
end; function EnableTouchscreenCallback(pContext:Pointer;szSymbloicName:PChar;nType:Word): Boolean;stdcall;
var
hDevice: THandle;
begin
hDevice := CreateDevice(szSymbloicName);
EnableTouch(hDevice,True);
CloseDevice(hDevice);
Result := True;
//显示触摸设备标识符
form1.Memo1.Clear;
Form1.Memo1.Lines.Add(szSymbloicName);
end;
procedure TForm1.btnEnableTouchClick(Sender: TObject);
var
dwNumsOfDevices: Word;
begin
//使所有触摸设备可以触摸
dwNumsOfDevices := EnumerateTouchInstance(, nil , EnableTouchscreenCallback);
end; procedure TForm1.btnDisEnableTouchClick(Sender: TObject);
var
dwNumsOfDevices: Word;
begin
//使所有触摸设备不可触摸
dwNumsOfDevices := EnumerateTouchInstance(, nil , DisEnableTouchscreenCallback); end; procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
FreeLibrary(DllHandle);
end; procedure TForm1.FormCreate(Sender: TObject);
var
DllName: string;
begin
DllName := ExtractFilePath(ParamStr()) + 'xtkutility.dll';
loadDll(PChar(DllName));
end; end.
Delphi静态加载DLL和动态加载DLL示例的更多相关文章
- vc静态加载dll和动态加载dll
如果你有a.dll和a.lib,两个文件都有的话可以用静态加载的方式: message函数的声明你应该知道吧,把它的声明和下面的语句写到一个头文件中 #pragma comment(lib, &quo ...
- Java之——Web项目中DLL文件动态加载方法
本文转自:https://blog.csdn.net/l1028386804/article/details/53903557 在Java Web项目中,我们经常会用到通过JNI调用dll动态库文件来 ...
- 在DLL中动态加载其所依赖的dll
windows下LoadLibrary函数的搜索顺序是先搜索system32等系统环境变量path下注册过的路径,然后是当前路径. 这里的相对路径是指的主exe所在路径,并且相对路径在程序运行过程中可 ...
- Vue加载组件、动态加载组件的几种方式
https://cn.vuejs.org/v2/guide/components.html https://cn.vuejs.org/v2/guide/components-dynamic-async ...
- c# 创建Excel com加载项Ribbon动态加载工作簿和工作表
使用 VSTO 创建外接程序,Gallery控件动态加载工作簿名称 代码如下: 加载工作簿名称: private void Gallery1_ItemsLoading(object sender, R ...
- android左右滑动加载分页以及动态加载数据
android UI 往右滑动,滑动到最后一页就自动加载数据并显示 如图: package cn.anycall.ju; import java.util.ArrayList; import java ...
- 动态链接库dll的 静态加载 与 动态加载
dll 两种链接方式 : 动态链接和静态链接(链接亦称加载) 动态链接是指在生成可执行文件时不将所有程序用到的函数链接到一个文件,因为有许多函数在操作系统带的dll文件中,当程序运行时直接从操作系统 ...
- c#实现动态加载Dll(转)
c#实现动态加载Dll 分类: .net2009-12-28 13:54 3652人阅读 评论(1) 收藏 举报 dllc#assemblynullexceptionclass 原理如下: 1.利用反 ...
- C#.Net 如何动态加载与卸载程序集(.dll或者.exe)3---- 动态加载Assembly应用程序
下载 supergraphfiles.exe 示例文件. 应用程序体系结构 在我专攻代码之前,我想谈谈我尝试做的事.您可能记得,SuperGraph 让您从函数列表中进行选择.我希望能够在具体的目录中 ...
随机推荐
- JAVA高级特性 - 注解
注解是插入到代码中用于某种工具处理的标签.这些标签可以在源码层次上进行操作,或者可以处理编译器将其纳入到注解类文件中. 注解不会改变对程序的编译方式.Java编译器会对包含注解和不包含注解的代码生成相 ...
- ajax post提交数据, input type=submit 返回prompt aborted by user
添加 return false;否则就报prompt aborted by user异常
- Bsie(鄙视IE)
http://www.bootcss.com/p/bsie/ 欢迎,这是bsie项目主页. 简介 bsie弥补了Bootstrap对IE6的不兼容.Bootstrap是 twitter.com 推出的 ...
- 经典SQL语句大全_主外键_约束
一.基础(建表.建约束.关系) 约束(Constraint)是Microsoft SQL Server 提供的自动保持数据库完整性的一种方法,定义了可输入表或表的单个列中的数据的限制条件(有关数据完整 ...
- 如何编写Linux设备驱动程序
一.Linux device driver 的概念 系统调用是操作系统内核和应用程序之间的接口,设备驱动程序是操作系统内核和机器硬件之间的接口.设备驱动程序为应用程序屏蔽了硬件的细节,这样在应用程序看 ...
- JDK1.5新特性(五)……Typesafe Enums
援引 Typesafe Enums - This flexible object-oriented enumerated type facility allows you to create enum ...
- 神经网络的学习 Neural Networks learing
1.一些基本符号 2.COST函数 ================Backpropagation Algorithm============= 1.要计算的东西 2.向前传递向量图,但为了计算上图的 ...
- 【转载】extern "C"的用法解析(原博主就是抄百度百科的,不如另外一篇好)
[说明]文章转载自Rollen Holt 的文章 http://www.cnblogs.com/rollenholt/archive/2012/03/20/2409046.html --------- ...
- dataStructure@ Check whether a given graph is Bipartite or not
Check whether a given graph is Bipartite or not A Bipartite Graph is a graph whose vertices can be d ...
- BNUOJ-26480 Horror List 最短路
题目链接:http://www.bnuoj.com/bnuoj/problem_show.php?pid=26480 题意:简单来说,就是给一个图,然后从每个honor list中的点求最短路.. 边 ...