123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- #include "precompile.h"
- #include <TlHelp32.h>
- 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);
- }
|