Leaked source code of windows server 2003
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.

214 lines
4.8 KiB

  1. // ReportEvent.cpp
  2. #include "precomp.h"
  3. #include "ReportEvent.h"
  4. CReportEventMap::~CReportEventMap()
  5. {
  6. for (CReportEventMapIterator i = begin(); i != end(); i++)
  7. delete ((*i).second);
  8. }
  9. CIMTYPE CReportEventMap::PrintfTypeToCimType(LPCWSTR szType)
  10. {
  11. CIMTYPE type = 0;
  12. LPWSTR szArray = wcsstr(szType, L"[]");
  13. // Look to see if this should be an array.
  14. if (szArray)
  15. {
  16. type = CIM_FLAG_ARRAY;
  17. *szArray = 0;
  18. }
  19. // See if the remainder of the string is only a single character.
  20. if (*szType && !*(szType + 1))
  21. {
  22. // Set the type for the single character cases.
  23. switch(*szType)
  24. {
  25. case 'u':
  26. type |= CIM_UINT32;
  27. break;
  28. case 'd':
  29. case 'i':
  30. type |= CIM_SINT32;
  31. break;
  32. case 'f':
  33. type |= CIM_REAL32;
  34. break;
  35. case 'g':
  36. type |= CIM_REAL64;
  37. break;
  38. case 's':
  39. type |= CIM_STRING;
  40. break;
  41. case 'c':
  42. type |= CIM_UINT8;
  43. break;
  44. case 'w':
  45. type |= CIM_UINT16;
  46. break;
  47. case 'b':
  48. type |= CIM_BOOLEAN;
  49. break;
  50. case 'o':
  51. type |= CIM_OBJECT;
  52. break;
  53. case 'O':
  54. type |= CIM_IUNKNOWN;
  55. break;
  56. default:
  57. type = CIM_EMPTY;
  58. break;
  59. }
  60. }
  61. // Else check for the more complicated cases.
  62. else if (!wcscmp(szType, L"I64d") || !wcscmp(szType, L"I64i"))
  63. type |= CIM_SINT64;
  64. else if (!wcscmp(szType, L"I64u"))
  65. type |= CIM_UINT64;
  66. else
  67. type = CIM_EMPTY;
  68. return type;
  69. }
  70. HANDLE CReportEventMap::CreateEvent(
  71. HANDLE hConnection,
  72. LPCWSTR szName,
  73. DWORD dwFlags,
  74. LPCWSTR szFormat)
  75. {
  76. if ( szName == NULL )
  77. return NULL;
  78. LPWSTR szTempFormat = _wcsdup(szFormat);
  79. HANDLE hEvent;
  80. // Out of memory?
  81. if (!szTempFormat)
  82. return NULL;
  83. hEvent =
  84. WmiCreateObject(
  85. hConnection,
  86. szName,
  87. dwFlags);
  88. if (hEvent == NULL)
  89. {
  90. free(szTempFormat);
  91. return NULL;
  92. }
  93. LPWSTR szCurrent = wcstok(szTempFormat, L" ");
  94. BOOL bBad = FALSE;
  95. while (szCurrent && !bBad)
  96. {
  97. LPWSTR szType = wcschr(szCurrent, '!'),
  98. szBang2;
  99. bBad = TRUE;
  100. if (szType)
  101. {
  102. szBang2 = wcschr(szType + 1, '!');
  103. if (szBang2)
  104. {
  105. *szBang2 = 0;
  106. *szType = 0;
  107. szType++;
  108. CIMTYPE type = PrintfTypeToCimType(szType);
  109. if (type != CIM_EMPTY)
  110. {
  111. bBad =
  112. !WmiAddObjectProp(
  113. hEvent,
  114. szCurrent,
  115. type,
  116. NULL);
  117. }
  118. }
  119. }
  120. szCurrent = wcstok(NULL, L" ");
  121. }
  122. if (bBad && hEvent)
  123. {
  124. // Something went wrong, so blow away the event and return NULL.
  125. WmiDestroyObject(hEvent);
  126. hEvent = NULL;
  127. }
  128. free(szTempFormat);
  129. return hEvent;
  130. }
  131. HANDLE CReportEventMap::GetEvent(
  132. HANDLE hConnection,
  133. LPCWSTR szName,
  134. LPCWSTR szFormat)
  135. {
  136. HANDLE hEvent;
  137. CReportParams params(szName, szFormat);
  138. CReportEventMapIterator i;
  139. // First find a match using the pointers, then verify it's a real match
  140. // by using string compares.
  141. if ((i = find(params)) != end())
  142. {
  143. // If it's a match, return the event we already have.
  144. if (params.IsEquivalent((*i).first))
  145. return (*i).second->GetEvent();
  146. else
  147. {
  148. // Was not a match, so free up the mapping.
  149. delete ((*i).second);
  150. erase(i);
  151. }
  152. }
  153. hEvent =
  154. CreateEvent(
  155. hConnection,
  156. szName,
  157. 0,
  158. szFormat);
  159. if (hEvent)
  160. {
  161. // If everything was OK then we need to store this event in our
  162. // map.
  163. CReportItem *pItem = new CReportItem( );
  164. if (pItem)
  165. {
  166. if ( pItem->Initialize( szName, szFormat, hEvent) )
  167. {
  168. (*this)[params] = pItem;
  169. return hEvent;
  170. }
  171. }
  172. WmiDestroyObject(hEvent);
  173. return NULL;
  174. }
  175. return hEvent;
  176. }