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.

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