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.

298 lines
6.7 KiB

  1. /*++
  2. Copyright (C) 1996-2001 Microsoft Corporation
  3. Module Name:
  4. Abstract:
  5. History:
  6. --*/
  7. #ifndef __COMUTL_H__
  8. #define __COMUTL_H__
  9. #include <wbemcli.h>
  10. #include "corex.h"
  11. /**************************************************************************
  12. CWbemPtr
  13. ***************************************************************************/
  14. template<class T> class CWbemPtr
  15. {
  16. public :
  17. T* m_pObj;
  18. void SetPtr( T* pObj )
  19. {
  20. if ( m_pObj != NULL )
  21. m_pObj->Release();
  22. m_pObj = pObj;
  23. if ( m_pObj != NULL )
  24. m_pObj->AddRef();
  25. }
  26. CWbemPtr() : m_pObj(NULL) { }
  27. CWbemPtr( T* pObj ) : m_pObj(NULL) { SetPtr(pObj); }
  28. CWbemPtr( const CWbemPtr<T>& p ) : m_pObj(NULL) { SetPtr(p.m_pObj); }
  29. CWbemPtr<T>& operator=( const CWbemPtr<T>& p )
  30. { SetPtr(p.m_pObj); return *this; }
  31. ~CWbemPtr() { if ( m_pObj != NULL ) m_pObj->Release(); }
  32. operator T* () { return m_pObj; }
  33. T** operator& () { return &m_pObj; }
  34. T* operator-> () { return m_pObj; }
  35. void Release() { if( m_pObj != NULL ) m_pObj->Release(); m_pObj = NULL; }
  36. };
  37. /***************************************************************************
  38. CWbemBSTR
  39. ****************************************************************************/
  40. class CWbemBSTR
  41. {
  42. BSTR m_bstr;
  43. public :
  44. CWbemBSTR() : m_bstr(NULL) { }
  45. CWbemBSTR( LPCWSTR lpszStr )
  46. {
  47. if ( lpszStr != NULL )
  48. {
  49. m_bstr = ::SysAllocString(lpszStr);
  50. if ( m_bstr == NULL )
  51. {
  52. throw CX_MemoryException();
  53. }
  54. }
  55. else
  56. {
  57. m_bstr = NULL;
  58. }
  59. }
  60. CWbemBSTR( LPCWSTR lpszStr, ULONG cLen )
  61. {
  62. if ( cLen > 0 )
  63. {
  64. m_bstr = ::SysAllocStringLen( NULL, cLen );
  65. if ( m_bstr == NULL )
  66. {
  67. throw CX_MemoryException();
  68. }
  69. wcscpy( m_bstr, lpszStr );
  70. }
  71. else
  72. {
  73. m_bstr = NULL;
  74. }
  75. }
  76. CWbemBSTR( ULONG cLen )
  77. {
  78. m_bstr = ::SysAllocStringLen( NULL, cLen + 1);
  79. if ( m_bstr == NULL )
  80. {
  81. throw CX_MemoryException();
  82. }
  83. *m_bstr = '\0';
  84. }
  85. CWbemBSTR( const CWbemBSTR& m ) : m_bstr(NULL) { *this = m; }
  86. CWbemBSTR& operator=( const CWbemBSTR& m)
  87. {
  88. ::SysFreeString( m_bstr );
  89. m_bstr = ::SysAllocStringLen( m.m_bstr, m.MaxLength() );
  90. if ( m_bstr == NULL )
  91. {
  92. throw CX_MemoryException();
  93. }
  94. return *this;
  95. }
  96. ~CWbemBSTR( ) { ::SysFreeString( m_bstr ); }
  97. operator BSTR() { return m_bstr; }
  98. BOOL operator!() { return m_bstr == NULL; }
  99. BSTR* operator& () { return &m_bstr; }
  100. void operator += ( LPCWSTR lpszStr )
  101. {
  102. if ( lpszStr == NULL ) return;
  103. Resize( wcslen(lpszStr) + Length() + 10 );
  104. wcscat( m_bstr + Length(), lpszStr );
  105. }
  106. void Free() { ::SysFreeString( m_bstr ); m_bstr = NULL; }
  107. ULONG Length() const {return m_bstr!=NULL ? wcslen(m_bstr) : 0;}
  108. ULONG MaxLength() const {return m_bstr!=NULL ? ::SysStringLen(m_bstr) : 0;}
  109. void Resize( ULONG cLen )
  110. {
  111. if ( cLen > MaxLength() )
  112. {
  113. BSTR bstrNewStr = ::SysAllocStringLen( NULL, cLen + 1 );
  114. if ( bstrNewStr == NULL )
  115. {
  116. throw CX_MemoryException();
  117. }
  118. if ( m_bstr != NULL )
  119. {
  120. wcscpy( bstrNewStr, m_bstr );
  121. ::SysFreeString( m_bstr );
  122. }
  123. else
  124. {
  125. *bstrNewStr = '\0';
  126. }
  127. m_bstr = bstrNewStr;
  128. }
  129. }
  130. };
  131. /***************************************************************************
  132. CPropVar
  133. ****************************************************************************/
  134. class CPropVar
  135. {
  136. public:
  137. VARIANT v;
  138. CPropVar() { VariantInit( &v ); }
  139. ~CPropVar() { VariantClear( &v ); }
  140. VARIANT* operator& () { return &v; }
  141. HRESULT CheckType( VARTYPE vt )
  142. {
  143. if ( V_VT(&v) != vt )
  144. {
  145. return DISP_E_TYPEMISMATCH;
  146. }
  147. return S_OK;
  148. }
  149. HRESULT SetType( VARTYPE vt )
  150. {
  151. if ( vt == V_VT(&v) )
  152. {
  153. return S_OK;
  154. }
  155. return VariantChangeType( &v, &v, 0, vt );
  156. }
  157. };
  158. /***************************************************************************
  159. CPropSafeArray
  160. ****************************************************************************/
  161. template<class T> class CPropSafeArray
  162. {
  163. T* m_aElems;
  164. SAFEARRAY* m_psa;
  165. public:
  166. CPropSafeArray( SAFEARRAY* psa = NULL ) : m_psa(psa), m_aElems(NULL)
  167. {
  168. SafeArrayAccessData( psa, (void**)&m_aElems );
  169. }
  170. CPropSafeArray( CPropSafeArray& rOther )
  171. : m_psa(NULL), m_aElems(NULL)
  172. {
  173. *this = rOther.m_psa;
  174. }
  175. CPropSafeArray& operator=( SAFEARRAY* psa )
  176. {
  177. if ( m_psa != NULL )
  178. {
  179. SafeArrayUnaccessData( m_psa );
  180. }
  181. m_psa = psa;
  182. SafeArrayAccessData( m_psa, (void**)&m_aElems );
  183. return *this;
  184. }
  185. ~CPropSafeArray()
  186. {
  187. if ( m_psa == NULL )
  188. {
  189. return;
  190. }
  191. SafeArrayUnaccessData( m_psa );
  192. }
  193. ULONG Length()
  194. {
  195. if ( m_aElems == NULL )
  196. {
  197. return 0;
  198. }
  199. long lUBound;
  200. SafeArrayGetUBound( m_psa, 1, &lUBound );
  201. return lUBound+1;
  202. }
  203. T* GetArray() { return m_aElems; }
  204. T& operator[](int i) { return m_aElems[i]; }
  205. };
  206. /***************************************************************************
  207. platform independent string conversion routines.
  208. ****************************************************************************/
  209. inline BOOL tsz2wsz( LPCWSTR tsz, WCHAR* pwch, ULONG* pcwch )
  210. {
  211. ULONG cwch = *pcwch;
  212. *pcwch = wcslen(tsz)+1;
  213. if ( cwch < *pcwch ) return FALSE;
  214. wcscpy( pwch, tsz );
  215. return TRUE;
  216. }
  217. inline BOOL tsz2wsz( LPCSTR tsz, WCHAR* pwch, ULONG* pcwch )
  218. {
  219. ULONG cwch = *pcwch;
  220. *pcwch = MultiByteToWideChar(CP_ACP, 0, tsz, -1, NULL, 0 );
  221. if ( cwch < *pcwch ) return FALSE;
  222. MultiByteToWideChar(CP_ACP, 0, tsz, -1, pwch, cwch );
  223. return TRUE;
  224. }
  225. /****************************************************************************
  226. API Enter/Exit macros - catch out of memory conditions and return appropriate
  227. hresult. Use at top level COM Interface implementations.
  228. *****************************************************************************/
  229. #define ENTER_API_CALL try {
  230. #define EXIT_API_CALL } catch(CX_MemoryException){return WBEM_E_OUT_OF_MEMORY;}
  231. #endif // __COMUTL_H__