下面以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. 比较const ,readonly, stitac readonly

    比较const ,readonly, stitac readonly: const和readonly的值一旦初始化则都不再可以改写: const必须在声明时初始化:readonly既可以在声明时初始化 ...

  2. HAOI2007 理想的正方形

    1047: [HAOI2007]理想的正方形 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1402  Solved: 738[Submit][Sta ...

  3. 西南科技大学第十届ACM程序设计竞赛题解

    A.德州扑克 B. 我恨11(1089) 问题描述 11是一个孤独的数字,小明十分讨厌这个数字,因此如果哪个数字中出现了11或者该数字是11的倍数,他同样讨厌这个数字.现在问题来了,在闭区间[L,R] ...

  4. vmware 虚拟机 mount :no medium found解决方法

    使用vmware时,在虚拟机设置里,设置CD/DVD为系统镜像,挂载时,有时会有找不到介质或者no medium found之类的提示.根本原因是iso镜像并没有加载到虚拟机系统内.解决办法: 首先确 ...

  5. (转)理解OAuth 2.0

    转自:http://www.ruanyifeng.com/blog/2014/05/oauth_2_0.html OAuth是一个关于授权(authorization)的开放网络标准,在全世界得到广泛 ...

  6. 转换Json格式帮助类

    using System; using System.Collections.Generic; using System.Text; using System.Reflection; using Sy ...

  7. Spring整合Hessian

    Spring让Hessian变得不但强大,而且易用,但是易用背后,却有不少陷阱!   这个例子很简单,但实际上的确花费了我超过一小时的时间,排除了种种问题,最后问题终于水落石出.   整合以上篇Hel ...

  8. Balanced Numbers(数位+状压)

    题意:求给定区间,一个数的数位上每个奇数出现偶数次,每个偶数出现奇数次,这样数的个数 分析:先考虑状态,但总是想不全,所以要把状态压缩一下,用三进制,0 该数不放  1 放了奇数次 2放了偶数次 dp ...

  9. Nitrous挂VPN

  10. QT中使用 slot 传递 opencv 中得Mat对象以及 使用多线程集成开源代码。

    关于 slot传递 Mat 对象 以前一直是使用 Qtimer 定时器,设定超时后读取 dialog 对象的 Mat成员实现在 UI 里显示图像,发现这样对以后集成其他面向过程的代码增加了复杂度. 所 ...