特殊功能,實現方法! Delphi / Windows SDK/API
http://www.delphi2007.net/DelphiAPI/html/delphi_20061118141925203.html
有沒有一種簡單的方法,可以監視一個exe檔案是否運行或死掉,一但發現
立即重啟此exe程式.改註冊表能不能實現???
附上我自己寫的一個按時間定時重啟exe的來源程式,大家研究一下;
http://www.sojibar.com/images/upfile/run2006-10-14.rar
順便幫我想個更好的方法,謝謝!
採用進程監視方法,建立該EXE進程後,會得到一個關於這個進程資訊的結構對象。然後調用GetExitCodeProcess,如果退出碼為STILL_ACTIVE表示進程還在運行,反之則進程已經關閉
woshihaoge 感謝!
是否有點複雜.
一般來說是用進程監視的方法!很多木馬或流氓軟體都是這麼做的!
<b> 能不能說具體一點 </b>
woshihaoge 感謝!
按你的方法,我實現了,不知道碰到程式假死,會不會生效。還沒有測試到。
代碼:
public
{ Public declarations }
SI : TStartupInfo;
PI : TProcessInformation;
form.show 加入:
FillChar(SI,sizeof(SI),#0);
with SI do
begin
cb:=sizeof(SI);
dwFlags:=StartF_UsesTDHandles or STARTF_USESHOWWINDOW;
lptitle:=nil;//pchar('gxtvodserver');
wShowWindow:=SW_Show;
end;
//Application.ProcessMessages;
CreateProcess(PChar(exefile),nil,nil,nil,true,IDLE_PRIORITY_CLASS,nil,nil,SI,PI);
//MyHandle:=OpenProcess(PROCESS_TERMINATE, true, pi.dwProcessId);
//開始監控
Timer1.Enabled:=TRUE;
procedure TForm1.Timer1Timer(Sender: TObject);
var
ExitCode:DWORD;
begin
//Handle:=OpenProcess(PROCESS_TERMINATE, true, PI.dwProcessId);
GetExitCodeProcess(PI.hProcess, ExitCode);
if not(ExitCode=STILL_ACTIVE) then //STILL_ACTIVE
begin
//showmessage('進程已經關閉');
Timer1.Enabled := FALSE ;
if chksetting() then
begin
RestartSvr();//start service
end;
end;
不會是做木馬吧
哈哈,你寫的代碼非常正確,給分拉。
其實最最笨的還有一個方法,就是在一個計時器控制項事件中,不斷用FindWindow函數檢測你啟動並執行程式是否存在,如果不存在了,可以用ShellExecute執行。
最後一搶!
FindWindow 不準確,回勿殺進程!