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.

317 lines
6.5 KiB

  1. /////////////////////////////////////////////////////////////////////
  2. //
  3. // CopyRight (c) 1999-2000 Microsoft Corporation
  4. //
  5. // Module Name:
  6. // SafeHandle.h
  7. //
  8. // Description:
  9. // Implementation of safe handle and pointer classes.
  10. //
  11. // Author:
  12. // Henry Wang (HenryWa) 24-AUG-1999
  13. //
  14. //////////////////////////////////////////////////////////////////////
  15. #pragma once
  16. //////////////////////////////////////////////////////////////////////////////
  17. // Include Files
  18. //////////////////////////////////////////////////////////////////////////////
  19. #include "ObjectPath.h"
  20. //////////////////////////////////////////////////////////////////////////////
  21. // Forward Declarations
  22. //////////////////////////////////////////////////////////////////////////////
  23. class CError;
  24. template< class T > class SafePtr;
  25. class CWstrBuf;
  26. //////////////////////////////////////////////////////////////////////////////
  27. //++
  28. //
  29. // class CClusterApi
  30. //
  31. // Description:
  32. // Wrap class for cluster Api
  33. //
  34. //--
  35. //////////////////////////////////////////////////////////////////////////////
  36. class CError
  37. {
  38. public:
  39. CError( void )
  40. : m_hr( 0 )
  41. {
  42. }
  43. inline CError & operator=( DWORD dw )
  44. {
  45. if ( dw != ERROR_SUCCESS )
  46. {
  47. throw CProvException ( dw );
  48. }
  49. m_hr = HRESULT_FROM_WIN32( dw );
  50. return *this;
  51. }
  52. inline CError & operator=( HRESULT hr )
  53. {
  54. if ( FAILED ( hr ) )
  55. {
  56. throw CProvException ( hr );
  57. }
  58. m_hr = hr;
  59. return *this;
  60. }
  61. operator DWORD( void )
  62. {
  63. return HRESULT_CODE( m_hr );
  64. }
  65. protected:
  66. HRESULT m_hr;
  67. }; //*** CError
  68. //////////////////////////////////////////////////////////////////////////////
  69. //++
  70. //
  71. // template< class T >
  72. // class SafePtr
  73. //
  74. // Description:
  75. // Safe handle/pointer class.
  76. //
  77. // Template Arguments:
  78. // T -- Type of handle or pointer.
  79. //
  80. //--
  81. //////////////////////////////////////////////////////////////////////////////
  82. template< class T >
  83. class SafePtr
  84. {
  85. public:
  86. SafePtr( void )
  87. : m_handle( NULL )
  88. {
  89. }
  90. SafePtr( T h )
  91. : m_handle( h )
  92. {
  93. h = NULL;
  94. }
  95. virtual ~SafePtr( void )
  96. {
  97. deleteHandle( m_handle );
  98. }
  99. T operator&( void )
  100. {
  101. return m_handle;
  102. }
  103. BOOL BIsNULL( void )
  104. {
  105. return m_handle == NULL;
  106. }
  107. operator T( void )
  108. {
  109. return m_handle;
  110. }
  111. operator void * ( void )
  112. {
  113. return static_cast< void * >( m_handle );
  114. }
  115. SafePtr< T > & operator=( T tRhs )
  116. {
  117. if ( tRhs == NULL )
  118. {
  119. throw CProvException ( GetLastError() );
  120. }
  121. if ( m_handle != NULL )
  122. {
  123. deleteHandle( m_handle );
  124. }
  125. m_handle = tRhs;
  126. tRhs = NULL;
  127. return *this;
  128. }
  129. protected:
  130. T m_handle;
  131. void deleteHandle( HCLUSTER hCluster )
  132. {
  133. if ( hCluster && ( CloseCluster( hCluster ) == FALSE ) )
  134. {
  135. throw CProvException( GetLastError() );
  136. }
  137. }
  138. void deleteHandle( HNODE hNode )
  139. {
  140. if ( hNode && ( CloseClusterNode( hNode ) == FALSE ) )
  141. {
  142. throw CProvException( GetLastError() );
  143. }
  144. }
  145. void deleteHandle( HCLUSENUM hEnum )
  146. {
  147. if ( hEnum && ( ClusterCloseEnum( hEnum ) != ERROR_SUCCESS ) )
  148. {
  149. throw CProvException( GetLastError() );
  150. }
  151. }
  152. void deleteHandle( HRESOURCE hRes )
  153. {
  154. if ( hRes && ( CloseClusterResource( hRes ) == FALSE ))
  155. {
  156. throw CProvException( GetLastError() );
  157. }
  158. }
  159. void deleteHandle( HGROUP hGroup )
  160. {
  161. if ( hGroup && ( CloseClusterGroup( hGroup ) == FALSE ) )
  162. {
  163. throw CProvException( GetLastError() );
  164. }
  165. }
  166. void deleteHandle( HNETWORK hNetwork )
  167. {
  168. if ( hNetwork && ( CloseClusterNetwork( hNetwork ) == FALSE ) )
  169. {
  170. throw CProvException( GetLastError() );
  171. }
  172. }
  173. void deleteHandle( HNETINTERFACE hNetInterface )
  174. {
  175. if ( hNetInterface && ( CloseClusterNetInterface( hNetInterface ) == FALSE ) )
  176. {
  177. throw CProvException( GetLastError() );
  178. }
  179. }
  180. void deleteHandle( HRESENUM hResEnum )
  181. {
  182. DWORD dwReturn;
  183. if ( hResEnum )
  184. {
  185. dwReturn = ClusterResourceCloseEnum( hResEnum ) ;
  186. if ( dwReturn != ERROR_SUCCESS )
  187. {
  188. throw CProvException( dwReturn );
  189. }
  190. }
  191. }
  192. void deleteHandle( HGROUPENUM hGroupEnum )
  193. {
  194. DWORD dwReturn;
  195. if ( hGroupEnum )
  196. {
  197. dwReturn = ClusterGroupCloseEnum( hGroupEnum );
  198. if ( dwReturn != ERROR_SUCCESS )
  199. {
  200. throw CProvException( dwReturn );
  201. }
  202. }
  203. }
  204. void deleteHandle( HCHANGE hChange )
  205. {
  206. BOOL fReturn;
  207. if ( hChange )
  208. {
  209. fReturn = CloseClusterNotifyPort( hChange );
  210. if ( ! fReturn )
  211. {
  212. throw CProvException( GetLastError() );
  213. }
  214. }
  215. }
  216. private:
  217. SafePtr( SafePtr< T > & cT )
  218. {
  219. }
  220. }; //*** class SafePtr
  221. //////////////////////////////////////////////////////////////////////////////
  222. //++
  223. //
  224. // class CWstrBuf
  225. //
  226. // Description:
  227. // Wrap class for Unicode strings.
  228. //
  229. //--
  230. //////////////////////////////////////////////////////////////////////////////
  231. class CWstrBuf
  232. {
  233. public:
  234. CWstrBuf( void )
  235. : m_pwsz( NULL )
  236. {
  237. }
  238. ~CWstrBuf( void )
  239. {
  240. delete [] m_pwsz;
  241. };
  242. void SetSize( DWORD dwSize )
  243. {
  244. delete [] m_pwsz;
  245. m_pwsz = new WCHAR[ sizeof( WCHAR ) * dwSize ];
  246. if ( m_pwsz == NULL )
  247. {
  248. throw WBEM_E_OUT_OF_MEMORY;
  249. } else {
  250. m_pwsz[0] = UNICODE_NULL;
  251. }
  252. }
  253. operator WCHAR*( void )
  254. {
  255. return m_pwsz;
  256. }
  257. void Empty( VOID )
  258. {
  259. m_pwsz[0] = UNICODE_NULL;
  260. }
  261. protected:
  262. LPWSTR m_pwsz;
  263. }; //*** class CWstrBuf
  264. typedef SafePtr< HRESOURCE > SAFERESOURCE;
  265. typedef SafePtr< HGROUP > SAFEGROUP;
  266. typedef SafePtr< HCLUSENUM > SAFECLUSENUM;
  267. typedef SafePtr< HNODE > SAFENODE;
  268. typedef SafePtr< HCLUSTER > SAFECLUSTER;
  269. typedef SafePtr< HNETWORK > SAFENETWORK;
  270. typedef SafePtr< HNETINTERFACE> SAFENETINTERFACE;
  271. typedef SafePtr< HRESENUM > SAFERESENUM;
  272. typedef SafePtr< HGROUPENUM > SAFEGROUPENUM;
  273. typedef SafePtr< HCHANGE > SAFECHANGE;