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.

306 lines
8.0 KiB

  1. // Event.h
  2. // These classes represent the hEvent returned by the CreateEvent functions.
  3. #pragma once
  4. // Forward declarations
  5. class CConnection;
  6. class CEvent;
  7. #include "array.h"
  8. #include "NCObjApi.h"
  9. /////////////////////////////////////////////////////////////////////////////
  10. // CPropInfo
  11. typedef BOOL (CEvent::*PROP_FUNC)();
  12. typedef CArray<int, int> CIntArray;
  13. class CPropInfo
  14. {
  15. public:
  16. DWORD m_dwCurrentSize;
  17. DWORD m_dwElementSize;
  18. PROP_FUNC m_pFunc;
  19. BOOL Init(CIMTYPE type);
  20. BOOL CountPrefixed() { return m_bCountPrefixNeeded; }
  21. BOOL IsPointer()
  22. {
  23. return m_bPointer;
  24. }
  25. void InitCurrentSize(LPBYTE pData);
  26. protected:
  27. BOOL m_bCountPrefixNeeded;
  28. BOOL m_bPointer;
  29. };
  30. /////////////////////////////////////////////////////////////////////////////
  31. // CEventWrap
  32. typedef CArray<CPropInfo, CPropInfo&> CPropInfoArray;
  33. class CEventWrap
  34. {
  35. public:
  36. CEventWrap(CSink *pSink, DWORD dwFlags);
  37. CEventWrap(CEvent *pEvent, int nIndexes, DWORD *pdwIndexes);
  38. ~CEventWrap();
  39. BOOL IsSubset() { return !m_bFreeEvent; }
  40. CEvent *GetEvent() { return m_pEvent; }
  41. CIntArray *GetIndexArray() { return !IsSubset() ? NULL : &m_pIndexes; }
  42. int SubIndexToEventIndex(int iIndex)
  43. {
  44. if (!IsSubset())
  45. return iIndex;
  46. else
  47. {
  48. if(iIndex < 0 || iIndex >= m_pIndexes.GetSize())
  49. {
  50. _ASSERT(FALSE);
  51. return -1;
  52. }
  53. return m_pIndexes[iIndex];
  54. }
  55. }
  56. protected:
  57. CEvent *m_pEvent;
  58. CIntArray m_pIndexes;
  59. BOOL m_bFreeEvent;
  60. };
  61. /////////////////////////////////////////////////////////////////////////////
  62. // CEvent
  63. // LenStr:
  64. // DWORD nBytes - Bytes in the string.
  65. // WCHAR[nBytes] String data.
  66. // BYTE[0-3] Padding to make the string DWORD aligned.
  67. // Event Layout Buffer:
  68. // DWORD NC_SRVMSG_EVENT_LAYOUT (msg type)
  69. // DWORD dwMsgBytes - The total number of bytes in the buffer.
  70. // DWORD dwEventIndex
  71. // DWORD dwSinkIndex
  72. // DWORD nProperties
  73. // LenStr szEventClassName
  74. // The next two properties are repeated for each property.
  75. // DWORD dwPropType (Uses CIMTYPE values)
  76. // LenStr szPropertyName
  77. // Event Data Buffer:
  78. // DWORD NC_SRVMSG_PREPPED_EVENT (msg type)
  79. // DWORD dwMsgBytes - The total number of bytes in the buffer.
  80. // DWORD dwEventIndex
  81. // DWORD[n] cNullMask (0 bit == null)
  82. // n = # of props divided by 32. If no props, n == 1.
  83. // DWORD[nProps] dwDataInfo
  84. // This contains actual data for scalar values for types that
  85. // fit into 32-bits and offsets the to data for everything else.
  86. // Offsets are relative from the start of the buffer.
  87. // BYTE[???] The data pointed to by dwDataInfo (if necessary).
  88. // Event SD:
  89. // DWORD NC_SRVMSG_SET_EVENT_SD
  90. // DWORD dwMsgBytes - The total number of bytes in the buffer.
  91. // DWORD dwEventIndex
  92. // BYTE[] SD data
  93. // Data encoding (total length is always DWORD aligned):
  94. // Strings:
  95. // All strings, both alone and in arrays, are encoded as LenStr's.
  96. //
  97. // Arrays:
  98. // DWORD dwItems - Number of elements in the array.
  99. // Type[dwItems] array data
  100. // BYTE[0-3] Padding to make the data end on a DWORD boundary.
  101. //
  102. // Objects:
  103. // DWORD dwBytes - Number of bytes of object data.
  104. // BYTE[dwBytes] Layout Buffer + Data Buffer
  105. // BYTE[0-3] Padding to make the data end on a DWORD boundary.
  106. //
  107. // Blob Event Layout:
  108. // DWORD NC_SRVMSG_BLOB_EVENT
  109. // DWORD dwMsgBytes - The total number of bytes in the buffer.
  110. // DWORD dwSinkIndex
  111. // LenStr szEventName
  112. // DWORD dwSize - Size of blob.
  113. // BYTE[dwSize] pBlob
  114. class CEvent : public CBuffer
  115. {
  116. public:
  117. CRITICAL_SECTION m_cs;
  118. CSink *m_pSink;
  119. CEvent(CSink *pSink, DWORD dwFlags);
  120. ~CEvent();
  121. void ResetEvent();
  122. // Prepared event functions
  123. BOOL PrepareEvent(
  124. LPCWSTR szEventName,
  125. DWORD nPropertyCount,
  126. LPCWSTR *pszPropertyNames,
  127. CIMTYPE *pPropertyTypes);
  128. BOOL FindProp(LPCWSTR szName, CIMTYPE* ptype, DWORD* pdwIndex);
  129. BOOL AddProp(LPCWSTR szName, CIMTYPE type, DWORD *pdwIndex);
  130. BOOL SetPropValues(CIntArray *pArr, va_list list);
  131. BOOL SetSinglePropValue(DWORD dwIndex, va_list list);
  132. BOOL SetPropValue(DWORD dwPropIndex, LPVOID pData, DWORD dwElements,
  133. DWORD dwSize);
  134. BOOL GetPropValue(DWORD dwPropIndex, LPVOID pData, DWORD dwBufferSize,
  135. DWORD *pdwBytesRead);
  136. BOOL SetPropNull(DWORD dwPropIndex);
  137. void ResetLayoutSent() { m_bLayoutSent = FALSE; }
  138. CBuffer *GetLayout() { return &m_bufferEventLayout; }
  139. CPropInfo *GetProp(DWORD dwIndex) { return &m_pProps[dwIndex]; }
  140. BOOL SendEvent();
  141. friend CPropInfo; // For CPropInfo::Init.
  142. friend CEventWrap;
  143. LPCWSTR GetClassName()
  144. {
  145. return (LPCWSTR) (m_bufferEventLayout.m_pBuffer + sizeof(DWORD) * 6);
  146. }
  147. BOOL IsEnabled()
  148. {
  149. BOOL bEnabled;
  150. bEnabled =
  151. m_bEnabled ||
  152. (m_pSink->GetConnection() &&
  153. m_pSink->GetConnection()->WaitingForWMIInit());
  154. return bEnabled;
  155. }
  156. void SetEnabled(BOOL bEnabled) { m_bEnabled = bEnabled; }
  157. void GetLayoutBuffer(
  158. LPBYTE *ppBuffer,
  159. DWORD *pdwSize,
  160. BOOL bIncludeHeader);
  161. void GetDataBuffer(
  162. LPBYTE *ppBuffer,
  163. DWORD *pdwSize,
  164. BOOL bIncludeHeader);
  165. BOOL SetLayoutAndDataBuffers(
  166. LPBYTE pLayoutBuffer,
  167. DWORD dwLayoutBufferSize,
  168. LPBYTE pDataBuffer,
  169. DWORD dwDataBufferSize);
  170. void Lock()
  171. {
  172. if (IsLockable())
  173. EnterCriticalSection(&m_cs);
  174. }
  175. void Unlock()
  176. {
  177. if (IsLockable())
  178. LeaveCriticalSection(&m_cs);
  179. }
  180. BOOL IsPropNull(DWORD dwIndex)
  181. {
  182. LPDWORD pTable = GetNullTable();
  183. return !(pTable[dwIndex / 32] & (1 << (dwIndex % 32)));
  184. }
  185. void SetPropNull(DWORD dwIndex, BOOL bNull)
  186. {
  187. LPDWORD pTable = GetNullTable();
  188. if (bNull)
  189. pTable[dwIndex / 32] &= ~(1 << (dwIndex % 32));
  190. else
  191. pTable[dwIndex / 32] |= 1 << (dwIndex % 32);
  192. }
  193. BOOL IsLockable()
  194. {
  195. return (m_dwFlags & WMI_CREATEOBJ_LOCKABLE) != 0;
  196. }
  197. protected:
  198. CPropInfoArray m_pProps;
  199. CBuffer m_bufferEventLayout;
  200. BOOL m_bLayoutSent,
  201. m_bEnabled;
  202. DWORD m_iCurrentVar,
  203. m_dwFlags;
  204. va_list m_valist;
  205. DWORD *m_pdwNullTable;
  206. DWORD *m_pdwPropTable;
  207. DWORD *m_pdwHeapData;
  208. void RecalcTables();
  209. BOOL AddBYTE();
  210. BOOL AddWORD();
  211. BOOL AddDWORD();
  212. BOOL AddDWORD64();
  213. BOOL AddFloat();
  214. BOOL AddStringW();
  215. BOOL AddObject();
  216. BOOL AddWmiObject();
  217. BOOL AddScalarArray();
  218. BOOL AddStringArray();
  219. DWORD GetEventIndex()
  220. {
  221. return *(DWORD*) (m_bufferEventLayout.m_pBuffer + sizeof(DWORD) * 2);
  222. }
  223. DWORD GetPropertyCount()
  224. {
  225. return *(DWORD*) (m_bufferEventLayout.m_pBuffer + sizeof(DWORD) * 4);
  226. }
  227. void SetPropertyCount(DWORD nProps)
  228. {
  229. *(DWORD*) (m_bufferEventLayout.m_pBuffer + sizeof(DWORD) * 4) =
  230. nProps;
  231. }
  232. DWORD *GetNullTable()
  233. {
  234. return m_pdwNullTable;
  235. }
  236. LPBYTE GetPropData(DWORD dwPropIndex);
  237. // Used by SetLayoutAndDataBuffers to figure out the current data size of
  238. // a property and set m_dwCurrentSize with it.
  239. DWORD CalcPropDataSize(CPropInfo *pInfo);
  240. // Called when our buffer is resized.
  241. virtual void OnResize()
  242. {
  243. RecalcTables();
  244. }
  245. };