bool KillProcess(wstring wstrExename) { bool bOK = false; WCHAR wszCmd[2048]; int nSize = 160; PROCESS_INFORMATION pi; STARTUPINFOW si; ZeroMemory(&si, sizeof(STARTUPINFOW)); ZeroMemory(&pi, sizeof(PROCESS_INFORMATION)); si.cb = sizeof(STARTUPINFOW); si.dwFlags = STARTF_USESHOWWINDOW; si.wShowWindow = SW_HIDE; swprintf(wszCmd, L"taskkill /F /IM %s", wstrExename.c_str()); if (CreateProcessW(NULL, wszCmd, NULL, NULL, FALSE, CREATE_NEW_CONSOLE|IDLE_PRIORITY_CLASS, NULL, NULL, &si, &pi)) { int nKillWaitTime = 30000; int nRet = WaitForSingleObject(pi.hProcess, nKillWaitTime); switch (nRet) { case WAIT_TIMEOUT: // Time out break; case WAIT_OBJECT_0: // Process over break; case WAIT_OBJECT_0 + 1: // Don't Know break; } CloseHandle(pi.hProcess); pi.hProcess = NULL; CloseHandle(pi.hThread); pi.hThread = NULL; bOK = true; } return bOK; }
2012年1月9日 星期一
[C++] Kill Process
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言