判断DLL的存在问题(200分)

  • 主题发起人 主题发起人 f2000
  • 开始时间 开始时间
F

f2000

Unregistered / Unconfirmed
GUEST, unregistred user!
判断DLL的存在问题<br><br>我想做一个函数 FISExistDLL(sDLLName:String):Boolean<br>sDLLName是给出的DLL文件名 <br>如果改DLL在内存中 返回为 True <br>没有被加载返回为  &nbsp; &nbsp; &nbsp; &nbsp; False<br><br>该怎么写 谢谢
 
unit Unit1;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br>&nbsp; ComCtrls, StdCtrls, ExtCtrls;<br><br>type<br><br><br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; ListBox1: TListBox;<br>&nbsp; &nbsp; Panel1: TPanel;<br>&nbsp; &nbsp; Button2: TButton;<br>&nbsp; &nbsp; ListBox2: TListBox;<br>&nbsp; &nbsp; Splitter1: TSplitter;<br>&nbsp; &nbsp; procedure Button2Click(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br>var<br>&nbsp; Form1: TForm1;<br><br>implementation<br>uses psapi;<br>{$R *.DFM}<br><br>procedure TForm1.Button2Click(Sender: TObject);<br>type<br>integer = DWORD; // different versions of psapi.pas floating around<br>var<br>&nbsp; i,j,pidNeeded,modNeeded : Integer;<br>&nbsp; PIDList : array[0..1000] of Integer; // 1000 should be enough<br>&nbsp; MODList : array[0..1000] of HInst;<br>&nbsp; PIDName : array [0..MAX_PATH - 1] of char;<br>&nbsp; MODName : array [0..MAX_PATH - 1] of char;<br>&nbsp; PH : THandle;<br>begin<br><br>&nbsp; // fill an array with process ids<br>&nbsp; if not enumprocesses (@PIDList, 1000, pidNeeded) then<br>&nbsp; begin<br>&nbsp; &nbsp; ListBox1.Items.Add('Need psapi.dll');<br>&nbsp; &nbsp; exit;<br>&nbsp; end;<br><br>&nbsp; // now open each process and its modules<br>&nbsp; for i := 0 to (pidNeeded div sizeof (Integer)- 1) do<br>&nbsp; begin<br>&nbsp; &nbsp; // get a handle to the process<br>&nbsp; &nbsp; PH := OpenProcess (PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, False,<br>&nbsp; &nbsp; PIDList);<br><br>&nbsp; &nbsp; if PH &lt;&gt; 0 then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; // print the process name<br>&nbsp; &nbsp; &nbsp; if GetModuleBaseName (PH, 0, PIDName, sizeof (PIDName)) &gt; 0 then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; ListBox1.Items.Add('process : ' + PIDName);<br><br>&nbsp; &nbsp; &nbsp; &nbsp; // fill an array of modules associated with this process<br>&nbsp; &nbsp; &nbsp; &nbsp; if not EnumProcessModules (PH,@MODList,1000, modNeeded) then modNeeded:= 0;<br><br>&nbsp; &nbsp; &nbsp; &nbsp; // print the modules in the list<br>&nbsp; &nbsp; &nbsp; &nbsp; for j := 0 to (modNeeded div sizeof (hInst) - 1) do<br>&nbsp; &nbsp; &nbsp; &nbsp; if GetModuleFileNameEx (PH, MODList[j], MODName,sizeof(MODName)) &gt; 0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ListBox1.Items.Add(' module: ' + MODName);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if LowerCase(trim(ExtractFileName(ModName))) = LowerCase('MHK.dll') then Caption := ('Keyboard hook already running'); //注意这里<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; if PH &gt; 0 then CloseHandle(PH);<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; end;<br>&nbsp; end;<br>&nbsp; <br>end;<br><br>end.<br>
 
很简单<br>直接GetModuleHandle<br>如果返回非 0 则为存在,否则不存在
 
LoadLibrary('动态链接库名')<br>如果返回值不等于0则说明存在
 
如果是在一个进程内判断,同意LiChaoHui.<br>如果是判断某DLL是不是被别的进程加载,则要想想办法了.
 
接受答案了.
 
后退
顶部