dll通用操作单元
dll通用操作单元
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 | /// <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. |
本文来自博客园,作者:{咏南中间件},转载请注明原文链接:https://www.cnblogs.com/hnxxcxg/p/10504130.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?