Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

257 lines
5.2 KiB

  1. //#define _WIN32_DCOM
  2. #define RCLENGTH 50
  3. #include <objbase.h>
  4. #include <winsvc.h>
  5. #include <tchar.h>
  6. #include <stdio.h>
  7. #include <wbemcli.h>
  8. int StartWMI()
  9. {
  10. OutputDebugString (L"StartWMI\n");
  11. int bFailed = TRUE;
  12. DWORD dwRC = NULL;
  13. SC_HANDLE hSvcCtrlMgrDB = OpenSCManager ( NULL , NULL , GENERIC_READ ) ;
  14. if ( hSvcCtrlMgrDB )
  15. {
  16. SC_HANDLE hService = OpenService ( hSvcCtrlMgrDB , L"Windows Management Instrumentation" , SERVICE_START ) ;
  17. if ( hService )
  18. {
  19. BOOL RC = StartService ( hService , 0 , NULL ) ;
  20. if ( RC )
  21. {
  22. bFailed = FALSE;
  23. OutputDebugString (L"Service Started\n");
  24. }
  25. else
  26. {
  27. dwRC = GetLastError();
  28. TCHAR RCText[RCLENGTH];
  29. swprintf(RCText, L"0x%d", dwRC);
  30. OutputDebugString(L"Service did not start. Error code: ");
  31. OutputDebugString(RCText);
  32. OutputDebugString(L"\r\n");
  33. }
  34. }
  35. else
  36. {
  37. dwRC = GetLastError();
  38. TCHAR RCText[RCLENGTH];
  39. swprintf(RCText, L"0x%d", dwRC);
  40. OutputDebugString(L"Could not get handle to service. Error code: ");
  41. OutputDebugString(RCText);
  42. OutputDebugString(L"\r\n");
  43. }
  44. }
  45. else
  46. {
  47. dwRC = GetLastError();
  48. TCHAR RCText[RCLENGTH];
  49. swprintf(RCText, L"0x%d", dwRC);
  50. OutputDebugString(L"Could not get handle to SCM. Error code: ");
  51. OutputDebugString(RCText);
  52. OutputDebugString(L"\r\n");
  53. }
  54. return bFailed;
  55. }
  56. int StopWMI()
  57. {
  58. OutputDebugString (L"StopWMI\n");
  59. int bFailed = TRUE;
  60. DWORD dwRC = NULL;
  61. SC_HANDLE hSvcCtrlMgrDB = OpenSCManager (NULL, NULL, GENERIC_READ);
  62. if ( hSvcCtrlMgrDB )
  63. {
  64. SC_HANDLE hService = OpenService (hSvcCtrlMgrDB, L"winmgmt", SERVICE_STOP);
  65. if ( hService )
  66. {
  67. SERVICE_STATUS lpServiceStatus;
  68. BOOL RC = ControlService (hService, SERVICE_CONTROL_STOP, &lpServiceStatus);
  69. if ( RC )
  70. {
  71. bFailed = FALSE;
  72. OutputDebugString (L"Service Stopped\n");
  73. }
  74. else
  75. {
  76. dwRC = GetLastError();
  77. TCHAR RCText[RCLENGTH];
  78. swprintf(RCText, L"0x%d", dwRC);
  79. OutputDebugString(L"Service did not be stopped. Error code: ");
  80. OutputDebugString(RCText);
  81. OutputDebugString(L"\r\n");
  82. }
  83. }
  84. else
  85. {
  86. dwRC = GetLastError();
  87. TCHAR RCText[RCLENGTH];
  88. swprintf(RCText, L"0x%d", dwRC);
  89. OutputDebugString(L"Could not get handle to service. Error code: ");
  90. OutputDebugString(RCText);
  91. OutputDebugString(L"\r\n");
  92. }
  93. }
  94. else
  95. {
  96. dwRC = GetLastError();
  97. TCHAR RCText[RCLENGTH];
  98. swprintf(RCText, L"0x%d", dwRC);
  99. OutputDebugString(L"Could not get handle to SCM. Error code: ");
  100. OutputDebugString(RCText);
  101. OutputDebugString(L"\r\n");
  102. }
  103. return bFailed;
  104. }
  105. HRESULT DelNamespace (IWbemLocator *pWbemLocator, BSTR bsParentNamespace, BSTR bsTargetNamespace)
  106. {
  107. HRESULT hres = 0;
  108. IWbemServices *pWbemServices = NULL;
  109. hres = pWbemLocator->ConnectServer(bsParentNamespace,
  110. NULL,
  111. NULL,
  112. NULL,
  113. 0L,
  114. NULL,
  115. NULL,
  116. &pWbemServices);
  117. if (FAILED(hres))
  118. {
  119. OutputDebugString(L"Could not connect to parent namespace.\n");
  120. return hres;
  121. }
  122. hres = pWbemServices->DeleteInstance(bsTargetNamespace,
  123. NULL,
  124. NULL,
  125. NULL);
  126. pWbemServices->Release();
  127. pWbemServices = NULL;
  128. return hres;
  129. }
  130. HRESULT clean()
  131. {
  132. OutputDebugString(L"clean function\n");
  133. HRESULT hres = CoInitialize(NULL);
  134. hres = CoInitializeSecurity(NULL, -1, NULL, NULL,
  135. RPC_C_AUTHN_LEVEL_CONNECT,
  136. RPC_C_IMP_LEVEL_IMPERSONATE,
  137. NULL, 0, 0);
  138. if (FAILED(hres))
  139. {
  140. OutputDebugString(L"CoInitializeSecurity Failed\n");
  141. return hres;
  142. }
  143. IWbemLocator *pIWbemLocator = NULL;
  144. hres = CoCreateInstance(CLSID_WbemLocator, NULL, CLSCTX_INPROC_SERVER,
  145. IID_IWbemLocator, (LPVOID *) &pIWbemLocator);
  146. if (FAILED(hres))
  147. {
  148. OutputDebugString(L"CoCreateInstance Failed\n");
  149. CoUninitialize();
  150. return hres;
  151. }
  152. BSTR bsParentNS = NULL;
  153. BSTR bsTargetNS = NULL;
  154. // Delete root\healthmon namespace if exists
  155. bsParentNS = SysAllocString (L"\\\\.\\root");
  156. bsTargetNS = SysAllocString (L"__namespace.name=\"HealthMon\"");
  157. hres = DelNamespace(pIWbemLocator, bsParentNS, bsTargetNS);
  158. SysFreeString(bsParentNS);
  159. SysFreeString(bsTargetNS);
  160. if (FAILED(hres))
  161. {
  162. OutputDebugString(L"Could not delete root\\healthmon\n");
  163. }
  164. // Delete root\cimv2\MicrosoftHealthMonitor namespace if exists
  165. bsParentNS = SysAllocString (L"\\\\.\\root\\cimv2");
  166. bsTargetNS = SysAllocString (L"__namespace.name=\"MicrosoftHealthMonitor\"");
  167. hres = DelNamespace(pIWbemLocator, bsParentNS, bsTargetNS);
  168. SysFreeString(bsParentNS);
  169. SysFreeString(bsTargetNS);
  170. if (FAILED(hres))
  171. {
  172. OutputDebugString(L"Could not delete root\\cimv2\\MicrosoftHealthMonitor\n");
  173. }
  174. pIWbemLocator->Release();
  175. pIWbemLocator = NULL;
  176. StopWMI();
  177. CoUninitialize();
  178. return TRUE;
  179. }
  180. int upgrade()
  181. {
  182. OutputDebugString(L"upgrade function\n");
  183. return FALSE;
  184. }
  185. int APIENTRY WinMain(HINSTANCE hInstance,
  186. HINSTANCE hPrevInstance,
  187. LPSTR lpCmdLine,
  188. int nCmdShow)
  189. {
  190. #ifdef SAVE
  191. _strupr (lpCmdLine);
  192. if (wcsstr(lpCmdLine, L"/CLEAN"))
  193. {
  194. OutputDebugString(L"clean command line\n");
  195. return clean();
  196. }
  197. if (wcsstr(lpCmdLine, L"/UPGRADE"))
  198. {
  199. OutputDebugString(L"upgrade command line\n");
  200. return upgrade();
  201. }
  202. return FALSE;
  203. #endif
  204. return clean();
  205. }