下面以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. maven-bundle-plugin插件, 用maven构建基于osgi的web应用

    maven-bundle-plugin 2.4.0以下版本导出META-INF中的内容到MANIFEST.MF中 今天终于把maven-bundle-plugin不能导出META-INF中的内容到Ex ...

  2. Download interrupted: URL not found.

    Download interrupted: URL not found.   androidURL not found 应该是url被墙了.可以试下:启动 Android SDK Manager ,打 ...

  3. (六)学习MVC之标签a提交页面

    标签<a>如何做到与<input type="submit"/>一样有提交页面信息的效果? @using (Html.BeginForm("Log ...

  4. 用VisualSVN做项目版本控制

    一.SVN服务端 1.VisualSVN Server下载: http://download.csdn.net/detail/jiminull/4448874 或 http://www.visuals ...

  5. 编写高效的C程序与C代码优化 via jobbole

    http://blog.jobbole.com/82582/ 原文出处: codeproject 译文出处:CodingWu的博客 欢迎分享原创到伯乐头条

  6. java语言实现简单接口工具--粗简版

    2016注定是变化的一年,忙碌.网红.项目融资失败,现在有点时间整整帖子~~ 目标: 提高工作效率与质量,能支持平台全量接口回归测试与迭代测试也要满足单一接口联调测试. 使用人员: 测试,开发 工具包 ...

  7. HW6.19

    import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...

  8. 洛谷 P2279 03湖南 消防局的设立

    2016-05-30 16:18:17 题目链接: 洛谷 P2279 03湖南 消防局的设立 题目大意: 给定一棵树,选定一个节点的集合,使得所有点都与集合中的点的距离在2以内 解法1: 贪心 首先D ...

  9. leetcode@ [30/76] Substring with Concatenation of All Words & Minimum Window Substring (Hashtable, Two Pointers)

    https://leetcode.com/problems/substring-with-concatenation-of-all-words/ You are given a string, s, ...

  10. POJ2411 - Mondriaan's Dream(状态压缩DP)

    题目大意 给定一个N*M大小的地板,要求你用1*2大小的砖块把地板铺满,问你有多少种方案? 题解 刚开始时看的是挑战程序设计竞赛上的关于铺砖块问题的讲解,研究一两天楞是没明白它代码是怎么写的,智商捉急 ...