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.

370 lines
7.1 KiB

  1. // P2Prov.cpp : Defines the entry point for the DLL application.
  2. //
  3. #include "precomp.h"
  4. #include <crtdbg.h>
  5. #include "buffer.h"
  6. #include "NCDefs.h"
  7. #include "NCObjApi.h"
  8. #include "dutils.h"
  9. #include "Connection.h"
  10. #include "Event.h"
  11. #include "Transport.h"
  12. #include "NamedPipe.h"
  13. #include <stdio.h>
  14. #define DWORD_ALIGNED(x) ((DWORD)((((x) * 8) + 31) & (~31)) / 8)
  15. /////////////////////////////////////////////////////////////////////////////
  16. // DllMain
  17. BOOL APIENTRY DllMain(
  18. HANDLE hModule,
  19. DWORD dwReason,
  20. LPVOID lpReserved)
  21. {
  22. return TRUE;
  23. }
  24. void SetOutOfMemory()
  25. {
  26. SetLastError(ERROR_OUTOFMEMORY);
  27. }
  28. /////////////////////////////////////////////////////
  29. // Functions exposed in DLL
  30. // Register to send events
  31. HANDLE WMIAPI WmiEventSourceConnect(
  32. LPCWSTR szNamespace,
  33. LPCWSTR szProviderName,
  34. BOOL bBatchSend,
  35. DWORD dwBatchBufferSize,
  36. DWORD dwMaxSendLatency,
  37. LPVOID pUserData,
  38. LPEVENT_SOURCE_CALLBACK pCallback)
  39. {
  40. if(szNamespace == NULL)
  41. {
  42. _ASSERT(FALSE);
  43. return NULL;
  44. }
  45. if(szProviderName == NULL)
  46. {
  47. _ASSERT(FALSE);
  48. return NULL;
  49. }
  50. CConnection *pConnection = NULL;
  51. CSink *pSink = NULL;
  52. if (!szNamespace || !szProviderName)
  53. return NULL;
  54. try
  55. {
  56. pConnection =
  57. new CConnection(bBatchSend, dwBatchBufferSize, dwMaxSendLatency);
  58. if (pConnection)
  59. {
  60. if (pConnection->Init(
  61. szNamespace,
  62. szProviderName,
  63. pUserData,
  64. pCallback))
  65. {
  66. pSink = pConnection->GetMainSink();
  67. }
  68. else
  69. {
  70. delete pConnection;
  71. pConnection = NULL;
  72. SetOutOfMemory();
  73. }
  74. }
  75. else
  76. SetOutOfMemory();
  77. }
  78. catch(...)
  79. {
  80. SetOutOfMemory();
  81. }
  82. return (HANDLE) pSink;
  83. }
  84. void WMIAPI WmiEventSourceDisconnect(HANDLE hSource)
  85. {
  86. if (!hSource)
  87. {
  88. _ASSERT(FALSE);
  89. return;
  90. }
  91. try
  92. {
  93. CSink *pSink = (CSink*) hSource;
  94. delete pSink->GetConnection();
  95. }
  96. catch(...)
  97. {
  98. }
  99. }
  100. BOOL
  101. WMIAPI
  102. WmiCommitObject(
  103. HANDLE hObject)
  104. {
  105. BOOL bRet = FALSE;
  106. if (!hObject)
  107. return FALSE;
  108. try
  109. {
  110. CEvent *pBuffer = ((CEventWrap*) hObject)->GetEvent();
  111. bRet = pBuffer->SendEvent();
  112. }
  113. catch(...)
  114. {
  115. }
  116. return bRet;
  117. }
  118. BOOL WMIAPI WmiSetAndCommitObject(
  119. HANDLE hObject,
  120. DWORD dwFlags,
  121. ...)
  122. {
  123. BOOL bRet;
  124. if (!hObject)
  125. return FALSE;
  126. if((dwFlags & ~WMI_SENDCOMMIT_SET_NOT_REQUIRED & ~WMI_USE_VA_LIST) != 0)
  127. return FALSE;
  128. try
  129. {
  130. CEventWrap *pWrap = (CEventWrap *) hObject;
  131. CEvent *pEvent = pWrap->GetEvent();
  132. BOOL bEnabled = pEvent->IsEnabled();
  133. // If the data to be set isn't important and if the event isn't
  134. // enabled, just return TRUE.
  135. if ((dwFlags & WMI_SENDCOMMIT_SET_NOT_REQUIRED) && !bEnabled)
  136. {
  137. bRet = TRUE;
  138. }
  139. else
  140. {
  141. va_list *pList;
  142. va_list list;
  143. va_start(list, dwFlags);
  144. if (!(dwFlags & WMI_USE_VA_LIST))
  145. pList = &list;
  146. else
  147. pList = va_arg(list, va_list*);
  148. // Make sure we have the event locked until we commit the values
  149. // we set.
  150. CCondInCritSec cs(&pEvent->m_cs, pEvent->IsLockable());
  151. bRet =
  152. pEvent->SetPropValues(
  153. pWrap->GetIndexArray(),
  154. *pList);
  155. if (bEnabled && bRet)
  156. WmiCommitObject(hObject);
  157. }
  158. }
  159. catch(...)
  160. {
  161. bRet = FALSE;
  162. }
  163. return bRet;
  164. }
  165. DWORD dwSet = 0;
  166. BOOL WMIAPI WmiDestroyObject(
  167. HANDLE hObject)
  168. {
  169. if (!hObject)
  170. {
  171. _ASSERT(FALSE);
  172. return FALSE;
  173. }
  174. try
  175. {
  176. delete (CEventWrap *) hObject;
  177. }
  178. catch(...)
  179. {
  180. }
  181. return TRUE;
  182. }
  183. HANDLE WMIAPI WmiCreateObjectWithFormat(
  184. HANDLE hSource,
  185. LPCWSTR szClassName,
  186. DWORD dwFlags,
  187. LPCWSTR szFormat)
  188. {
  189. CSink *pSink = (CSink*) hSource;
  190. HANDLE hEvent;
  191. BOOL bRet = FALSE;
  192. if (!pSink || !szClassName || !szFormat)
  193. return NULL;
  194. if((dwFlags & ~WMI_CREATEOBJ_LOCKABLE) != 0)
  195. return NULL;
  196. try
  197. {
  198. hEvent =
  199. pSink->m_mapReportEvents.CreateEvent(
  200. hSource, szClassName, dwFlags, szFormat);
  201. }
  202. catch(...)
  203. {
  204. hEvent = NULL;
  205. }
  206. return hEvent;
  207. }
  208. BOOL WMIAPI WmiIsObjectActive(HANDLE hObject)
  209. {
  210. BOOL bRet;
  211. if (!hObject)
  212. return FALSE;
  213. try
  214. {
  215. CEvent *pEvent = ((CEventWrap*) hObject)->GetEvent();
  216. bRet = pEvent->IsEnabled();
  217. }
  218. catch(...)
  219. {
  220. bRet = FALSE;
  221. }
  222. return bRet;
  223. }
  224. HANDLE WMIAPI WmiCreateObjectWithProps(
  225. HANDLE hSource,
  226. LPCWSTR szEventName,
  227. DWORD dwFlags,
  228. DWORD nPropertyCount,
  229. LPCWSTR *pszPropertyNames,
  230. CIMTYPE *pPropertyTypes)
  231. {
  232. CSink *pSink = NULL;
  233. CEventWrap *pWrap = NULL;
  234. if (!hSource || !szEventName)
  235. return NULL;
  236. if((dwFlags & ~WMI_CREATEOBJ_LOCKABLE) != 0)
  237. return NULL;
  238. try
  239. {
  240. pSink = (CSink*) hSource;
  241. pWrap = new CEventWrap(pSink, dwFlags);
  242. if (pWrap)
  243. {
  244. CEvent *pEvent = pWrap->GetEvent();
  245. if (pWrap->GetEvent()->PrepareEvent(
  246. szEventName,
  247. nPropertyCount,
  248. pszPropertyNames,
  249. pPropertyTypes))
  250. {
  251. // Figure out if this event should be enabled (ready to be fired)
  252. // or not.
  253. pSink->EnableEventUsingList(pEvent);
  254. }
  255. else
  256. {
  257. delete pWrap;
  258. pWrap = NULL;
  259. }
  260. }
  261. else
  262. SetOutOfMemory();
  263. }
  264. catch(...)
  265. {
  266. }
  267. return (HANDLE) pWrap;
  268. }
  269. // For adding properties one at a time.
  270. HANDLE WMIAPI WmiCreateObject(
  271. HANDLE hSource,
  272. LPCWSTR szEventName,
  273. DWORD dwFlags)
  274. {
  275. if (!hSource)
  276. return NULL;
  277. return
  278. WmiCreateObjectWithProps(
  279. hSource,
  280. szEventName,
  281. dwFlags,
  282. 0,
  283. NULL,
  284. NULL);
  285. }
  286. BOOL WMIAPI WmiAddObjectProp(
  287. HANDLE hObject,
  288. LPCWSTR szPropertyName,
  289. CIMTYPE type,
  290. DWORD *pdwPropIndex)
  291. {
  292. BOOL bRet;
  293. if (!hObject || !szPropertyName)
  294. return FALSE;
  295. try
  296. {
  297. CEvent *pBuffer = ((CEventWrap *) hObject)->GetEvent();
  298. bRet = pBuffer->AddProp(szPropertyName, type, pdwPropIndex);
  299. }
  300. catch(...)
  301. {
  302. bRet = FALSE;
  303. }
  304. return bRet;
  305. }