sprestart.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #include <WinSock2.h>
  2. #ifndef WIN32_LEAN_AND_MEAN
  3. #define WIN32_LEAN_AND_MEAN
  4. #endif
  5. #include <Mmsystem.h>
  6. #include <windows.h>
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9. #include <assert.h>
  10. #include <process.h>
  11. #include <errno.h>
  12. #include <time.h>
  13. #include <string.h>
  14. #include <crtdbg.h>
  15. #include <locale.h>
  16. #include <time.h>
  17. #include <TlHelp32.h>
  18. BOOL bSpshellKilled = FALSE;
  19. BOOL CALLBACK spEnumWindowsProc(HWND hwnd, LPARAM lParam)
  20. {
  21. char title[300] = { 0 };
  22. SendMessage(hwnd, WM_GETTEXT, 256, (LPARAM)title);
  23. if (_stricmp(title, "Microsoft Visual C++ Runtime Library") == 0)
  24. SendMessage(hwnd, WM_COMMAND, ((WPARAM)BN_CLICKED) << 16 | (WPARAM)3, 0L);
  25. return TRUE;
  26. }
  27. int KillProc()
  28. {
  29. HANDLE hSnapshot;
  30. int rc = TRUE;
  31. int result, xenum;
  32. char xx1[64] = { 0 };
  33. char xx[64] = { 0 };
  34. char xx2[64] = { 0 };
  35. char xx3[64] = { 0 };
  36. HANDLE hProcess;
  37. BOOL bFindSpshell = FALSE;
  38. //find and kill spshell.exe
  39. hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  40. if (hSnapshot) {
  41. PROCESSENTRY32 pe;
  42. pe.dwSize = sizeof(pe);
  43. if (Process32First(hSnapshot, &pe)) {
  44. do {
  45. if (_stricmp(&pe.szExeFile[0], "spshell.exe") == 0) {
  46. bFindSpshell = TRUE;
  47. hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pe.th32ProcessID);
  48. if (hProcess == NULL) {
  49. continue;
  50. } else {
  51. result = TerminateProcess(hProcess, -1);
  52. if (result)
  53. bSpshellKilled = TRUE;
  54. else {
  55. continue;
  56. }
  57. WaitForSingleObject(hProcess, INFINITE);
  58. CloseHandle(hProcess);
  59. }
  60. xenum = EnumWindows(spEnumWindowsProc, 0);
  61. break;
  62. }
  63. } while (Process32Next(hSnapshot, &pe));
  64. if (bFindSpshell == FALSE)
  65. bSpshellKilled = TRUE;
  66. }
  67. CloseHandle(hSnapshot);
  68. }
  69. //find and kill sphost.exe if any
  70. hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  71. if (hSnapshot) {
  72. PROCESSENTRY32 pe;
  73. pe.dwSize = sizeof(pe);
  74. if (Process32First(hSnapshot, &pe)) {
  75. do {
  76. if (_stricmp(&pe.szExeFile[0], "sphost.exe") == 0) {
  77. hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pe.th32ProcessID);
  78. if (hProcess == NULL) {
  79. continue;
  80. } else {
  81. result = TerminateProcess(hProcess, -1);
  82. if (!result) {
  83. continue;
  84. }
  85. WaitForSingleObject(hProcess, INFINITE);
  86. CloseHandle(hProcess);
  87. }
  88. xenum = EnumWindows(spEnumWindowsProc, 0);
  89. }
  90. } while (Process32Next(hSnapshot, &pe));
  91. }
  92. CloseHandle(hSnapshot);
  93. }
  94. return 0;
  95. }
  96. int RestartProc(const char* csPath)
  97. {
  98. STARTUPINFO si;
  99. PROCESS_INFORMATION pi;
  100. if (bSpshellKilled) {
  101. ZeroMemory(&si, sizeof(si));
  102. si.cb = sizeof(si);
  103. ZeroMemory(&pi, sizeof(pi));
  104. if (!CreateProcess(NULL,
  105. (LPSTR)csPath,
  106. NULL,
  107. NULL,
  108. FALSE,
  109. 0,
  110. NULL,
  111. NULL,
  112. &si,
  113. &pi)
  114. ) {
  115. printf("CreateProcess failed (%d).\n", GetLastError());
  116. return -1;
  117. }
  118. }
  119. return 0;
  120. }
  121. int RunProc(const char* csPath)
  122. {
  123. STARTUPINFO si;
  124. PROCESS_INFORMATION pi;
  125. ZeroMemory(&si, sizeof(si));
  126. si.cb = sizeof(si);
  127. si.lpReserved = NULL;
  128. si.lpDesktop = NULL;
  129. si.lpTitle = NULL;
  130. si.dwFlags = STARTF_USESHOWWINDOW;
  131. si.wShowWindow = SW_HIDE;
  132. si.cbReserved2 = NULL;
  133. si.lpReserved2 = NULL;
  134. ZeroMemory(&pi, sizeof(pi));
  135. if (!CreateProcess(NULL, (LPSTR)csPath, NULL, NULL, FALSE, 0,NULL,NULL,&si,&pi))
  136. {
  137. printf("CreateProcess failed (%d).\n", GetLastError());
  138. return -1;
  139. }
  140. WaitForSingleObject(pi.hProcess, INFINITE);
  141. // Close process and thread handles.
  142. CloseHandle(pi.hProcess);
  143. CloseHandle(pi.hThread);
  144. return 0;
  145. }
  146. int main(int argc, char **argv)
  147. {
  148. //find and kill the process
  149. //
  150. Sleep(4000);//wait framework to quit oilyang 20140604
  151. RunProc("taskkill.exe /f /im spshell.exe");
  152. RunProc("taskkill.exe /f /im sphost.exe");
  153. Sleep(2000);
  154. bSpshellKilled = 1;
  155. if (_strnicmp(argv[2], "r", 1) == 0) {
  156. RestartProc(argv[1]);
  157. }
  158. return 0;
  159. }
  160. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
  161. {
  162. return main(__argc, __argv);
  163. }