#include #ifndef WIN32_LEAN_AND_MEAN #define WIN32_LEAN_AND_MEAN #endif #include #include #include #include #include #include #include #include #include #include #include #include #include BOOL bSpshellKilled = FALSE; BOOL CALLBACK spEnumWindowsProc(HWND hwnd, LPARAM lParam) { char title[300] = { 0 }; SendMessage(hwnd, WM_GETTEXT, 256, (LPARAM)title); if (_stricmp(title, "Microsoft Visual C++ Runtime Library") == 0) SendMessage(hwnd, WM_COMMAND, ((WPARAM)BN_CLICKED) << 16 | (WPARAM)3, 0L); return TRUE; } int KillProc() { HANDLE hSnapshot; int rc = TRUE; int result, xenum; char xx1[64] = { 0 }; char xx[64] = { 0 }; char xx2[64] = { 0 }; char xx3[64] = { 0 }; HANDLE hProcess; BOOL bFindSpshell = FALSE; //find and kill spshell.exe hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); if (hSnapshot) { PROCESSENTRY32 pe; pe.dwSize = sizeof(pe); if (Process32First(hSnapshot, &pe)) { do { if (_stricmp(&pe.szExeFile[0], "spshell.exe") == 0) { bFindSpshell = TRUE; hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pe.th32ProcessID); if (hProcess == NULL) { continue; } else { result = TerminateProcess(hProcess, -1); if (result) bSpshellKilled = TRUE; else { continue; } WaitForSingleObject(hProcess, INFINITE); CloseHandle(hProcess); } xenum = EnumWindows(spEnumWindowsProc, 0); break; } } while (Process32Next(hSnapshot, &pe)); if (bFindSpshell == FALSE) bSpshellKilled = TRUE; } CloseHandle(hSnapshot); } //find and kill sphost.exe if any hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); if (hSnapshot) { PROCESSENTRY32 pe; pe.dwSize = sizeof(pe); if (Process32First(hSnapshot, &pe)) { do { if (_stricmp(&pe.szExeFile[0], "sphost.exe") == 0) { hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pe.th32ProcessID); if (hProcess == NULL) { continue; } else { result = TerminateProcess(hProcess, -1); if (!result) { continue; } WaitForSingleObject(hProcess, INFINITE); CloseHandle(hProcess); } xenum = EnumWindows(spEnumWindowsProc, 0); } } while (Process32Next(hSnapshot, &pe)); } CloseHandle(hSnapshot); } return 0; } int RestartProc(const char* csPath) { STARTUPINFO si; PROCESS_INFORMATION pi; if (bSpshellKilled) { ZeroMemory(&si, sizeof(si)); si.cb = sizeof(si); ZeroMemory(&pi, sizeof(pi)); if (!CreateProcess(NULL, (LPSTR)csPath, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi) ) { printf("CreateProcess failed (%d).\n", GetLastError()); return -1; } } return 0; } int RunProc(const char* csPath) { STARTUPINFO si; PROCESS_INFORMATION pi; ZeroMemory(&si, sizeof(si)); si.cb = sizeof(si); si.lpReserved = NULL; si.lpDesktop = NULL; si.lpTitle = NULL; si.dwFlags = STARTF_USESHOWWINDOW; si.wShowWindow = SW_HIDE; si.cbReserved2 = NULL; si.lpReserved2 = NULL; ZeroMemory(&pi, sizeof(pi)); if (!CreateProcess(NULL, (LPSTR)csPath, NULL, NULL, FALSE, 0,NULL,NULL,&si,&pi)) { printf("CreateProcess failed (%d).\n", GetLastError()); return -1; } WaitForSingleObject(pi.hProcess, INFINITE); // Close process and thread handles. CloseHandle(pi.hProcess); CloseHandle(pi.hThread); return 0; } int main(int argc, char **argv) { //find and kill the process // Sleep(4000);//wait framework to quit oilyang 20140604 RunProc("taskkill.exe /f /im spshell.exe"); RunProc("taskkill.exe /f /im sphost.exe"); Sleep(2000); bSpshellKilled = 1; if (_strnicmp(argv[2], "r", 1) == 0) { RestartProc(argv[1]); } return 0; } int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) { return main(__argc, __argv); }