dll通用操作单元
1/// <author>cxg 2019-3-4</author> 2/// 装载(释放)DLL 3/// 适用于Delphi所有版本 4 5unit ynDLL; 6 7interface 8 9uses 10 Classes, Windows, SysUtils; 11 12type 13 TDll = record 14 dllName: string; 15 dllHandle: Cardinal; 16 end; 17 18var 19 dllList: array of TDll; 20 21type 22 TynFun = function(params: string): string; stdcall; 23/// <summary> 24/// 执行指名DLL里面的指名函数 25/// </summary> 26/// <param name="dllName">DLL文件名</param> 27/// <param name="procName">函数名</param> 28/// <param name="inParams">函数入参</param> 29/// <returns>结果</returns> 30 31function ExecDllProc(const dllName, procName, inParams: string): string; 32/// <summary> 33/// 释放所有加载的DLL 34/// </summary> 35 36procedure FreeDllList; 37/// <summary> 38/// 获取指定文件夹里面的所有文件名,不包括其子文件夹 39/// </summary> 40/// <param name="path">文件夹</param> 41/// <param name="ext">文件扩展名,默认是所有类型</param> 42/// <returns></returns> 43 44function SearchFiles(path: string; ext: string = '*.*'): TStringList; 45/// <summary> 46/// 加载指名文件夹里面的所有DLL 47/// </summary> 48/// <param name="path">文件夹</param> 49 50procedure LoadAllDll(path: string); 51 52implementation 53 54function SearchFiles(path: string; ext: string = '*.*'): TStringList; 55var 56 SearchRec: TSearchRec; 57 found: integer; 58begin 59 Result := TStringList.Create; 60 found := FindFirst(path + '\' + ext, faAnyFile, SearchRec); 61 while found = 0 do 62 begin 63 if (SearchRec.Name <> '.') and (SearchRec.Name <> '..') and (SearchRec.Attr <> faDirectory) then 64 Result.Add(SearchRec.Name); 65 found := FindNext(SearchRec); 66 end; 67 FindClose(SearchRec); 68end; 69 70procedure FreeDllList; 71var 72 i: integer; 73begin 74 for i := Low(dllList) to High(dllList) do 75 begin 76 FreeLibrary(dllList[i].dllHandle); 77 end; 78end; 79 80procedure LoadAllDll(path: string); 81var 82 list: TStringList; 83 fullName: string; 84 i: integer; 85 handle: Cardinal; 86 dll: TDll; 87begin 88 list := SearchFiles(path, '*.dll'); 89 SetLength(dllList, list.Count); 90 for i := 0 to list.Count - 1 do 91 begin 92 fullName := path + '\' + list[i]; 93 handle := LoadLibrary(PChar(fullName)); 94 if handle <> 0 then 95 begin 96 dll.dllName := list[i]; 97 dll.dllHandle := handle; 98 dllList[i] := dll; 99 end; 100 end; 101 if Assigned(list) then 102 list.Free; 103end; 104 105function ExecDllProc(const dllName, procName, inParams: string): string; 106var 107 LHandle: Cardinal; 108 LPointer: Pointer; 109 LDll: TDll; 110 LSize: Integer; 111 112 function ExistDll(const dll: string): Cardinal; 113 var 114 i: Integer; 115 s: string; 116 begin 117 result := 0; 118 s := ExtractFileName(dll); 119 for i := 0 to High(dllList) do 120 begin 121 if SameText(s, dllList[i].dllName) then 122 begin 123 result := dllList[i].dllHandle; 124 Exit; 125 end; 126 end; 127 end; 128 129begin 130 Result := ''; 131 if (dllName = '') or (procName = '') then 132 Exit; 133 LHandle := ExistDll(dllName); 134 if LHandle = 0 then 135 begin 136 if LHandle = 0 then // dll not loaded 137 try 138 LHandle := LoadLibrary(PChar(dllName)); // load dll 139 LDll.dllName := ExtractFileName(dllName); 140 LDll.dllHandle := LHandle; 141 LSize := High(dllList); 142 if LSize = -1 then // dllList not init 143 begin 144 SetLength(dllList, 1); 145 dllList[0] := LDll; 146 end 147 else 148 begin 149 SetLength(dllList, LSize + 2); 150 dllList[LSize] := LDll; 151 end; 152 LPointer := GetProcAddress(LHandle, PChar(procName)); // load function 153 if LPointer <> nil then 154 begin 155 Result := TynFun(LPointer)(inParams) // execute function and get result 156 end; 157 except 158 FreeLibrary(LHandle); 159 end; 160 end 161 else 162 begin // dll is loaded 163 LPointer := GetProcAddress(LHandle, PChar(procName)); // load function 164 if LPointer <> nil then 165 begin 166 Result := TynFun(LPointer)(inParams) // execute function and get result 167 end; 168 end; 169end; 170 171end.