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.

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