如何在一个程序中得到另一个已知文件名的程序是否正在运行或已经关闭?(100分)

  • 主题发起人 主题发起人 wz_hzb
  • 开始时间 开始时间
W

wz_hzb

Unregistered / Unconfirmed
GUEST, unregistred user!
已知有一个程序名为AAA.EXE,<br>如何在另一个程序中得到该程序是否正在运行或已经关闭?<br>不要用FINDWINDOW(),因为AAA.EXE窗口名在运行过程序中会改变。<br>
 
UP!我想是不是察看注册表,不过我不会
 
uses TLHelp32<br>&nbsp; <br>var lppe: TProcessEntry32; &nbsp;<br>found : boolean; &nbsp;<br>Hand : THandle; &nbsp;<br>begin &nbsp;<br>Hand := CreateToolhelp32Snapshot(TH32CS_SNAPALL,0); &nbsp;<br>found := Process32First(Hand,lppe); &nbsp;<br>while found do &nbsp;<br>begin &nbsp;<br>ListBox.Items.Add(StrPas(lppe.szExeFile));//列出所有进程。 &nbsp;<br>found := Process32Next(Hand,lppe); &nbsp;<br>end; &nbsp;<br>end; &nbsp;<br>---------------------------<br>uses ... TLHelp32, ... &nbsp;<br>type &nbsp;<br>TForm1 = class(TForm) &nbsp;<br>... &nbsp;<br>end; &nbsp;<br><br>var &nbsp;<br>Form1: TForm1; &nbsp;<br>l : Tlist; ////返回的东东在"L"这个TList中。 &nbsp;<br><br>type &nbsp;<br>TProcessInfo = Record &nbsp;<br>ExeFile : String; &nbsp;<br>ProcessID : DWORD; &nbsp;<br>end; &nbsp;<br>pProcessInfo = ^TProcessInfo; &nbsp;<br><br>implementation &nbsp;<br><br>{$R *.DFM} &nbsp;<br><br>procedure TForm1.FormCreate(Sender: TObject); &nbsp;<br>var p : pProcessInfo; &nbsp;<br>i : integer; &nbsp;<br>ContinueLoop:BOOL; &nbsp;<br>var &nbsp;<br>FSnapshotHandle:THandle; &nbsp;<br>FProcessEntry32:TProcessEntry32; &nbsp;<br>begin &nbsp;<br>l := TList.Create; &nbsp;<br>l.Clear; &nbsp;<br>FSnapshotHandle:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0); &nbsp;<br>FProcessEntry32.dwSize:=Sizeof(FProcessEntry32); &nbsp;<br>ContinueLoop:=Process32First(FSnapshotHandle,FProcessEntry32); &nbsp;<br>while integer(ContinueLoop)&lt;&gt;0 do &nbsp;<br>begin &nbsp;<br>New(p); &nbsp;<br>p.ExeFile := FProcessEntry32.szExeFile; &nbsp;<br>p.ProcessID := FProcessEntry32.th32ProcessID; &nbsp;<br>l.Add(p); &nbsp;<br>ContinueLoop:=Process32Next(FSnapshotHandle,FProcessEntry32); &nbsp;<br>end; &nbsp;<br>end; &nbsp;<br><br>procedure TForm1.FormDestroy(Sender: TObject); &nbsp;<br>var p : pProcessInfo; &nbsp;<br>i : integer; &nbsp;<br>begin &nbsp;<br>With l do &nbsp;<br>for i := Count - 1 DownTo 0 do &nbsp;<br>begin p := items; Dispose(p); Delete(i); end; &nbsp;<br>end; &nbsp;<br><br>... &nbsp;<br>end. &nbsp;<br>--------------------------------<br>再用pos()函数看看你要的程序名在不在进程列表里面就可以了<br>
 
如果是nt/2000上面的不管用,可利用psapi.dll
 
先得到进程列表,把这个列表弄进一个String,然后....pos(),就行了。
 
function FIsFileInUse(fName: string): boolean;<br>var<br>&nbsp; HFileRes: HFILE;<br>begin &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //判断是否文件启动<br>&nbsp; Result := false;<br>&nbsp; if not FileExists(fName) then<br>&nbsp; &nbsp; exit;<br>&nbsp; HFileRes := CreateFile(pchar(fName), GENERIC_READ or GENERIC_WRITE,<br>&nbsp; &nbsp; 0 {this is the trick!}, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);<br>&nbsp; Result := (HFileRes = INVALID_HANDLE_VALUE);<br>&nbsp; if not Result then<br>&nbsp; &nbsp; CloseHandle(HFileRes);<br>end;<br>以写的方式打开文件打不开的话就是正在被使用 <br><br>
 
问题上说fName是经常变得,不可以这么用吧!!
 
接受答案了.
 
后退
顶部