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.

407 lines
9.1 KiB

  1. #define _WIN32_DCOM
  2. #define RCLENGTH 20
  3. #include <objbase.h>
  4. #include <winsvc.h>
  5. #include <tchar.h>
  6. #include <stdio.h>
  7. #include <wbemcli.h>
  8. #ifdef SAVE_ERIC
  9. HRESULT DelNamespace (IWbemLocator *pWbemLocator, BSTR bsParentNamespace, BSTR bsTargetNamespace)
  10. {
  11. HRESULT hres = 0;
  12. IWbemServices *pWbemServices = NULL;
  13. hres = pWbemLocator->ConnectServer(bsParentNamespace,
  14. NULL,
  15. NULL,
  16. NULL,
  17. 0L,
  18. NULL,
  19. NULL,
  20. &pWbemServices);
  21. if (FAILED(hres))
  22. {
  23. OutputDebugString("Could not connect to parent namespace.\n");
  24. return hres;
  25. }
  26. hres = pWbemServices->DeleteInstance(bsTargetNamespace,
  27. NULL,
  28. NULL,
  29. NULL);
  30. pWbemServices->Release();
  31. pWbemServices = NULL;
  32. return hres;
  33. }
  34. HRESULT clean()
  35. {
  36. OutputDebugString("clean function\n");
  37. HRESULT hres = CoInitialize(NULL);
  38. hres = CoInitializeSecurity(NULL, -1, NULL, NULL,
  39. RPC_C_AUTHN_LEVEL_CONNECT,
  40. RPC_C_IMP_LEVEL_IMPERSONATE,
  41. NULL, 0, 0);
  42. if (FAILED(hres))
  43. {
  44. OutputDebugString("CoInitializeSecurity Failed\n");
  45. return hres;
  46. }
  47. IWbemLocator *pIWbemLocator = NULL;
  48. hres = CoCreateInstance(CLSID_WbemLocator, NULL, CLSCTX_INPROC_SERVER,
  49. IID_IWbemLocator, (LPVOID *) &pIWbemLocator);
  50. if (FAILED(hres))
  51. {
  52. OutputDebugString("CoCreateInstance Failed\n");
  53. CoUninitialize();
  54. return hres;
  55. }
  56. BSTR bsParentNS = NULL;
  57. BSTR bsTargetNS = NULL;
  58. // Delete root\healthmon namespace if exists
  59. bsParentNS = SysAllocString (L"\\\\.\\root");
  60. bsTargetNS = SysAllocString (L"__namespace.name=\"HealthMon\"");
  61. hres = DelNamespace(pIWbemLocator, bsParentNS, bsTargetNS);
  62. SysFreeString(bsParentNS);
  63. SysFreeString(bsTargetNS);
  64. if (FAILED(hres))
  65. {
  66. OutputDebugString("Could not delete root\\healthmon\n");
  67. }
  68. // Delete root\cimv2\MicrosoftHealthMonitor namespace if exists
  69. bsParentNS = SysAllocString (L"\\\\.\\root\\cimv2");
  70. bsTargetNS = SysAllocString (L"__namespace.name=\"MicrosoftHealthMonitor\"");
  71. hres = DelNamespace(pIWbemLocator, bsParentNS, bsTargetNS);
  72. SysFreeString(bsParentNS);
  73. SysFreeString(bsTargetNS);
  74. if (FAILED(hres))
  75. {
  76. OutputDebugString("Could not delete root\\cimv2\\MicrosoftHealthMonitor\n");
  77. }
  78. pIWbemLocator->Release();
  79. pIWbemLocator = NULL;
  80. StopWMI();
  81. CoUninitialize();
  82. return TRUE;
  83. }
  84. #endif
  85. int upgrade()
  86. {
  87. OutputDebugString(L"upgrade function\n");
  88. return FALSE;
  89. }
  90. #ifdef SAVE_ERIC
  91. BOOL MofComp(LPDLLCALLPARAMS lpDllParams)
  92. {
  93. #ifdef DEBUG
  94. __asm int 3
  95. #endif
  96. HRESULT hres = S_OK;
  97. TCHAR *pszBuffer = NULL;
  98. pszBuffer = new TCHAR[MAX_STRING_LEN];
  99. // Set the startup structure
  100. //==========================
  101. if (strlen(lpDllParams->lpszParam) == 0)
  102. {
  103. OutputDebugString (_T("Invalid Parameter\r\n"));
  104. LoadString(hInstance, INVALID_PATH, pszBuffer, MAX_STRING_LEN);
  105. SetVariable(lpDllParams, ERROR_TEXT, pszBuffer);
  106. pszBuffer[0]=NULL;
  107. SetVariable(lpDllParams, ERROR_FACILITY, lpDllParams->lpszParam);
  108. delete[] pszBuffer;
  109. return FALSE;
  110. }
  111. IMofCompiler* pCompiler = NULL;
  112. hres = CoInitialize(NULL);
  113. if (FAILED(hres))
  114. {
  115. OutputDebugString (_T("CoInitialize failed\r\n"));
  116. SetRC (lpDllParams, hres);
  117. LoadString(hInstance, COINIT_FAILED, pszBuffer, MAX_STRING_LEN);
  118. SetVariable(lpDllParams, ERROR_TEXT, pszBuffer);
  119. pszBuffer[0]=NULL;
  120. LoadString(hInstance, FACILITY_DCOM, pszBuffer, MAX_STRING_LEN);
  121. SetVariable(lpDllParams, ERROR_FACILITY, pszBuffer);
  122. pszBuffer[0]=NULL;
  123. delete[] pszBuffer;
  124. return FALSE;
  125. }
  126. hres = CoCreateInstance(CLSID_MofCompiler,
  127. 0,
  128. CLSCTX_INPROC_SERVER,
  129. IID_IMofCompiler,
  130. (LPVOID *) &pCompiler);
  131. if (FAILED(hres))
  132. {
  133. OutputDebugString(_T("CoCreateInstance failed\r\n"));
  134. SetRC (lpDllParams, hres);
  135. LoadString(hInstance, CO_CREATE_INST_FAILED, pszBuffer, MAX_STRING_LEN);
  136. pszBuffer[0]=NULL;
  137. SetVariable(lpDllParams, ERROR_TEXT, pszBuffer);
  138. LoadString(hInstance, FACILITY_DCOM, pszBuffer, MAX_STRING_LEN);
  139. SetVariable(lpDllParams, ERROR_FACILITY, pszBuffer);
  140. pszBuffer[0]=NULL;
  141. CoUninitialize();
  142. delete[] pszBuffer;
  143. return FALSE;
  144. }
  145. WCHAR MofPath[MAX_PATH];
  146. MofPath[0]=0;
  147. int rc = 0;
  148. rc = MultiByteToWideChar(CP_ACP, MB_ERR_INVALID_CHARS,
  149. lpDllParams->lpszParam, strlen(lpDllParams->lpszParam),
  150. MofPath, MAX_PATH);
  151. MofPath[rc] = NULL; // NULL termanate string
  152. if (rc == 0)
  153. {
  154. hres = GetLastError();
  155. SetRC (lpDllParams, hres);
  156. LoadString(hInstance, CONVT_PATH_FAILED, pszBuffer, MAX_STRING_LEN);
  157. SetVariable(lpDllParams, ERROR_TEXT, pszBuffer);
  158. pszBuffer[0]=NULL;
  159. OutputDebugString(_T("could not convert path to WCHAR\r\n"));
  160. #ifdef _DEBUG
  161. if (hres == ERROR_INSUFFICIENT_BUFFER)
  162. OutputDebugString(_T("ERROR_INSUFFICIENT_BUFFER\r\n"));
  163. if (hres == ERROR_INVALID_FLAGS)
  164. OutputDebugString(_T("ERROR_INVALID_FLAGS\r\n"));
  165. if (hres == ERROR_INVALID_PARAMETER)
  166. OutputDebugString(_T("ERROR_INVALID_PARAMETER\r\n"));
  167. if (hres == ERROR_NO_UNICODE_TRANSLATION)
  168. OutputDebugString(_T("ERROR_NO_UNICODE_TRANSLATION\r\n"));
  169. #endif
  170. CoUninitialize();
  171. delete[] pszBuffer;
  172. return FALSE;
  173. }
  174. WBEM_COMPILE_STATUS_INFO info;
  175. char *OldWDir = NULL;
  176. char *NewWDir = NULL;
  177. OldWDir = new char[MAX_PATH*4];
  178. NewWDir = new char[MAX_PATH*4];
  179. GetVariable (lpDllParams, TEMP_DIR, NewWDir);
  180. _getcwd (OldWDir, MAX_PATH*4);
  181. if ( _chdir (NewWDir) )
  182. {
  183. GetVariable (lpDllParams, INSTALL_DIR, NewWDir);
  184. if ( _chdir (NewWDir) )
  185. {
  186. OutputDebugString(_T("could not set working dir\r\n"));
  187. _chdir (OldWDir);
  188. }
  189. }
  190. hres = pCompiler->CompileFile(MofPath, //FileName
  191. NULL, //ServerAndNamespace
  192. NULL, //Username
  193. NULL, //Authority
  194. NULL, //password
  195. NULL, //lOptionFlags---WBEM_FLAG_CONSOLE_PRINT output to a console window
  196. NULL, //lClassFlags
  197. NULL, //lInstanceFlags
  198. &info); //pWbem_Compile_Status_Info
  199. pCompiler->Release();
  200. _chdir (OldWDir);
  201. delete[] OldWDir;
  202. delete[] NewWDir;
  203. OldWDir = NULL;
  204. NewWDir = NULL;
  205. if (hres == S_OK)
  206. {
  207. OutputDebugString(_T("Compiled file\r\n"));
  208. CoUninitialize();
  209. delete[] pszBuffer;
  210. return TRUE;
  211. }
  212. SetRC(lpDllParams, info.hRes);
  213. char RCLoc[MAX_STRING_LEN];
  214. if (info.ObjectNum == 0)
  215. {
  216. LoadString(hInstance, PHASE_ONLY, pszBuffer, MAX_STRING_LEN);
  217. sprintf (RCLoc, pszBuffer, info.lPhaseError);
  218. }
  219. else
  220. {
  221. LoadString(hInstance, PHASE_OBJECT_COMBO, pszBuffer, MAX_STRING_LEN);
  222. sprintf (RCLoc, pszBuffer, info.lPhaseError, info.ObjectNum,
  223. info.FirstLine, info.LastLine);
  224. }
  225. SetVariable(lpDllParams, ERROR_LOCATION, RCLoc);
  226. #ifdef _DEBUG
  227. sprintf (RCLoc, "hres: %d\r\n", hres);
  228. OutputDebugString(RCLoc);
  229. #endif
  230. if ((info.hRes > S_OK) && (info.hRes < MAX_MOFCOMP_ERRORS)) // syntax error while parsing
  231. {
  232. LoadString(hInstance, SYNTAX_FILE_ERROR, pszBuffer, MAX_STRING_LEN);
  233. SetVariable(lpDllParams, ERROR_TEXT, pszBuffer);
  234. pszBuffer[0]=NULL;
  235. char pszFacilityText[MAX_STRING_LEN];
  236. char pszMofFile[MAX_STRING_LEN];
  237. GetVariable(lpDllParams, MOF_FILE_NAME, pszMofFile);
  238. LoadString(hInstance, MOF_FILE_SPRINTF_STRING, pszBuffer, MAX_STRING_LEN);
  239. sprintf(pszFacilityText, pszBuffer, pszMofFile);
  240. SetVariable(lpDllParams, ERROR_FACILITY, pszFacilityText);
  241. OutputDebugString(_T("syntax or file error\r\n"));
  242. CoUninitialize();
  243. delete[] pszBuffer;
  244. return FALSE;
  245. }
  246. // WMI error while putting information
  247. HRESULT hr = 0;
  248. IWbemStatusCodeText * pSCText = NULL;
  249. hr = CoCreateInstance(CLSID_WbemStatusCodeText, 0, CLSCTX_INPROC_SERVER,
  250. IID_IWbemStatusCodeText, (LPVOID *) &pSCText);
  251. if (!SUCCEEDED(hr))
  252. {
  253. OutputDebugString(_T("could not instanciate WbemStatusCodeText\r\n"));
  254. LoadString(hInstance, UNKNOWN_ERROR, pszBuffer, MAX_STRING_LEN);
  255. SetVariable(lpDllParams, ERROR_TEXT, pszBuffer);
  256. pszBuffer[0]=NULL;
  257. CoUninitialize();
  258. delete[] pszBuffer;
  259. return FALSE;
  260. }
  261. BSTR bstr = 0;
  262. hr = pSCText->GetErrorCodeText(info.hRes, 0, 0, &bstr);
  263. if(hr == S_OK)
  264. {
  265. int size = 0;
  266. size = wcstombs(NULL, bstr, SysStringLen(bstr));
  267. char* pszErrorText = new char[size+1];
  268. wcstombs(pszErrorText, bstr, SysStringLen(bstr));
  269. pszErrorText[size]=NULL;
  270. SetVariable(lpDllParams, ERROR_TEXT, pszErrorText);
  271. delete[] pszErrorText;
  272. SysFreeString(bstr);
  273. bstr = 0;
  274. }
  275. hr = pSCText->GetFacilityCodeText(info.hRes, 0, 0, &bstr);
  276. if(hr == S_OK)
  277. {
  278. int size = 0;
  279. size = wcstombs(NULL, bstr, SysStringLen(bstr));
  280. char* pszFacilityText = new char[size+1];
  281. wcstombs(pszFacilityText, bstr, SysStringLen(bstr));
  282. pszFacilityText[size]=NULL;
  283. SetVariable(lpDllParams, ERROR_FACILITY, pszFacilityText);
  284. delete[] pszFacilityText;
  285. SysFreeString(bstr);
  286. bstr = 0;
  287. }
  288. pSCText->Release();
  289. OutputDebugString(_T("end\r\n"));
  290. CoUninitialize();
  291. delete[] pszBuffer;
  292. return FALSE;
  293. }
  294. #endif
  295. int APIENTRY WinMain(HINSTANCE hInstance,
  296. HINSTANCE hPrevInstance,
  297. LPSTR lpCmdLine,
  298. int nCmdShow)
  299. {
  300. _strupr (lpCmdLine);
  301. #ifdef SAVE_ERIC
  302. if (strstr(lpCmdLine, "/CLEAN"))
  303. {
  304. OutputDebugString(L"clean command line\n");
  305. return clean();
  306. }
  307. if (strstr(lpCmdLine, "/UPGRADE"))
  308. {
  309. OutputDebugString("upgrade command line\n");
  310. // return upgrade();
  311. }
  312. #endif
  313. return FALSE;
  314. }