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通用操作单元的更多相关文章

  1. C++ dll 通用dll编写

    头文件 extern "C" _declspec(dllexport)void AddFunction(); cpp文件 extern "C" _declspe ...

  2. 那些年,用C#调用过的外部Dll

    经常有人找到我咨询以前在csdn资源里分享的dll调用.算算也写过N多接口程序.翻一翻试试写篇随笔. 明华IC读写器DLL 爱迪尔门锁接口DLL 通用OPOS指令打印之北洋pos打印机dll 明泰非接 ...

  3. VC++DLL动态链接库程序

    VC++DLL动态链接库程序 VC++DLL动态链接库程序 C++ DLL 导出函数 使用VS2017等IDE生成dll程序,示例如下: C++ DLL 导出类 1.导出类中第一种方法:简单导出类(不 ...

  4. Quartz.NET配置

    概述 Quartz.NET 在开源任务调度框架中的翘首,它提供了强大任务调度机制,难能可贵的是它同时保持了使用的简单性.Quartz 允许开发人员灵活地定义触发器的调度时间表,并可以对触发器和任务进行 ...

  5. Quarzt.NET 任务调度框架

      Quartz.NET是一个开源的作业调度框架,是 OpenSymphony 的 Quartz API 的.NET移植,它用C#写成,可用于winform和asp.net应用中.它提供了巨大的灵活性 ...

  6. windows XP系统内核文件分析(全)

    Windows XP个别 System32 文件 System32 文件夹下个别要移除的文件 我们就要删除另外600 个 system32 文件...我们要一次把它们全都解决掉. 以下是我所删除的 S ...

  7. 使用普通Windows服务创建Quartz.Net服务项目

    Quartz.NET 项目地址 http://quartznet.sourceforge.net/ 源码下载地址:Quartz.Net.2.0 首先创建Quartz.Net.2.0解决方案,添加 Wi ...

  8. 平时Error记录

    The Windows Firewall on this machine is currently 1.This row already belongs to another table. DataT ...

  9. 关于最新版本的log4net使用中遇到的问题

    Quartz.NET是一个开源的作业调度框架,是OpenSymphony 的 Quartz API的.NET移植,它用C#写成,可用于winform和asp.net应用中.它提供了巨大的灵活性而不牺牲 ...

随机推荐

  1. apiCloud app调用浏览器打开网页的方法

    在APP调用浏览器有两种方法:1.使用openApp2.使用openWin 两种方法调用浏览器后的效果有一点不同:1.使用openApp调用浏览器后,如果手机内有多个浏览器,会首先弹出选择浏览器的框2 ...

  2. 搜索引擎ElasticSearchV5.4.2系列二之ElasticSearchV5.4.2+kibanaV5.4.2+x-packV5.4.2安装

    相关博文: 搜索引擎ElasticSearchV5.4.2系列一之ES介绍 搜索引擎ElasticSearchV5.4.2系列二之ElasticSearchV5.4.2+klanaV5.4.2+x-p ...

  3. ajax请求成功但不执行success-function回调函数的问题

    在success:function(data){}下面加个error:function(){},看看是不是出错了走了error.如果是,说明返回值类型不符合要求. 比如:下面代码返回String类型. ...

  4. nginx:在centos中自启动

    参考网址:http://www.jb51.net/article/120545.htm # vi /etc/init.d/nginx #!/bin/sh # Name:nginx4comex # ng ...

  5. Shiro:ajax的session超时处理

    本问题解决方案参照网站多篇文章融合解决,在此表示感谢! 环境:springboot+shiro+jquery-easyui 问题:在ajax请求时,如果此时session已经失效,系统没有自动跳转到登 ...

  6. maven-replacer-plugin 静态资源版本号解决方案(css/js等)

    本文介绍如何使用 maven 的 com.google.code.maven-replacer-plugin 插件来自动添加版本号,防止浏览器缓存. 目录 1.解决方案 2.原始文件和最终生成效果 3 ...

  7. KDevelop调试NS2

    1.将NS2项目导入到KDevelop 具体操作步骤:打开Kdevelop,首先,选择"Project"->"Open/Import Project".然 ...

  8. .NetCore Cap 结合 RabbitMQ 实现消息订阅

    开源分布式消息框架 Cap 可以在GitHub上拉也可以通过nuget添加 上一篇博文写了 Windows RabbitMQ的安装使用 Cap支持事务,通过捕获数据库上下文连接对象实现 消息事务,消息 ...

  9. EFCore CodeFirst 适配数据库

    EF6中可以直接根据代码模型生成数据库Database.SetInitializer即可 在EFCore中如何实现呢? 这项功能放在了DatabaseFacade对象中,传入数据库上下文对象实例化到一 ...

  10. Redis学习笔记10--Redis主从复制

    redis主从复制配置和使用都非常简单.通过主从复制可以允许多个slave server拥有和master server相同的数据库副本.下面是关于redis主从复制的一些特点:       1.ma ...