下面以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示例的更多相关文章

  1. vc静态加载dll和动态加载dll

    如果你有a.dll和a.lib,两个文件都有的话可以用静态加载的方式: message函数的声明你应该知道吧,把它的声明和下面的语句写到一个头文件中 #pragma comment(lib, &quo ...

  2. Java之——Web项目中DLL文件动态加载方法

    本文转自:https://blog.csdn.net/l1028386804/article/details/53903557 在Java Web项目中,我们经常会用到通过JNI调用dll动态库文件来 ...

  3. 在DLL中动态加载其所依赖的dll

    windows下LoadLibrary函数的搜索顺序是先搜索system32等系统环境变量path下注册过的路径,然后是当前路径. 这里的相对路径是指的主exe所在路径,并且相对路径在程序运行过程中可 ...

  4. Vue加载组件、动态加载组件的几种方式

    https://cn.vuejs.org/v2/guide/components.html https://cn.vuejs.org/v2/guide/components-dynamic-async ...

  5. c# 创建Excel com加载项Ribbon动态加载工作簿和工作表

    使用 VSTO 创建外接程序,Gallery控件动态加载工作簿名称 代码如下: 加载工作簿名称: private void Gallery1_ItemsLoading(object sender, R ...

  6. android左右滑动加载分页以及动态加载数据

    android UI 往右滑动,滑动到最后一页就自动加载数据并显示 如图: package cn.anycall.ju; import java.util.ArrayList; import java ...

  7. 动态链接库dll的 静态加载 与 动态加载

    dll 两种链接方式  : 动态链接和静态链接(链接亦称加载) 动态链接是指在生成可执行文件时不将所有程序用到的函数链接到一个文件,因为有许多函数在操作系统带的dll文件中,当程序运行时直接从操作系统 ...

  8. c#实现动态加载Dll(转)

    c#实现动态加载Dll 分类: .net2009-12-28 13:54 3652人阅读 评论(1) 收藏 举报 dllc#assemblynullexceptionclass 原理如下: 1.利用反 ...

  9. C#.Net 如何动态加载与卸载程序集(.dll或者.exe)3---- 动态加载Assembly应用程序

    下载 supergraphfiles.exe 示例文件. 应用程序体系结构 在我专攻代码之前,我想谈谈我尝试做的事.您可能记得,SuperGraph 让您从函数列表中进行选择.我希望能够在具体的目录中 ...

随机推荐

  1. 代理服务器squid

    http://www.baidu.com/s?wd=squid%E4%BB%A3%E7%90%86%E6%9C%8D%E5%8A%A1%E5%99%A8&f=12&rsp=0& ...

  2. sdut Message Flood(c++ map)

    用字典树没过,学习了一下map; 参考博客:http://blog.csdn.net/zhengnanlee/article/details/8962432 AC代码 #include<iost ...

  3. jquery自动焦点图

    效果预览:http://runjs.cn/detail/vydinibc  带左右箭头的自动焦点图:http://runjs.cn/detail/wr1d1keh html: <div clas ...

  4. [selenium webdriver Java]常用api

    1. 获取元素文本 WebElement类的getText()方法返回元素的innerText属性.所以元素里如果有子节点一样也会被返回出来.如下所示 public class GetText { @ ...

  5. 【暑假】[深入动态规划]UVAlive 3983 Robotruck

     UVAlive 3983 Robotruck 题目: Robotruck   Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format ...

  6. uva 12296 Pieces and Discs

    题意: 有个矩形,左下角(0,0),左上角(L,W). 思路: 除了圆盘之外,本题的输入也是个PSLG,因此可以按照前面叙述的算法求出各个区域:只需把线段视为直线,用切割凸多边形的方法 :每次读入线段 ...

  7. javascript活动对象的理解——伪单例模式

    在自己研究javascript各种设计模式的过程中,偶然写出的一段代码让自己理解的更深刻了,之所以称之为伪单例模式,是因为这段代码造成的结果很想单例模式,但是实际上是活动对象捣乱所造成的误会. 代码很 ...

  8. BNUOJ-26480 Horror List 最短路

    题目链接:http://www.bnuoj.com/bnuoj/problem_show.php?pid=26480 题意:简单来说,就是给一个图,然后从每个honor list中的点求最短路.. 边 ...

  9. Android实例-消息框(XE8+小米2)

    方法一支持. 方法二与方法三都是三方单元,功能相同. 方法4与方法5报错,提示平台不支持. 第三方单元一: unit Android.JNI.Toast; // Java bridge class i ...

  10. python 定制类

    看到类似__slots__这种形如__xxx__的变量或者函数名就要注意,这些在Python中是有特殊用途的. __slots__我们已经知道怎么用了,__len__()方法我们也知道是为了能让cla ...