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.

474 lines
10 KiB

  1. #ifndef __HELPERFUNCS_CPP
  2. #define __HELPERFUNCS_CPP
  3. /*
  4. * Class:
  5. *
  6. * WmiAllocator
  7. *
  8. * Description:
  9. *
  10. * Provides abstraction above heap allocation functions
  11. *
  12. * Version:
  13. *
  14. * Initial
  15. *
  16. * Last Changed:
  17. *
  18. * See Source Depot for change history
  19. *
  20. */
  21. #include <Windows.h>
  22. #include <assert.h>
  23. #include <Allocator.h>
  24. #include <pssException.h>
  25. #include <HelperFuncs.h>
  26. #include <strsafe.h>
  27. /******************************************************************************
  28. *
  29. * Name:
  30. *
  31. *
  32. * Description:
  33. *
  34. *
  35. *****************************************************************************/
  36. #if 0
  37. WmiStatusCode WmiHelper :: InitializeCriticalSection ( CRITICAL_SECTION *a_CriticalSection )
  38. {
  39. SetStructuredExceptionHandler t_StructuredException ;
  40. WmiStatusCode t_StatusCode = e_StatusCode_Success ;
  41. try
  42. {
  43. :: InitializeCriticalSection ( a_CriticalSection ) ;
  44. }
  45. catch ( Structured_Exception & t_StructuredException )
  46. {
  47. if ( t_StructuredException.GetExceptionCode () == STATUS_NO_MEMORY )
  48. {
  49. t_StatusCode = e_StatusCode_OutOfMemory ;
  50. }
  51. else
  52. {
  53. t_StatusCode = e_StatusCode_Unknown ;
  54. #ifdef DBG
  55. assert ( FALSE ) ;
  56. #endif
  57. }
  58. }
  59. catch ( ... )
  60. {
  61. t_StatusCode = e_StatusCode_Unknown ;
  62. #ifdef DBG
  63. assert ( FALSE ) ;
  64. #endif
  65. }
  66. return t_StatusCode ;
  67. }
  68. /******************************************************************************
  69. *
  70. * Name:
  71. *
  72. *
  73. * Description:
  74. *
  75. *
  76. *****************************************************************************/
  77. WmiStatusCode WmiHelper :: DeleteCriticalSection ( CRITICAL_SECTION *a_CriticalSection )
  78. {
  79. WmiStatusCode t_StatusCode = e_StatusCode_Success ;
  80. :: DeleteCriticalSection ( a_CriticalSection ) ;
  81. return t_StatusCode ;
  82. }
  83. /******************************************************************************
  84. *
  85. * Name:
  86. *
  87. *
  88. * Description:
  89. *
  90. *
  91. *****************************************************************************/
  92. WmiStatusCode WmiHelper :: EnterCriticalSection ( CRITICAL_SECTION *a_CriticalSection , BOOL a_WaitCritical )
  93. {
  94. SetStructuredExceptionHandler t_StructuredException ;
  95. WmiStatusCode t_StatusCode = e_StatusCode_Success ;
  96. BOOL t_Do ;
  97. do
  98. {
  99. try
  100. {
  101. :: EnterCriticalSection ( a_CriticalSection ) ;
  102. t_Do = FALSE ;
  103. }
  104. catch ( Structured_Exception & t_StructuredException )
  105. {
  106. t_Do = a_WaitCritical ;
  107. if ( t_Do )
  108. {
  109. Sleep ( 1000 ) ;
  110. }
  111. if ( t_StructuredException.GetExceptionCode () == STATUS_NO_MEMORY )
  112. {
  113. t_StatusCode = e_StatusCode_OutOfMemory ;
  114. }
  115. else
  116. {
  117. t_StatusCode = e_StatusCode_Unknown ;
  118. #ifdef DBG
  119. assert ( FALSE ) ;
  120. #endif
  121. t_Do = FALSE ;
  122. }
  123. }
  124. catch ( ... )
  125. {
  126. #ifdef DBG
  127. assert ( FALSE ) ;
  128. #endif
  129. t_Do = FALSE ;
  130. t_StatusCode = e_StatusCode_Unknown ;
  131. }
  132. }
  133. while ( t_Do ) ;
  134. return t_StatusCode ;
  135. }
  136. /******************************************************************************
  137. *
  138. * Name:
  139. *
  140. *
  141. * Description:
  142. *
  143. *
  144. *****************************************************************************/
  145. WmiStatusCode WmiHelper :: LeaveCriticalSection ( CRITICAL_SECTION *a_CriticalSection )
  146. {
  147. WmiStatusCode t_StatusCode = e_StatusCode_Success ;
  148. :: LeaveCriticalSection ( a_CriticalSection ) ;
  149. return t_StatusCode ;
  150. }
  151. #endif
  152. WmiStatusCode WmiHelper :: InitializeCriticalSection ( CriticalSection *a_CriticalSection )
  153. {
  154. return a_CriticalSection->valid() ? e_StatusCode_Success : e_StatusCode_OutOfMemory;
  155. }
  156. WmiStatusCode WmiHelper :: DeleteCriticalSection ( CriticalSection *a_CriticalSection )
  157. {
  158. return e_StatusCode_Success ;
  159. }
  160. WmiStatusCode WmiHelper :: EnterCriticalSection ( CriticalSection *a_CriticalSection, BOOL a_WaitCritical )
  161. {
  162. if (a_CriticalSection->valid())
  163. {
  164. if (a_CriticalSection->acquire())
  165. return e_StatusCode_Success;
  166. if (a_WaitCritical == FALSE )
  167. return e_StatusCode_OutOfMemory;
  168. while (!a_CriticalSection->acquire())
  169. Sleep(1000);
  170. return e_StatusCode_Success;
  171. }
  172. else
  173. return e_StatusCode_OutOfMemory ;
  174. }
  175. WmiStatusCode WmiHelper :: LeaveCriticalSection ( CriticalSection *a_CriticalSection )
  176. {
  177. a_CriticalSection->release();
  178. return e_StatusCode_Success ;
  179. }
  180. /******************************************************************************
  181. *
  182. * Name:
  183. *
  184. *
  185. * Description:
  186. *
  187. *
  188. *****************************************************************************/
  189. WmiStatusCode WmiHelper :: DuplicateString ( WmiAllocator &a_Allocator , const wchar_t *a_String , wchar_t *&a_DuplicatedString )
  190. {
  191. WmiStatusCode t_StatusCode = e_StatusCode_Success ;
  192. if ( a_String )
  193. {
  194. t_StatusCode = a_Allocator.New (
  195. ( void ** ) & a_DuplicatedString ,
  196. sizeof ( wchar_t ) * ( wcslen ( a_String ) + 1)
  197. ) ;
  198. if ( t_StatusCode == e_StatusCode_Success )
  199. {
  200. StringCbCopyW ( a_DuplicatedString , sizeof ( wchar_t ) * ( wcslen ( a_String ) + 1) , a_String ) ;
  201. }
  202. }
  203. return t_StatusCode ;
  204. }
  205. /******************************************************************************
  206. *
  207. * Name:
  208. *
  209. *
  210. * Description:
  211. *
  212. *
  213. *****************************************************************************/
  214. WmiStatusCode WmiHelper :: CreateUnNamedEvent ( HANDLE &a_Event , BOOL a_ManualReset , BOOL a_InitialState )
  215. {
  216. WmiStatusCode t_StatusCode = e_StatusCode_Success ;
  217. a_Event = CreateEvent ( NULL , a_ManualReset , a_InitialState , NULL ) ;
  218. if ( ! a_Event )
  219. {
  220. t_StatusCode = e_StatusCode_OutOfResources ;
  221. }
  222. return t_StatusCode ;
  223. }
  224. /******************************************************************************
  225. *
  226. * Name:
  227. *
  228. *
  229. * Description:
  230. *
  231. *
  232. *****************************************************************************/
  233. WmiStatusCode WmiHelper :: CreateNamedEvent ( wchar_t *a_Name , HANDLE &a_Event , BOOL a_ManualReset , BOOL a_InitialState )
  234. {
  235. WmiStatusCode t_StatusCode = e_StatusCode_Success ;
  236. if ( t_StatusCode == e_StatusCode_Success )
  237. {
  238. a_Event = CreateEvent (
  239. NULL ,
  240. a_ManualReset ,
  241. a_InitialState ,
  242. a_Name
  243. ) ;
  244. if ( a_Event == NULL )
  245. {
  246. if ( GetLastError () == ERROR_ALREADY_EXISTS )
  247. {
  248. a_Event = OpenEvent (
  249. EVENT_ALL_ACCESS ,
  250. FALSE ,
  251. a_Name
  252. ) ;
  253. if ( a_Event == NULL )
  254. {
  255. t_StatusCode = e_StatusCode_Unknown ;
  256. }
  257. }
  258. else
  259. {
  260. t_StatusCode = e_StatusCode_OutOfResources ;
  261. }
  262. }
  263. }
  264. return t_StatusCode ;
  265. }
  266. /******************************************************************************
  267. *
  268. * Name:
  269. *
  270. *
  271. * Description:
  272. *
  273. *
  274. *****************************************************************************/
  275. WmiStatusCode WmiHelper :: DuplicateHandle ( HANDLE a_Handle , HANDLE &a_DuplicatedHandle )
  276. {
  277. WmiStatusCode t_StatusCode = e_StatusCode_Success ;
  278. HANDLE t_ProcessHandle = GetCurrentProcess ();
  279. BOOL t_Status = :: DuplicateHandle (
  280. t_ProcessHandle ,
  281. a_Handle ,
  282. t_ProcessHandle ,
  283. & a_DuplicatedHandle ,
  284. 0 ,
  285. FALSE ,
  286. DUPLICATE_SAME_ACCESS
  287. ) ;
  288. if ( ! t_Status )
  289. {
  290. t_StatusCode = e_StatusCode_OutOfResources ;
  291. }
  292. return t_StatusCode ;
  293. }
  294. /******************************************************************************
  295. *
  296. * Name:
  297. *
  298. *
  299. * Description:
  300. *
  301. *
  302. *****************************************************************************/
  303. WmiStatusCode WmiHelper :: ConcatenateStrings ( ULONG a_ArgCount , BSTR *a_AllocatedString , ... )
  304. {
  305. WmiStatusCode t_StatusCode = e_StatusCode_Success ;
  306. va_list t_VarArgList ;
  307. va_start ( t_VarArgList , a_AllocatedString );
  308. ULONG t_AllocatedStringLength = 0 ;
  309. ULONG t_ArgIndex = 0 ;
  310. while ( t_ArgIndex < a_ArgCount )
  311. {
  312. LPWSTR t_CurrentArg = va_arg ( t_VarArgList , LPWSTR ) ;
  313. if ( t_CurrentArg )
  314. {
  315. t_AllocatedStringLength = t_AllocatedStringLength + wcslen ( t_CurrentArg ) ;
  316. }
  317. t_ArgIndex ++ ;
  318. }
  319. va_end ( t_VarArgList ) ;
  320. va_start ( t_VarArgList , a_AllocatedString );
  321. *a_AllocatedString = SysAllocStringLen ( NULL , t_AllocatedStringLength + 1 ) ;
  322. size_t allocatedBufferSize = t_AllocatedStringLength + 1;
  323. if ( *a_AllocatedString )
  324. {
  325. t_ArgIndex = 0 ;
  326. t_AllocatedStringLength = 0 ;
  327. while ( t_ArgIndex < a_ArgCount )
  328. {
  329. LPWSTR t_CurrentArg = va_arg ( t_VarArgList , LPWSTR ) ;
  330. if ( t_CurrentArg )
  331. {
  332. HRESULT hr = StringCchCopyW ( & ( ( * a_AllocatedString ) [ t_AllocatedStringLength ] ) , allocatedBufferSize -t_AllocatedStringLength, t_CurrentArg ) ;
  333. t_AllocatedStringLength = t_AllocatedStringLength + wcslen ( t_CurrentArg ) ;
  334. }
  335. t_ArgIndex ++ ;
  336. }
  337. ( ( *a_AllocatedString ) [ t_AllocatedStringLength ] ) = NULL ;
  338. }
  339. else
  340. {
  341. t_StatusCode = e_StatusCode_OutOfMemory ;
  342. }
  343. va_end ( t_VarArgList ) ;
  344. return t_StatusCode ;
  345. }
  346. /******************************************************************************
  347. *
  348. * Name:
  349. *
  350. *
  351. * Description:
  352. *
  353. *
  354. *****************************************************************************/
  355. WmiStatusCode WmiHelper :: ConcatenateStrings_Wchar ( ULONG a_ArgCount , wchar_t **a_AllocatedString , ... )
  356. {
  357. WmiStatusCode t_StatusCode = e_StatusCode_Success ;
  358. va_list t_VarArgList ;
  359. va_start ( t_VarArgList , a_AllocatedString );
  360. ULONG t_AllocatedStringLength = 0 ;
  361. ULONG t_ArgIndex = 0 ;
  362. while ( t_ArgIndex < a_ArgCount )
  363. {
  364. LPWSTR t_CurrentArg = va_arg ( t_VarArgList , LPWSTR ) ;
  365. if ( t_CurrentArg )
  366. {
  367. t_AllocatedStringLength = t_AllocatedStringLength + wcslen ( t_CurrentArg ) ;
  368. }
  369. t_ArgIndex ++ ;
  370. }
  371. va_end ( t_VarArgList ) ;
  372. va_start ( t_VarArgList , a_AllocatedString );
  373. *a_AllocatedString = new wchar_t [ t_AllocatedStringLength + 1 ] ;
  374. size_t allocatedBufferSize = t_AllocatedStringLength + 1;
  375. if ( *a_AllocatedString )
  376. {
  377. t_ArgIndex = 0 ;
  378. t_AllocatedStringLength = 0 ;
  379. while ( t_ArgIndex < a_ArgCount )
  380. {
  381. LPWSTR t_CurrentArg = va_arg ( t_VarArgList , LPWSTR ) ;
  382. if ( t_CurrentArg )
  383. {
  384. HRESULT hr = StringCchCopyW ( & ( ( * a_AllocatedString ) [ t_AllocatedStringLength ] ) , allocatedBufferSize -t_AllocatedStringLength, t_CurrentArg ) ;
  385. t_AllocatedStringLength = t_AllocatedStringLength + wcslen ( t_CurrentArg ) ;
  386. }
  387. t_ArgIndex ++ ;
  388. }
  389. ( ( *a_AllocatedString ) [ t_AllocatedStringLength ] ) = NULL ;
  390. }
  391. else
  392. {
  393. t_StatusCode = e_StatusCode_OutOfMemory ;
  394. }
  395. va_end ( t_VarArgList ) ;
  396. return t_StatusCode ;
  397. }
  398. #endif __HELPERFUNCS_CPP