如何判断一个应用程序是否在运行? (急) ( 积分: 100 )

  • 主题发起人 主题发起人 CHINESEWHOAMI
  • 开始时间 开始时间
C

CHINESEWHOAMI

Unregistered / Unconfirmed
GUEST, unregistred user!
1.如何判断一个应用程序是否在运行?&nbsp;<br>2.一个应用程序怎么判断自身已经运行?
 
搜索一下DELPHIBBS
 
挺常见的问题:<br>uses&nbsp;TLHelp32;<br>//ProcessName:&nbsp;程序或进程名;返回值:&nbsp;程序是否运行。<br>function&nbsp;ProcessRun(const&nbsp;ProcessName:&nbsp;string):&nbsp;Boolean;<br>var<br>&nbsp;&nbsp;hsp,&nbsp;hsm:&nbsp;LongWord;<br>&nbsp;&nbsp;lppe:&nbsp;TProcessEntry32;<br>&nbsp;&nbsp;lpme:&nbsp;TModuleEntry32;<br>begin<br>&nbsp;&nbsp;hsp&nbsp;:=&nbsp;CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,&nbsp;0);<br>&nbsp;&nbsp;lppe.dwSize&nbsp;:=&nbsp;SizeOf(TProcessEntry32);<br>&nbsp;&nbsp;lpme.dwSize&nbsp;:=&nbsp;SizeOf(TModuleEntry32);<br>&nbsp;&nbsp;Result&nbsp;:=&nbsp;Process32First(hsp,&nbsp;lppe);<br>&nbsp;&nbsp;while&nbsp;Result&nbsp;do<br>&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;hsm&nbsp;:=&nbsp;CreateToolHelp32SnapShot(TH32CS_SNAPMODULE,&nbsp;lppe.th32ProcessID);<br>&nbsp;&nbsp;&nbsp;&nbsp;Result&nbsp;:=&nbsp;Module32First(hsm,&nbsp;lpme);<br>&nbsp;&nbsp;&nbsp;&nbsp;while&nbsp;Result&nbsp;do<br>&nbsp;&nbsp;&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;ExtractFileName(lpme.szExePath)&nbsp;&lt;&gt;&nbsp;ProcessName&nbsp;then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Result&nbsp;:=&nbsp;Module32Next(hsm,&nbsp;lpme)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else&nbsp;Break;<br>&nbsp;&nbsp;&nbsp;&nbsp;end;<br>&nbsp;&nbsp;&nbsp;&nbsp;CloseHandle(hsm);<br>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;not&nbsp;Result&nbsp;then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Result&nbsp;:=&nbsp;Process32Next(hsp,&nbsp;lppe)<br>&nbsp;&nbsp;&nbsp;&nbsp;else&nbsp;Break;<br>&nbsp;&nbsp;end;<br>&nbsp;&nbsp;CloseHandle(hsp);<br>end;
 
第一个问题:学习楼上。<br><br>第二个问题:<br>function&nbsp;AppIsRunning:&nbsp;Boolean;<br>const<br>&nbsp;&nbsp;STR_UNIQUE&nbsp;=&nbsp;'A5CE8FF5-A931-479A-8CB3-8338C59946FC';&nbsp;&nbsp;//这里是自定义的字符串,建议用&nbsp;GUID&nbsp;码<br>var<br>&nbsp;&nbsp;MutHandle:&nbsp;THandle;<br>begin<br>&nbsp;&nbsp;MutHandle&nbsp;:=&nbsp;OpenMutex(MUTEX_ALL_ACCESS,&nbsp;False,&nbsp;STR_UNIQUE);&nbsp;&nbsp;&nbsp;//打开互斥对象<br>&nbsp;&nbsp;Result&nbsp;:=&nbsp;(MutHandle&nbsp;=&nbsp;0);<br>&nbsp;&nbsp;if&nbsp;Result&nbsp;then&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;MutHandle&nbsp;:=&nbsp;CreateMutex(nil,&nbsp;False,&nbsp;STR_UNIQUE)&nbsp;&nbsp;&nbsp;&nbsp;//建立互斥对象<br>end;
 
findwindow(nil,'标题')
 
findwindow(nil,'标题')
 
原来楼主的成绩还“不错”<br>CHINESEWHOAMI&nbsp;有:&nbsp;27个待答问题,&nbsp;0个已答问题
 
program&nbsp;Doll;<br><br>uses<br>&nbsp;&nbsp;Windows,<br>&nbsp;&nbsp;Forms,<br>&nbsp;&nbsp;Unit1&nbsp;in&nbsp;'Unit1.pas'&nbsp;{Form1};<br><br>{$R&nbsp;*.res}<br>var<br>MutexHandle:Hwnd;<br>begin<br>&nbsp;&nbsp;Application.Initialize;<br>&nbsp;&nbsp;MutexHandle:=CreateMutex(nil,false,Pchar('MyMutex'));<br><br>&nbsp;&nbsp;if&nbsp;GetLastError&lt;&gt;ERROR_ALREADY_EXISTS&nbsp;then<br>&nbsp;&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Application.CreateForm(TForm1,&nbsp;Form1);<br>&nbsp;&nbsp;Application.Run;<br>&nbsp;&nbsp;&nbsp;end<br>&nbsp;&nbsp;else<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MessageBox(0,Pchar('程序已经运行'),'警告'&nbsp;,MB_OK&nbsp;+&nbsp;MB_ICONWARNING);<br>&nbsp;&nbsp;ReleaseMutex(MutexHandle);<br>end.
 
后退
顶部