sprestart.c 3.9 KB

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