#include "precompile.h" #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 ) { //MessageBoxA(0,"Fail to open process!",0,0); //MessageBoxA(0,_itoa((int)GetLastError(),xx3,10),0,0); //return -1; continue; } else { result = TerminateProcess(hProcess,-1); if (result) bSpshellKilled = TRUE; else { //MessageBoxA(0,"Terminate False",0,0); //MessageBoxA(0,_itoa((int)GetLastError(),xx,10),0,0); //return -1; 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 ) { //MessageBoxA(0,"Fail to open process(2)!",0,0); //MessageBoxA(0,_itoa((int)GetLastError(),xx3,10),0,0); //return -1; continue; } else { result = TerminateProcess(hProcess,-1); if (!result) { //MessageBoxA(0,"False(2)",0,0); //MessageBoxA(0,_itoa((int)GetLastError(),xx,10),0,0); //return -1; 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) { //MessageBoxA(0,(LPCSTR)csPath,0,0); 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 main(int argc, char **argv) { //find and kill the process // int retKill; Sleep(4000);//wait framework to quit oilyang 20140604 WinExec("cmd.exe /c taskkill /f /im spshell.exe", SW_HIDE); WinExec("cmd.exe /c taskkill /f /im sphost.exe", SW_HIDE); Sleep(2000); // system("taskkill /f /im spshell.exe"); // system("taskkill /f /im sphost.exe"); bSpshellKilled = 1; /* retKill = KillProc();*/ //if have killed spshell.exe,restart it //MessageBoxA(0,(LPCSTR)argv[0],0,0); //MessageBoxA(0,(LPCSTR)argv[1],0,0); //MessageBoxA(0,(LPCSTR)argv[2],0,0); 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); }