如何判定某个程序是否已经运行?(100分)

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

fengyuyang

Unregistered / Unconfirmed
GUEST, unregistred user!
有一个程序,在服务器上运行,有时会自动关闭,我想做一个程序,进行监控,如发现这个程序没有运行则立刻运行该程序。
 
if findwindow('TApplication','Test')&lt;&gt;0 then Halt;<br>&nbsp; //'Test' is Application.Title <br>
 
if findwindow(nil,'caption') = 0 then winexec('exefile');<br><br>The FindWindow function retrieves the handle to the top-level window whose class name and window name match the specified strings. This function does not search child windows. <br><br>HWND FindWindow(<br><br>&nbsp; &nbsp; LPCTSTR lpClassName, // pointer to class name<br>&nbsp; &nbsp; LPCTSTR lpWindowName // pointer to window name<br>&nbsp; &nbsp;); <br>&nbsp;<br><br>Parameters<br><br>lpClassName<br><br>Points to a null-terminated string that specifies the class name or is an atom that identifies the class-name string. If this parameter is an atom, it must be a global atom created by a previous call to the GlobalAddAtom function. The atom, a 16-bit value, must be placed in the low-order word of lpClassName; the high-order word must be zero. <br><br>lpWindowName<br><br>Points to a null-terminated string that specifies the window name (the window's title). If this parameter is NULL, all window names match. <br><br>&nbsp;<br><br>Return Values<br><br>If the function succeeds, the return value is the handle to the window that has the specified class name and window name.<br>If the function fails, the return value is NULL. To get extended error information, call GetLastError.
 
unit UnitMain;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br>&nbsp; Dialogs,tlhelp32, ExtCtrls, StdCtrls;<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; Timer1: TTimer;<br>&nbsp; &nbsp; Edit1: TEdit;<br>&nbsp; &nbsp; Label10: TLabel;<br>&nbsp; &nbsp; Label1: TLabel;<br>&nbsp; &nbsp; Label3: TLabel;<br>&nbsp; &nbsp; Label4: TLabel;<br>&nbsp; &nbsp; Label31: TLabel;<br>&nbsp; &nbsp; Label32: TLabel;<br>&nbsp; &nbsp; Label5: TLabel;<br>&nbsp; &nbsp; Label8: TLabel;<br>&nbsp; &nbsp; Label13: TLabel;<br>&nbsp; &nbsp; Label2: TLabel;<br>&nbsp; &nbsp; Label6: TLabel;<br>&nbsp; &nbsp; Label7: TLabel;<br>&nbsp; &nbsp; GroupBox1: TGroupBox;<br>&nbsp; &nbsp; Edit2: TEdit;<br>&nbsp; &nbsp; Edit3: TEdit;<br>&nbsp; &nbsp; Label9: TLabel;<br>&nbsp; &nbsp; Label11: TLabel;<br>&nbsp; &nbsp; procedure FormClose(Sender: TObject; var Action: TCloseAction);<br>&nbsp; &nbsp; procedure Timer1Timer(Sender: TObject);<br>&nbsp; private<br>&nbsp; procedure WMMsg(var message:TMessage);Message wm_user;<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br>&nbsp; procedure InstallDll(path:string;MainFormHandle,ExplorerProcessID:THandle;num,lSAddress,lEAddress:Cardinal);stdcall;external 'install.dll';<br>&nbsp; procedure RemoveDll;stdcall;external 'install.dll';<br>var<br>&nbsp; Form1: TForm1;<br><br>implementation<br><br>{$R *.dfm}<br><br>function FindProcessName:THandle;<br>var<br>&nbsp; lppe: tprocessentry32;<br>&nbsp; sshandle: thandle;<br>&nbsp; found: boolean;<br>begin<br>&nbsp; result:=0;<br>&nbsp; sshandle := createtoolhelp32snapshot(TH32CS_SNAPALL, 0);<br>&nbsp; found := process32first(sshandle, lppe);<br>&nbsp; while found do<br>&nbsp; begin<br>// &nbsp; &nbsp;if ansiCompareText(ExtractFileName(lppe.szExefile),'EXPLORER.EXE') = 0 then<br>&nbsp; &nbsp; if ansiCompareText(ExtractFileName(lppe.szExefile),'QQ.exe') = 0 then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp;result:=lppe.th32ProcessID;<br><br>&nbsp; &nbsp; &nbsp; &nbsp;break;<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; found := process32next(sshandle, lppe); {检索下一个进程}<br>&nbsp; end;<br>&nbsp; CloseHandle(sshandle);<br>end;<br><br>procedure TForm1.Timer1Timer(Sender: TObject);<br>var<br>&nbsp; &nbsp;h: THandle;<br>&nbsp; &nbsp;num: Cardinal;<br>&nbsp; &nbsp;lSAddress,lEAddress:Cardinal;<br>begin<br>&nbsp; h:=FindProcessName;<br>&nbsp; if h=0 then begin<br>ShellExecute(0,'open','C:/Program Files/QQ/QQ.exe',nil,nil,sw_hide); &nbsp;end;<br>&nbsp; end;<br>end;<br><br><br>end.
 
To:jianguobu<br>我跟踪测试了你的代码,<br>found := process32first(sshandle, lppe);<br>执行后,found 的值始终是 false ,无法进入 while 循环。
 
楼主,以下是我刚才调试通过了的<br><br><br>unit UnitMain;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br>&nbsp; Dialogs,tlhelp32, ExtCtrls, StdCtrls, shellapi;<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; Timer1: TTimer;<br>&nbsp; &nbsp; Edit1: TEdit;<br>&nbsp; &nbsp; Label10: TLabel;<br>&nbsp; &nbsp; Label1: TLabel;<br>&nbsp; &nbsp; Label3: TLabel;<br>&nbsp; &nbsp; Label4: TLabel;<br>&nbsp; &nbsp; Label31: TLabel;<br>&nbsp; &nbsp; Label32: TLabel;<br>&nbsp; &nbsp; Label5: TLabel;<br>&nbsp; &nbsp; Label8: TLabel;<br>&nbsp; &nbsp; Label13: TLabel;<br>&nbsp; &nbsp; Label2: TLabel;<br>&nbsp; &nbsp; Label6: TLabel;<br>&nbsp; &nbsp; Label7: TLabel;<br>&nbsp; &nbsp; GroupBox1: TGroupBox;<br>&nbsp; &nbsp; Edit2: TEdit;<br>&nbsp; &nbsp; Edit3: TEdit;<br>&nbsp; &nbsp; Label9: TLabel;<br>&nbsp; &nbsp; Label11: TLabel;<br>&nbsp; &nbsp; procedure FormClose(Sender: TObject; var Action: TCloseAction);<br>&nbsp; &nbsp; procedure Timer1Timer(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br>&nbsp;var<br>&nbsp; Form1: TForm1;<br><br>implementation<br><br>{$R *.dfm}<br><br>function FindProcessName:THandle;<br>var<br>&nbsp; lppe: tprocessentry32;<br>&nbsp; sshandle: thandle;<br>&nbsp; found: boolean;<br>begin<br>&nbsp; result:=0;<br>&nbsp; sshandle := createtoolhelp32snapshot(TH32CS_SNAPALL, 0);<br>&nbsp; found := process32first(sshandle, lppe);<br>&nbsp; while found do<br>&nbsp; begin<br>&nbsp; &nbsp; if ansiCompareText(ExtractFileName(lppe.szExefile),'QQ.exe') = 0 then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp;result:=lppe.th32ProcessID;<br>&nbsp; &nbsp; &nbsp; &nbsp;break;<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; found := process32next(sshandle, lppe); {检索下一个进程}<br>&nbsp; end;<br>&nbsp; CloseHandle(sshandle);<br>end;<br><br>procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);<br>begin<br>&nbsp; &nbsp;RemoveDll;<br>end;<br><br>procedure TForm1.Timer1Timer(Sender: TObject);<br>var<br>&nbsp; &nbsp;h: THandle;<br>begin<br>&nbsp; h:=FindProcessName;<br>&nbsp; if h=0 then begin<br>&nbsp; &nbsp; ShellExecute(0,'open','E:/BJGE/Program Files/Tencent/QQ.exe',nil,nil,sw_hide);<br>// &nbsp; &nbsp; &nbsp;winexec('E:/BJGE/Program Files/Tencent/QQ.exe',sw_normal);<br>&nbsp; end;<br>end;<br><br><br>end.
 
To:jianguobu<br>我刚才测试的时候没有加<br>procedure InstallDll<br>procedure RemoveDll<br>这两个过程的代码,这次加上后,运行程序报错没有找到install.dll,我在我的电脑上搜索了一遍也没有找到这个文件。
 
用一个计时器判断,是否有这个程序在运行,没在,就重新创建 一个进程。
 
你把它删掉,在这里确实是没用的.你复制下面那贴
 
To:jianguobu<br>请帮忙看看我的代码有问题吗?<br>unit SAPPrint;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br>&nbsp; Dialogs, StdCtrls, tlhelp32, ExtCtrls;<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; Button1: TButton;<br>&nbsp; &nbsp; procedure Button1Click(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><br>{$R *.dfm}<br><br>function FindProcessName:THandle;<br>var<br>&nbsp; lppe: tprocessentry32;<br>&nbsp; sshandle: thandle;<br>&nbsp; found: boolean;<br>begin<br>&nbsp; result:=0;<br>&nbsp; sshandle := createtoolhelp32snapshot(TH32CS_SNAPALL, 0);<br>&nbsp; found := process32first(sshandle, lppe);<br>始终等于false,无法进入下面的循环<br>&nbsp; while found do<br>&nbsp; begin<br>&nbsp; &nbsp; if ansiCompareText(ExtractFileName(lppe.szExefile),'QQ.exe') = 0 then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp;result:=lppe.th32ProcessID;<br>&nbsp; &nbsp; &nbsp; &nbsp;break;<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; found := process32next(sshandle, lppe); {&amp;frac14;ì&amp;Euml;÷&amp;Iuml;&amp;Acirc;&amp;Ograve;&amp;raquo;&amp;cedil;&amp;ouml;&amp;frac12;&amp;oslash;&amp;sup3;&amp;Igrave;}<br>&nbsp; end;<br>&nbsp; CloseHandle(sshandle);<br>end;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; h: THandle;<br>begin<br>&nbsp; h:=FindProcessName;<br>&nbsp; if h=0 then<br>// &nbsp; &nbsp;ShellExecute(0,'open','C:/Program Files/QQ/QQ.exe',nil,nil,sw_hide);<br>另外,上面这句编译时无法通过,Undeclared identifier: 'ShellExecute' &nbsp; &nbsp;<br>&nbsp; &nbsp; &nbsp;application.MessageBox('NO','NO')<br>&nbsp; else<br>&nbsp; &nbsp; application.MessageBox('YES','YES');<br>end;<br><br>end.
 
用已个OpenMutex来创建一个互斥对象,通过返回参数来判断是否已经有一个实例在运行。
 
你要uses一下,shellapi单元<br><br>你别单步调试,我都测试通过了的.
 
给你个例子吧。这个好使!<br><br>procedure TFStart.FormCreate(Sender: TObject);<br>var<br>errNo:integer;<br>hMutex:HWND;<br>begin<br>&nbsp; &nbsp;hMutex:=CreateMutex(nil,False,pchar(application.title));<br>&nbsp; &nbsp;errNo:=GetLastError;<br>&nbsp; &nbsp;if errNo=ERROR_ALREADY_EXISTS Then<br>&nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp;application.MessageBox('System is in runing','Information',MB_OK);<br>&nbsp; &nbsp; &nbsp;application.Terminate;<br>&nbsp; &nbsp;end;<br><br>end;
 
To:jianguobu<br>&nbsp; 兄弟,我执行了可是结果不对,我的QQ明明开着,可程序却返回“NO”,即h=0。
 
你加我QQ:16970995
 
接受答案了.
 
整理后的<br><br>unit UnitMain;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br>&nbsp; Dialogs,tlhelp32, ExtCtrls, StdCtrls, shellapi;<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; Timer1: TTimer;<br>&nbsp; &nbsp; Edit1: TEdit;<br>&nbsp; &nbsp; procedure Timer1Timer(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br>&nbsp;var<br>&nbsp; Form1: TForm1;<br><br>implementation<br><br>{$R *.dfm}<br><br>function FindProcessName:THandle;<br>var<br>&nbsp; lppe: tprocessentry32;<br>&nbsp; sshandle: thandle;<br>&nbsp; found: boolean;<br>begin<br>&nbsp; result:=0;<br>&nbsp; sshandle := createtoolhelp32snapshot(TH32CS_SNAPALL, 0);<br>&nbsp; found := process32first(sshandle, lppe);<br>&nbsp; while found do<br>&nbsp; begin<br>&nbsp; &nbsp; if ansiCompareText(ExtractFileName(lppe.szExefile),'QQ.exe') = 0 then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp;result:=lppe.th32ProcessID;<br>&nbsp; &nbsp; &nbsp; &nbsp;break;<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; found := process32next(sshandle, lppe); {检索下一个进程}<br>&nbsp; end;<br>&nbsp; CloseHandle(sshandle);<br>end;<br><br>procedure TForm1.Timer1Timer(Sender: TObject);<br>var<br>&nbsp; &nbsp;h: THandle;<br>&nbsp; &nbsp;str :string;<br>begin<br>&nbsp; h:=FindProcessName;<br>&nbsp; if h=0 then begin<br>&nbsp; &nbsp; str :=edit1.Text;<br>&nbsp; &nbsp; ShellExecute(0,'open',pchar(str),nil,nil,sw_hide);<br>// &nbsp; &nbsp; &nbsp;winexec('E:/BJGE/Program Files/Tencent/QQ.exe',sw_normal);<br>&nbsp; end;<br>end;<br><br><br>end.
 
后退
顶部