dll通用操作单元
dll通用操作单元
/// <author>cxg 2019-3-4</author>
/// 装载(释放)DLL
/// 适用于Delphi所有版本 unit ynDLL; interface uses
Classes, Windows, SysUtils; type
TDll = record
dllName: string;
dllHandle: Cardinal;
end; var
dllList: array of TDll; type
TynFun = function(params: string): string; stdcall;
/// <summary>
/// 执行指名DLL里面的指名函数
/// </summary>
/// <param name="dllName">DLL文件名</param>
/// <param name="procName">函数名</param>
/// <param name="inParams">函数入参</param>
/// <returns>结果</returns> function ExecDllProc(const dllName, procName, inParams: string): string;
/// <summary>
/// 释放所有加载的DLL
/// </summary> procedure FreeDllList;
/// <summary>
/// 获取指定文件夹里面的所有文件名,不包括其子文件夹
/// </summary>
/// <param name="path">文件夹</param>
/// <param name="ext">文件扩展名,默认是所有类型</param>
/// <returns></returns> function SearchFiles(path: string; ext: string = '*.*'): TStringList;
/// <summary>
/// 加载指名文件夹里面的所有DLL
/// </summary>
/// <param name="path">文件夹</param> procedure LoadAllDll(path: string); implementation function SearchFiles(path: string; ext: string = '*.*'): TStringList;
var
SearchRec: TSearchRec;
found: integer;
begin
Result := TStringList.Create;
found := FindFirst(path + '\' + ext, faAnyFile, SearchRec);
while found = 0 do
begin
if (SearchRec.Name <> '.') and (SearchRec.Name <> '..') and (SearchRec.Attr <> faDirectory) then
Result.Add(SearchRec.Name);
found := FindNext(SearchRec);
end;
FindClose(SearchRec);
end; procedure FreeDllList;
var
i: integer;
begin
for i := Low(dllList) to High(dllList) do
begin
FreeLibrary(dllList[i].dllHandle);
end;
end; procedure LoadAllDll(path: string);
var
list: TStringList;
fullName: string;
i: integer;
handle: Cardinal;
dll: TDll;
begin
list := SearchFiles(path, '*.dll');
SetLength(dllList, list.Count);
for i := 0 to list.Count - 1 do
begin
fullName := path + '\' + list[i];
handle := LoadLibrary(PChar(fullName));
if handle <> 0 then
begin
dll.dllName := list[i];
dll.dllHandle := handle;
dllList[i] := dll;
end;
end;
if Assigned(list) then
list.Free;
end; function ExecDllProc(const dllName, procName, inParams: string): string;
var
LHandle: Cardinal;
LPointer: Pointer;
LDll: TDll;
LSize: Integer; function ExistDll(const dll: string): Cardinal;
var
i: Integer;
s: string;
begin
result := 0;
s := ExtractFileName(dll);
for i := 0 to High(dllList) do
begin
if SameText(s, dllList[i].dllName) then
begin
result := dllList[i].dllHandle;
Exit;
end;
end;
end; begin
Result := '';
if (dllName = '') or (procName = '') then
Exit;
LHandle := ExistDll(dllName);
if LHandle = 0 then
begin
if LHandle = 0 then // dll not loaded
try
LHandle := LoadLibrary(PChar(dllName)); // load dll
LDll.dllName := ExtractFileName(dllName);
LDll.dllHandle := LHandle;
LSize := High(dllList);
if LSize = -1 then // dllList not init
begin
SetLength(dllList, 1);
dllList[0] := LDll;
end
else
begin
SetLength(dllList, LSize + 2);
dllList[LSize] := LDll;
end;
LPointer := GetProcAddress(LHandle, PChar(procName)); // load function
if LPointer <> nil then
begin
Result := TynFun(LPointer)(inParams) // execute function and get result
end;
except
FreeLibrary(LHandle);
end;
end
else
begin // dll is loaded
LPointer := GetProcAddress(LHandle, PChar(procName)); // load function
if LPointer <> nil then
begin
Result := TynFun(LPointer)(inParams) // execute function and get result
end;
end;
end; end.
dll通用操作单元的更多相关文章
- C++ dll 通用dll编写
头文件 extern "C" _declspec(dllexport)void AddFunction(); cpp文件 extern "C" _declspe ...
- 那些年,用C#调用过的外部Dll
经常有人找到我咨询以前在csdn资源里分享的dll调用.算算也写过N多接口程序.翻一翻试试写篇随笔. 明华IC读写器DLL 爱迪尔门锁接口DLL 通用OPOS指令打印之北洋pos打印机dll 明泰非接 ...
- VC++DLL动态链接库程序
VC++DLL动态链接库程序 VC++DLL动态链接库程序 C++ DLL 导出函数 使用VS2017等IDE生成dll程序,示例如下: C++ DLL 导出类 1.导出类中第一种方法:简单导出类(不 ...
- Quartz.NET配置
概述 Quartz.NET 在开源任务调度框架中的翘首,它提供了强大任务调度机制,难能可贵的是它同时保持了使用的简单性.Quartz 允许开发人员灵活地定义触发器的调度时间表,并可以对触发器和任务进行 ...
- Quarzt.NET 任务调度框架
Quartz.NET是一个开源的作业调度框架,是 OpenSymphony 的 Quartz API 的.NET移植,它用C#写成,可用于winform和asp.net应用中.它提供了巨大的灵活性 ...
- windows XP系统内核文件分析(全)
Windows XP个别 System32 文件 System32 文件夹下个别要移除的文件 我们就要删除另外600 个 system32 文件...我们要一次把它们全都解决掉. 以下是我所删除的 S ...
- 使用普通Windows服务创建Quartz.Net服务项目
Quartz.NET 项目地址 http://quartznet.sourceforge.net/ 源码下载地址:Quartz.Net.2.0 首先创建Quartz.Net.2.0解决方案,添加 Wi ...
- 平时Error记录
The Windows Firewall on this machine is currently 1.This row already belongs to another table. DataT ...
- 关于最新版本的log4net使用中遇到的问题
Quartz.NET是一个开源的作业调度框架,是OpenSymphony 的 Quartz API的.NET移植,它用C#写成,可用于winform和asp.net应用中.它提供了巨大的灵活性而不牺牲 ...
随机推荐
- KVM -> 虚拟化简介&虚拟机安装_01
什么是虚拟化? 在计算机技术中,虚拟化(技术)或虚拟技术(英语:Virtualization)是一种资源管理技术,是将计算机的各种实体资源(CPU.内存.磁盘空间.网络适配器等),予以抽象.转换后呈现 ...
- 启动虚拟机报错VMware Workstation cannot connect to the virtual machine
启动虚拟机报错: VMware Workstation cannot connect to the virtual machine. Make sure you have rights to run ...
- Pandas DataFrame数据的增、删、改、查
Pandas DataFrame数据的增.删.改.查 https://blog.csdn.net/zhangchuang601/article/details/79583551 #删除列 df_2 = ...
- SpringMvc定时器任务
在最近的工作中,涉及到一个定时任务,由于以前对springMVC使用较少,所以,上网找了一点资料.这个demo感觉挺好,推荐给大家. 使用到的JAR文件: aopalliance-1.0.jarcom ...
- 《JavaScript 高级程序设计》第一章:简介
JavaScript 历史 JavaScript的诞生的主要是当时的 netspace 公司谋求为自己的浏览器 Navigator 添加一种脚本语言,以便在本地客户端进行一些行为操作,而这一功能的需求 ...
- .NetCore Linux环境下安装InfluxDB以及配置设置
Linux下安装 确定需要安装的版本,我的linux是干净的,所以我需要先安装wget yum -y install wget 下载安装 wget https://dl.influxdata.com/ ...
- shell学习(二)
1.EOF Shell中通常将EOF与 <<和cat 结合使用,表示后续的输入作为子命令或子Shell的输入,直到遇到EOF为止,再返回到主调Shell. 可以把EOF替换成其他东西,意思 ...
- 【LOJ】#2073. 「JSOI2016」扭动的回文串
题解 就是一个回文串拼上左右两端 类似二分找lcp这么做 可以直接用哈希找回文串 注意要找A串前半部分,B串找后半部分 代码 #include <bits/stdc++.h> #defin ...
- CSS------如何让大小不一样的div中心对齐
如图: 代码: <div style = 'display:inline;color:green;font-size:30px;font-weight:bold'>¥1666.00< ...
- CSUOJ Water Drinking
Description The Happy Desert is full of sands. There is only a kind of animal called camel living on ...