如何取得某一应用程序是否执行(100分)

  • 主题发起人 主题发起人 老郭
  • 开始时间 开始时间

老郭

Unregistered / Unconfirmed
GUEST, unregistred user!
怎么知道某一程序是否执行了?比如QQ运行了,运行此程序会提示QQ运行或未运行!用getmodulehandle可以吗?
 
通过API查看进度就OK了。
 
procedure TfrmMain.Timer1Timer(Sender: TObject);<br>var vHandle:HWND;<br>begin<br> &nbsp;vHandle := FindWindow(nil,'QQ.exe');<br> &nbsp;if vHandle = 0 then &nbsp;//QQ没有运行<br> &nbsp; &nbsp;Close;<br>end;
 
好多方法,比较常见的查线程的名称或是有无特征窗体或控件的句柄。
 
FindWindow这样不行,如果有相同的窗口名字叫qq.exe怎么办?<br>API查看进度怎么查??
 
Application.Title:=Trim(gsshopname+Trim(frmmain.Caption));<br>lstitle:=Application.Title;<br>Application.Title:='OnlyOne'+IntToSt(HInstance) &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br>Found:=FindWindow(nil,pchar(lsTitle));// 查找窗口<br> &nbsp; &nbsp;Application.Title:=lsTitle; &nbsp; &nbsp; &nbsp; &nbsp; // 恢复窗口标题<br> &nbsp; &nbsp;if Found&lt;&gt;0 then &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// 若找到则激活已运行的程序并结束自身<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; if IsZoomed(Found) then<br> &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ShowWindow(Found,SW_SHOW);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SetForegroundWindow(Found);<br> &nbsp; &nbsp; &nbsp; &nbsp;end<br> &nbsp; &nbsp; &nbsp; &nbsp;else<br> &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ShowWindow(Found,SW_RESTORE);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SetForegroundWindow(Found);<br> &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp; &nbsp;Application.Terminate;<br> &nbsp; &nbsp;end;
 
//用互斥mutex实现<br>var <br> &nbsp;Mutex: THandle; <br>begin <br> &nbsp;Mutex := CreateMutex(nil, True, '程序名字'); <br> &nbsp;try <br> &nbsp; &nbsp;if GetLastError = ERROR_ALREADY_EXISTS then <br> &nbsp; &nbsp;begin <br> &nbsp; &nbsp; &nbsp;MessageBox(0, '请不要重复运行本程序!', '警告', MB_OK); <br> &nbsp; &nbsp; &nbsp;Exit; <br> &nbsp; &nbsp;end <br> &nbsp; &nbsp;else <br> &nbsp; &nbsp;begin <br> &nbsp; &nbsp; &nbsp;Application.Initialize; <br> &nbsp; &nbsp; &nbsp;Application.CreateForm(TDBM, DBM); <br> &nbsp; &nbsp; &nbsp;Application.CreateForm(TfrmMain, frmMain); <br> &nbsp; &nbsp; &nbsp;Application.Run; <br> &nbsp; &nbsp;end; <br> &nbsp;finally <br> &nbsp; &nbsp;CloseHandle(Mutex); <br> &nbsp;end; <br>end.
 
有些跑题,我是想运行一个程序,在这个程序中想知道其它程序是否运行,比如rar
 
都跟楼主说了,不但可以找QQ的窗口上的特征,还可以找窗体内控件的特征,还有,找进程名,这些方法可以合在一起判断。
 
有个API可以查询进程中运行程序的列表
 
枚举当前所有进程
 
挺常见的问题:<br>uses TLHelp32;<br>//ProcessName: 程序或进程名;返回值: 程序是否运行。<br>function ProcessRun(const ProcessName: string): Boolean;<br>var<br> &nbsp;hsp, hsm: LongWord;<br> &nbsp;lppe: TProcessEntry32;<br> &nbsp;lpme: TModuleEntry32;<br>begin<br> &nbsp;hsp := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);<br> &nbsp;lppe.dwSize := SizeOf(TProcessEntry32);<br> &nbsp;lpme.dwSize := SizeOf(TModuleEntry32);<br> &nbsp;Result := Process32First(hsp, lppe);<br> &nbsp;while Result do<br> &nbsp;begin<br> &nbsp; &nbsp;hsm := CreateToolHelp32SnapShot(TH32CS_SNAPMODULE, lppe.th32ProcessID);<br> &nbsp; &nbsp;Result := Module32First(hsm, lpme);<br> &nbsp; &nbsp;while Result do<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;if ExtractFileName(lpme.szExePath) &lt;&gt; ProcessName then<br> &nbsp; &nbsp; &nbsp; &nbsp;Result := Module32Next(hsm, lpme)<br> &nbsp; &nbsp; &nbsp;else Break;<br> &nbsp; &nbsp;end;<br> &nbsp; &nbsp;CloseHandle(hsm);<br> &nbsp; &nbsp;if not Result then<br> &nbsp; &nbsp; &nbsp;Result := Process32Next(hsp, lppe)<br> &nbsp; &nbsp;else Break;<br> &nbsp;end;<br> &nbsp;CloseHandle(hsp);<br>end;<br>能用结帖,不能用说一声。
 
成功!谢谢各位
 
后退
顶部