我正在尝试查找正在运行的进程并从我的应用程序中删除它。 以下是我的代码。
bool ProcessKill()
{
PROCESSENTRY32 process;
Logger::getInstance()->test(L" checking the call kill status");
ZeroMemory(&process, sizeof(process));
process.dwSize = sizeof(PROCESSENTRY32);
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
if( snapshot == INVALID_HANDLE_VALUE )
{
Logger::getInstance()->test( TEXT(" CreateToolhelp32Snapshot (of processes)") );
return( FALSE );
}
Logger::getInstance()->test(L" checking the call kill status 2222");
if (Process32First(snapshot, & process) != FALSE)
{
Logger::getInstance()->test(L"checking the kill status");
while(Process32Next(snapshot, &process) == TRUE)
{ Logger::getInstance()->test(L"1111");
if (wcsicmp(process.szExeFile, L"kill.exe") == 0)
{
Logger::getInstance()->test(L"found kill running");
HANDLE hProcess_Name = OpenProcess(PROCESS_ALL_ACCESS, FALSE, process.th32ProcessID);
DWORD dwRet = ::WaitForSingleObject(hProcess_Name, 5000);
if (dwRet == WAIT_OBJECT_0)
dwRet = ERROR_SUCCESS;
//Logger::getInstance()->test(L"kill is running");
else
{
::TerminateProcess(hProcess_Name, 1);
::CloseHandle(hProcess_Name);
Logger::getInstance()->test(L"kill is terminated");
CloseHandle(snapshot);
return 1;
}
}
}
}
else
{
Logger::getInstance()->test(L" Process32First the call kill status 333");
}
CloseHandle(snapshot);
return 0;
}
我观察到Process32First失败了它返回false。 任何人都可以让我知道失败背后的原因以及如何解决这个问题。任何解决方法。
答案 0 :(得分:0)
在if (Process32First(snapshot, & process) != FALSE)
之前添加波纹管语句:
pe32.dwSize = sizeof(PROCESSENTRY32W);
如MSDN中所述:
tagPROCESSENTRY32
结构:
typedef struct tagPROCESSENTRY32 {
DWORD dwSize;
DWORD cntUsage;
DWORD th32ProcessID;
ULONG_PTR th32DefaultHeapID;
DWORD th32ModuleID;
DWORD cntThreads;
DWORD th32ParentProcessID;
LONG pcPriClassBase;
DWORD dwFlags;
CHAR szExeFile[MAX_PATH];
} PROCESSENTRY32;
dwSize
在调用Process32First
函数之前,将此成员设置为sizeof(PROCESSENTRY32)
。如果不初始化dwSize
,则Process32First
失败。