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.

321 lines
8.8 KiB

  1. //***************************************************************************
  2. //
  3. // GLOBAL.CPP
  4. //
  5. // Module: HEALTHMON SERVER AGENT
  6. //
  7. // Purpose: Global helper functions, defines, macros...
  8. //
  9. // Copyright (c)1999 Microsoft Corporation, All Rights Reserved
  10. //
  11. //***************************************************************************
  12. #include "global.h"
  13. IWbemServices* g_pIWbemServices = NULL;
  14. IWbemServices* g_pIWbemServicesCIMV2 = NULL;
  15. //XXXSet the following as static vaiables of each of their repecive classes,
  16. //and provide a Set function for it.
  17. IWbemObjectSink* g_pSystemEventSink = NULL;
  18. IWbemObjectSink* g_pDataGroupEventSink = NULL;
  19. IWbemObjectSink* g_pDataCollectorEventSink = NULL;
  20. IWbemObjectSink* g_pDataCollectorPerInstanceEventSink = NULL;
  21. IWbemObjectSink* g_pDataCollectorStatisticsEventSink = NULL;
  22. IWbemObjectSink* g_pThresholdEventSink = NULL;
  23. //IWbemObjectSink* g_pThresholdInstanceEventSink = NULL;
  24. IWbemObjectSink* g_pActionEventSink = NULL;
  25. IWbemObjectSink* g_pActionTriggerEventSink = NULL;
  26. //LPTSTR state[] = {L"OK",L"COLLECTING",L"RESET",L"INFO",L"DISABLED",L"SCHEDULEDOUT",L"RESERVED1",L"RESERVED2",L"WARNING",L"CRITICAL"};
  27. //LPTSTR condition[] = {L"<",L">",L"=",L"!=",L">=",L"<=",L"contains",L"!contains",L"always"};
  28. LPTSTR stateLocStr[10];
  29. LPTSTR conditionLocStr[9];
  30. void ClearLocStrings(void);
  31. // Set these at startup
  32. HRESULT SetLocStrings(void)
  33. {
  34. int i;
  35. TCHAR szTemp[1024];
  36. HRESULT hRetRes = S_OK;
  37. for (i=0; i<10; i++)
  38. {
  39. stateLocStr[i] = NULL;
  40. }
  41. for (i=0; i<9; i++)
  42. {
  43. conditionLocStr[i] = NULL;
  44. }
  45. if (g_hResLib == NULL || !LoadString(g_hResLib, HMRES_OK, szTemp, 1024))
  46. {
  47. wcscpy(szTemp, L"OK");
  48. }
  49. stateLocStr[0] = new TCHAR[wcslen(szTemp)+1];
  50. MY_ASSERT(stateLocStr[0]); if (!stateLocStr[0]) {hRetRes = WBEM_E_OUT_OF_MEMORY; goto error;}
  51. wcscpy(stateLocStr[0], szTemp);
  52. if (g_hResLib == NULL || !LoadString(g_hResLib, HMRES_COLLECTING, szTemp, 1024))
  53. {
  54. wcscpy(szTemp, L"COLLECTING");
  55. }
  56. stateLocStr[1] = new TCHAR[wcslen(szTemp)+1];
  57. MY_ASSERT(stateLocStr[1]); if (!stateLocStr[1]) {hRetRes = WBEM_E_OUT_OF_MEMORY; goto error;}
  58. wcscpy(stateLocStr[1], szTemp);
  59. if (g_hResLib == NULL || !LoadString(g_hResLib, HMRES_RESET, szTemp, 1024))
  60. {
  61. wcscpy(szTemp, L"RESET");
  62. }
  63. stateLocStr[2] = new TCHAR[wcslen(szTemp)+1];
  64. MY_ASSERT(stateLocStr[2]); if (!stateLocStr[2]) {hRetRes = WBEM_E_OUT_OF_MEMORY; goto error;}
  65. wcscpy(stateLocStr[2], szTemp);
  66. if (g_hResLib == NULL || !LoadString(g_hResLib, HMRES_INFO, szTemp, 1024))
  67. {
  68. wcscpy(szTemp, L"INFO");
  69. }
  70. stateLocStr[3] = new TCHAR[wcslen(szTemp)+1];
  71. MY_ASSERT(stateLocStr[3]); if (!stateLocStr[3]) {hRetRes = WBEM_E_OUT_OF_MEMORY; goto error;}
  72. wcscpy(stateLocStr[3], szTemp);
  73. if (g_hResLib == NULL || !LoadString(g_hResLib, HMRES_DISABLED, szTemp, 1024))
  74. {
  75. wcscpy(szTemp, L"DISABLED");
  76. }
  77. stateLocStr[4] = new TCHAR[wcslen(szTemp)+1];
  78. MY_ASSERT(stateLocStr[4]); if (!stateLocStr[4]) {hRetRes = WBEM_E_OUT_OF_MEMORY; goto error;}
  79. wcscpy(stateLocStr[4], szTemp);
  80. if (g_hResLib == NULL || !LoadString(g_hResLib, HMRES_SCHEDULEDOUT, szTemp, 1024))
  81. {
  82. wcscpy(szTemp, L"SCHEDULEDOUT");
  83. }
  84. stateLocStr[5] = new TCHAR[wcslen(szTemp)+1];
  85. MY_ASSERT(stateLocStr[5]); if (!stateLocStr[5]) {hRetRes = WBEM_E_OUT_OF_MEMORY; goto error;}
  86. wcscpy(stateLocStr[5], szTemp);
  87. if (g_hResLib == NULL || !LoadString(g_hResLib, HMRES_RESERVED1, szTemp, 1024))
  88. {
  89. wcscpy(szTemp, L"RESERVED1");
  90. }
  91. stateLocStr[6] = new TCHAR[wcslen(szTemp)+1];
  92. MY_ASSERT(stateLocStr[6]); if (!stateLocStr[6]) {hRetRes = WBEM_E_OUT_OF_MEMORY; goto error;}
  93. wcscpy(stateLocStr[6], szTemp);
  94. if (g_hResLib == NULL || !LoadString(g_hResLib, HMRES_RESERVED2, szTemp, 1024))
  95. {
  96. wcscpy(szTemp, L"RESERVED2");
  97. }
  98. stateLocStr[7] = new TCHAR[wcslen(szTemp)+1];
  99. MY_ASSERT(stateLocStr[7]); if (!stateLocStr[7]) {hRetRes = WBEM_E_OUT_OF_MEMORY; goto error;}
  100. wcscpy(stateLocStr[7], szTemp);
  101. if (g_hResLib == NULL || !LoadString(g_hResLib, HMRES_WARNING, szTemp, 1024))
  102. {
  103. wcscpy(szTemp, L"WARNING");
  104. }
  105. stateLocStr[8] = new TCHAR[wcslen(szTemp)+1];
  106. MY_ASSERT(stateLocStr[8]); if (!stateLocStr[8]) {hRetRes = WBEM_E_OUT_OF_MEMORY; goto error;}
  107. wcscpy(stateLocStr[8], szTemp);
  108. if (g_hResLib == NULL || !LoadString(g_hResLib, HMRES_CRITICAL, szTemp, 1024))
  109. {
  110. wcscpy(szTemp, L"CRITICAL");
  111. }
  112. stateLocStr[9] = new TCHAR[wcslen(szTemp)+1];
  113. MY_ASSERT(stateLocStr[9]); if (!stateLocStr[9]) {hRetRes = WBEM_E_OUT_OF_MEMORY; goto error;}
  114. wcscpy(stateLocStr[9], szTemp);
  115. //
  116. if (g_hResLib == NULL || !LoadString(g_hResLib, HMRES_LT, szTemp, 1024))
  117. {
  118. wcscpy(szTemp, L"<");
  119. }
  120. conditionLocStr[0] = new TCHAR[wcslen(szTemp)+1];
  121. MY_ASSERT(conditionLocStr[0]); if (!conditionLocStr[0]) {hRetRes = WBEM_E_OUT_OF_MEMORY; goto error;}
  122. wcscpy(conditionLocStr[0], szTemp);
  123. if (g_hResLib == NULL || !LoadString(g_hResLib, HMRES_GT, szTemp, 1024))
  124. {
  125. wcscpy(szTemp, L">");
  126. }
  127. conditionLocStr[1] = new TCHAR[wcslen(szTemp)+1];
  128. MY_ASSERT(conditionLocStr[1]); if (!conditionLocStr[1]) {hRetRes = WBEM_E_OUT_OF_MEMORY; goto error;}
  129. wcscpy(conditionLocStr[1], szTemp);
  130. if (g_hResLib == NULL || !LoadString(g_hResLib, HMRES_EQ, szTemp, 1024))
  131. {
  132. wcscpy(szTemp, L"=");
  133. }
  134. conditionLocStr[2] = new TCHAR[wcslen(szTemp)+1];
  135. MY_ASSERT(conditionLocStr[2]); if (!conditionLocStr[2]) {hRetRes = WBEM_E_OUT_OF_MEMORY; goto error;}
  136. wcscpy(conditionLocStr[2], szTemp);
  137. if (g_hResLib == NULL || !LoadString(g_hResLib, HMRES_NE, szTemp, 1024))
  138. {
  139. wcscpy(szTemp, L"!=");
  140. }
  141. conditionLocStr[3] = new TCHAR[wcslen(szTemp)+1];
  142. MY_ASSERT(conditionLocStr[3]); if (!conditionLocStr[3]) {hRetRes = WBEM_E_OUT_OF_MEMORY; goto error;}
  143. wcscpy(conditionLocStr[3], szTemp);
  144. if (g_hResLib == NULL || !LoadString(g_hResLib, HMRES_GTEQ, szTemp, 1024))
  145. {
  146. wcscpy(szTemp, L">=");
  147. }
  148. conditionLocStr[4] = new TCHAR[wcslen(szTemp)+1];
  149. MY_ASSERT(conditionLocStr[4]); if (!conditionLocStr[4]) {hRetRes = WBEM_E_OUT_OF_MEMORY; goto error;}
  150. wcscpy(conditionLocStr[4], szTemp);
  151. if (g_hResLib == NULL || !LoadString(g_hResLib, HMRES_LTEQ, szTemp, 1024))
  152. {
  153. wcscpy(szTemp, L"<=");
  154. }
  155. conditionLocStr[5] = new TCHAR[wcslen(szTemp)+1];
  156. MY_ASSERT(conditionLocStr[5]); if (!conditionLocStr[5]) {hRetRes = WBEM_E_OUT_OF_MEMORY; goto error;}
  157. wcscpy(conditionLocStr[5], szTemp);
  158. if (g_hResLib == NULL || !LoadString(g_hResLib, HMRES_CONTAINS, szTemp, 1024))
  159. {
  160. wcscpy(szTemp, L"contains");
  161. }
  162. conditionLocStr[6] = new TCHAR[wcslen(szTemp)+1];
  163. MY_ASSERT(conditionLocStr[6]); if (!conditionLocStr[6]) {hRetRes = WBEM_E_OUT_OF_MEMORY; goto error;}
  164. wcscpy(conditionLocStr[6], szTemp);
  165. if (g_hResLib == NULL || !LoadString(g_hResLib, HMRES_NOTCONTAINS, szTemp, 1024))
  166. {
  167. wcscpy(szTemp, L"!contains");
  168. }
  169. conditionLocStr[7] = new TCHAR[wcslen(szTemp)+1];
  170. MY_ASSERT(conditionLocStr[7]); if (!conditionLocStr[7]) {hRetRes = WBEM_E_OUT_OF_MEMORY; goto error;}
  171. wcscpy(conditionLocStr[7], szTemp);
  172. if (g_hResLib == NULL || !LoadString(g_hResLib, HMRES_ALWAYS, szTemp, 1024))
  173. {
  174. wcscpy(szTemp, L"always");
  175. }
  176. conditionLocStr[8] = new TCHAR[wcslen(szTemp)+1];
  177. MY_ASSERT(conditionLocStr[8]); if (!conditionLocStr[8]) {hRetRes = WBEM_E_OUT_OF_MEMORY; goto error;}
  178. wcscpy(conditionLocStr[8], szTemp);
  179. return S_OK;
  180. error:
  181. MY_ASSERT(FALSE);
  182. ClearLocStrings();
  183. return hRetRes;
  184. }
  185. void ClearLocStrings(void)
  186. {
  187. int i;
  188. for (i=0; i<10; i++)
  189. {
  190. delete [] stateLocStr[i];
  191. }
  192. for (i=0; i<9; i++)
  193. {
  194. delete [] conditionLocStr[i];
  195. }
  196. }
  197. BOOL ReadUI64(LPCWSTR wsz, UNALIGNED unsigned __int64& rui64)
  198. {
  199. unsigned __int64 ui64 = 0;
  200. const WCHAR* pwc = wsz;
  201. // Check for a NULL pointer
  202. if ( NULL == wsz )
  203. {
  204. return FALSE;
  205. }
  206. while(ui64 < 0xFFFFFFFFFFFFFFFF / 8 && *pwc >= L'0' && *pwc <= L'9')
  207. {
  208. unsigned __int64 ui64old = ui64;
  209. ui64 = ui64 * 10 + (*pwc - L'0');
  210. if(ui64 < ui64old)
  211. return FALSE;
  212. pwc++;
  213. }
  214. if(*pwc)
  215. {
  216. return FALSE;
  217. }
  218. rui64 = ui64;
  219. return TRUE;
  220. }
  221. HRESULT ReplaceStr(LPTSTR *pszString, LPTSTR pszOld, LPTSTR pszNew)
  222. {
  223. HRESULT hRetRes = S_OK;
  224. BSTR bstrPropName = NULL;
  225. TCHAR szQuery[4096];
  226. LPTSTR pStr;
  227. LPTSTR pStr2;
  228. LPTSTR pszNewStr;
  229. MY_ASSERT(pszOld);
  230. if (!pszOld)
  231. {
  232. return WBEM_E_INVALID_PROPERTY;
  233. }
  234. MY_ASSERT(pszNew);
  235. if (!pszNew)
  236. {
  237. return WBEM_E_INVALID_PROPERTY;
  238. }
  239. MY_ASSERT(*pszString);
  240. if (!*pszString)
  241. {
  242. return WBEM_E_INVALID_PROPERTY;
  243. }
  244. wcsncpy(szQuery, *pszString, 4095);
  245. szQuery[4095] = '\0';
  246. _wcsupr(szQuery);
  247. pStr = wcsstr(szQuery, pszOld);
  248. if (!pStr)
  249. {
  250. return S_OK;
  251. }
  252. pszNewStr = new TCHAR[wcslen(*pszString)+wcslen(pszNew)-wcslen(pszOld)+1];
  253. MY_ASSERT(pszNewStr);
  254. if (!pszNewStr)
  255. {
  256. hRetRes = WBEM_E_OUT_OF_MEMORY;
  257. }
  258. else
  259. {
  260. pStr2 = *pszString;
  261. pStr2 += pStr-szQuery;
  262. *pStr2 = '\0';
  263. wcscpy(pszNewStr, *pszString);
  264. wcscat(pszNewStr, pszNew);
  265. pStr2 += wcslen(pszOld);
  266. wcscat(pszNewStr, pStr2);
  267. }
  268. if (hRetRes == S_OK)
  269. {
  270. delete [] *pszString;
  271. *pszString = pszNewStr;
  272. }
  273. return hRetRes;
  274. }