123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- #include <WinSock2.h>
- #ifndef WIN32_LEAN_AND_MEAN
- #define WIN32_LEAN_AND_MEAN
- #endif
- #include <Mmsystem.h>
- #include <windows.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <assert.h>
- #include <process.h>
- #include <errno.h>
- #include <time.h>
- #include <string.h>
- #include <crtdbg.h>
- #include <locale.h>
- #include <time.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) {
- 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);
- }
|