咨詢個問題:
我有一個守護程式,watchDog.exe
主程式xviewer.exe通過udp每次向watchdog.exe發送一次心跳包,如果5秒內watchdog收不到xviewr的心跳包,有可能xviewer已經是死機狀態了,就去關閉xviewer,并重新啟動它
我是這么重啟動的
// 重啟動xviewer
WinExec("xViewer.exe", SW_NORMAL);
但是,如何去關閉呢?
uj5u.com熱心網友回復:
沒有人知道嗎?uj5u.com熱心網友回復:
WinExec("TASKKILL /F /IM QQ.exe", SW_NORMAL);uj5u.com熱心網友回復:
http://www.cnblogs.com/yangyh/archive/2011/03/09/1978975.htmluj5u.com熱心網友回復:
TerminateProcess(procStruct.hProcess,0);用這個陳述句結束行程,
uj5u.com熱心網友回復:
通程序式路徑找到程式句柄,然后發送關閉訊息
#include "iKillProgram.h" // 殺死指定行程;關閉外部程式;關閉指定行程
// #include "iKillProgram.cpp" // 殺死指定行程;關閉外部程式;關閉指定行程
#ifndef _iKillProgram_cpp_
#define _iKillProgram_cpp_
#include "iGetModuleFileNameEx.h" // 列舉(列出)所有系統行程
BOOL CALLBACK EnumWinProc(HWND hwnd, LPARAM lParam)
{
DWORD dwID;
GetWindowThreadProcessId(hwnd, &dwID);
if(dwID == (DWORD)lParam)
{
PostMessage(hwnd, WM_QUIT, 0, 0); // 只回傳是否成功發送了訊息,而不管結果是成功還是失敗
//SendMessage(hwnd,WM_CLOSE,NULL,NULL); // 能收到處理后的結果是成功還是失敗
return FALSE; // 此處回傳值必須是大寫的FALE,改成TRUE等關閉程式就不好用了?
}
return TRUE;
}
//#include <tlhelp32.h>
int KillProgram( // 關閉指定全路徑的程式
// 要關閉程式的全路徑
String ExeName
){
int T_SQKH=0;
if(L"" == ExeName.Trim())
{
return T_SQKH;
}
HANDLE snapshot;
PROCESSENTRY32 processinfo; // #include <tlhelp32.h>
processinfo.dwSize = sizeof (processinfo);
snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
if(NULL == snapshot)
{
return T_SQKH;
}
bool status = Process32First(snapshot,&processinfo);
while(status)
{
//TListItem *li = ListView1->Items->Add();
String buffer;
int length;
buffer.SetLength(512);
//length = sprintf(buffer.w_str(),L"%08X", processinfo.th32ProcessID);
length = swprintf(buffer.w_str(),L"%08X", processinfo.th32ProcessID);
buffer.SetLength(length);
//li->Caption = buffer;
buffer.SetLength(512);
length = swprintf(buffer.w_str(),L"%08X",processinfo.th32ParentProcessID);
buffer.SetLength(length);
if(ExeName.LowerCase()==ProcessNameAndID(processinfo.th32ProcessID).LowerCase())
{
bool T_GKTQ=EnumWindows((WNDENUMPROC)EnumWinProc,processinfo.th32ProcessID);
T_SQKH=T_SQKH+((T_GKTQ == FALSE) ? 1 : 0);
}
status = Process32Next(snapshot,&processinfo);
}
return T_SQKH;
}
int KillProgramFiles( // 關閉指定目錄中運行的除自身外的所有程式
// 要關閉的程式所在的目錄
String FilePath
,
// 當前程式的全路徑,當主程式在FilePath目錄中時用次引數避免被關閉
String T_BCQY
){
int T_NPJY=0;
if(L"" == FilePath.Trim())
{
return T_NPJY;
}
if(L"\\" != FilePath.SubString( FilePath.Length(),1) )
{
FilePath = FilePath+L"\\"; // 如果Path最后一個字符不是“\”就在后面加上“\”
}
HANDLE snapshot;
PROCESSENTRY32 processinfo; // #include <tlhelp32.h>
processinfo.dwSize = sizeof (processinfo);
snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
if(NULL == snapshot)
{
return T_NPJY;
}
bool status = Process32First(snapshot,&processinfo);
while(status)
{
//TListItem *li = ListView1->Items->Add();
String buffer;
int length;
buffer.SetLength(512);
//length = sprintf(buffer.w_str(),L"%08X", processinfo.th32ProcessID);
length = swprintf(buffer.w_str(),L"%08X", processinfo.th32ProcessID);
buffer.SetLength(length);
//li->Caption = buffer;
buffer.SetLength(512);
length = swprintf(buffer.w_str(),L"%08X",processinfo.th32ParentProcessID);
buffer.SetLength(length);
if(
(ProcessNameAndID(processinfo.th32ProcessID).LowerCase()).Pos(FilePath.LowerCase())>0
){
if(
T_BCQY.LowerCase()!=ProcessNameAndID(processinfo.th32ProcessID).LowerCase()
){
bool T_FSHI=EnumWindows((WNDENUMPROC)EnumWinProc,processinfo.th32ProcessID);
T_NPJY=T_NPJY+StrToInt(((T_FSHI == FALSE) ? 1 : 0));
}
}
status = Process32Next(snapshot,&processinfo);
}
return T_NPJY;
}
#endif
/*// 使用方法:
void __fastcall TForm1::Button1Click(TObject *Sender)
{ // 關閉指定全路徑的程式
//KillProgram(L"R:\\NOTEPAD.EXE"); // 關閉程式
//KillProgram(ExpandFileName(Application->ExeName).w_str()); // 關閉程式自身
KillProgram(ExpandFileName(Application->ExeName)); // 關閉程式自身
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{ // 關閉指定目錄中運行的除自身外的所有程式
int T_APEZ=KillProgramFiles( // 關閉指定目錄中運行的除自身外的所有程式
// 要關閉的程式所在的目錄
ExtractFilePath(ExpandFileName(Application->ExeName))
,
// 當前程式的全路徑,當主程式在FilePath目錄中時用此引數避免被關閉
ExpandFileName(Application->ExeName)
);
ShowMessage(IntToStr(T_APEZ));
}
*/
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/95796.html
