大佬教程收集整理的这篇文章主要介绍了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: @R_489_8056@r; 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: @R_489_8056@r; 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: @R_489_8056@r; 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,inParams: String): String; var LHandle: Cardinal; LPointer: Pointer; LDll: TDll; LSize: @R_489_8056@r; function ExistDll(const dll: String): Cardinal; var i: @R_489_8056@r; 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通用操作单元全部内容,希望文章能够帮你解决dll通用操作单元所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。