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.

320 lines
7.3 KiB

  1. /*++
  2. Copyright (c) 1997-2000 Microsoft Corporation
  3. Module Name:
  4. rndutil.h
  5. Abstract:
  6. Definitions for some utility classes and functions.
  7. --*/
  8. #ifndef __RENDEZVOUS_GENERAL__
  9. #define __RENDEZVOUS_GENERAL__
  10. #include "rndcommc.h"
  11. // extra includes for new GetDomainControllerName function.
  12. #include <dsgetdc.h>
  13. #include <objbase.h>
  14. #include <lmcons.h>
  15. #include <lmapibuf.h>
  16. /////////////////////////////////////////////////////////////////////////////
  17. // CBstr is a smart pointer for a BSTR.
  18. /////////////////////////////////////////////////////////////////////////////
  19. class CBstr
  20. {
  21. public:
  22. CBstr() : m_Bstr(NULL) {}
  23. ~CBstr() { if (m_Bstr) SysFreeString(m_Bstr); }
  24. BSTR &GetBstr() { return m_Bstr; }
  25. operator BSTR() { return m_Bstr; }
  26. BSTR *operator&() { return &m_Bstr; }
  27. HRESULT SetBstr(const TCHAR * const wStr)
  28. {
  29. if (NULL == m_Bstr)
  30. {
  31. m_Bstr = SysAllocString(wStr);
  32. return (NULL == m_Bstr) ? E_OUTOFMEMORY : S_OK;
  33. }
  34. else
  35. return (!SysReAllocString(&m_Bstr, wStr)) ? E_OUTOFMEMORY : S_OK;
  36. }
  37. protected:
  38. BSTR m_Bstr;
  39. };
  40. /////////////////////////////////////////////////////////////////////////////
  41. // CTstr is a smart pointer for a TSTR.
  42. /////////////////////////////////////////////////////////////////////////////
  43. class CTstr
  44. {
  45. public:
  46. CTstr() : m_p(NULL){}
  47. CTstr(TCHAR *p) : m_p(p){}
  48. ~CTstr() { delete m_p; }
  49. operator TCHAR * () { return m_p; }
  50. TCHAR ** operator& () { return &m_p; }
  51. BOOL operator! () { return (m_p == NULL); }
  52. BOOL set (TCHAR *p)
  53. {
  54. if (p == NULL)
  55. {
  56. delete m_p;
  57. m_p = NULL;
  58. return TRUE;
  59. }
  60. TCHAR *t = new TCHAR [lstrlen(p) + 1];
  61. if (t == NULL) return FALSE;
  62. delete m_p;
  63. m_p = t;
  64. lstrcpy(m_p, p);
  65. return TRUE;
  66. }
  67. private:
  68. TCHAR *m_p;
  69. };
  70. /////////////////////////////////////////////////////////////////////////////
  71. // CTstr is a smart pointer for a TSTR.
  72. /////////////////////////////////////////////////////////////////////////////
  73. template <class T>
  74. class CSmartPointer
  75. {
  76. public:
  77. CSmartPointer() : m_p(NULL){}
  78. CSmartPointer(T *p) : m_p(p){}
  79. ~CSmartPointer() { delete m_p; }
  80. operator T * () { return m_p; }
  81. T ** operator& () { return &m_p; }
  82. BOOL operator! () { return (m_p == NULL); }
  83. private:
  84. T *m_p;
  85. };
  86. /////////////////////////////////////////////////////////////////////////////
  87. // _CopyBSTR is used in creating BSTR enumerators.
  88. /////////////////////////////////////////////////////////////////////////////
  89. class _CopyBSTR
  90. {
  91. public:
  92. static void copy(BSTR *p1, BSTR *p2)
  93. {
  94. (*p1) = SysAllocString(*p2);
  95. }
  96. static void init(BSTR* p) {*p = NULL;}
  97. static void destroy(BSTR* p) { SysFreeString(*p);}
  98. };
  99. /////////////////////////////////////////////////////////////////////////////
  100. // my critical section
  101. /////////////////////////////////////////////////////////////////////////////
  102. class CCritSection
  103. {
  104. private:
  105. CRITICAL_SECTION m_CritSec;
  106. public:
  107. CCritSection()
  108. {
  109. InitializeCriticalSection(&m_CritSec);
  110. }
  111. ~CCritSection()
  112. {
  113. DeleteCriticalSection(&m_CritSec);
  114. }
  115. void Lock()
  116. {
  117. EnterCriticalSection(&m_CritSec);
  118. }
  119. BOOL TryLock()
  120. {
  121. return TryEnterCriticalSection(&m_CritSec);
  122. }
  123. void Unlock()
  124. {
  125. LeaveCriticalSection(&m_CritSec);
  126. }
  127. };
  128. /////////////////////////////////////////////////////////////////////////////
  129. // an auto lock that uses my critical section
  130. /////////////////////////////////////////////////////////////////////////////
  131. class CLock
  132. {
  133. private:
  134. CCritSection &m_CriticalSection;
  135. public:
  136. CLock(CCritSection &CriticalSection)
  137. : m_CriticalSection(CriticalSection)
  138. {
  139. m_CriticalSection.Lock();
  140. }
  141. ~CLock()
  142. {
  143. m_CriticalSection.Unlock();
  144. }
  145. };
  146. /////////////////////////////////////////////////////////////////////////////
  147. // an simple vector implementation
  148. /////////////////////////////////////////////////////////////////////////////
  149. const DWORD DELTA = 8;
  150. template <class T, DWORD delta = DELTA>
  151. class SimpleVector
  152. {
  153. public:
  154. SimpleVector() : m_dwSize(0), m_dwCapacity(0), m_Elements(NULL) {};
  155. ~SimpleVector() {if (m_Elements) free(m_Elements); }
  156. BOOL add(T& elem)
  157. {
  158. return grow(1) ? (m_Elements[m_dwSize ++] = elem, TRUE) : FALSE;
  159. }
  160. BOOL add()
  161. {
  162. return grow(1) ? (m_dwSize ++, TRUE) : FALSE;
  163. }
  164. DWORD shrink(DWORD i = 1)
  165. {
  166. return m_dwSize -= i;
  167. }
  168. void removeAt(DWORD i)
  169. {
  170. m_Elements[ i ] = m_Elements[ --m_dwSize ];
  171. }
  172. void reset()
  173. {
  174. m_dwSize = 0;
  175. m_dwCapacity = 0;
  176. if (m_Elements) free(m_Elements);
  177. m_Elements = NULL;
  178. }
  179. DWORD size() const { return m_dwSize; }
  180. T& operator [] (DWORD index) { return m_Elements[index]; }
  181. const T* elements() const { return m_Elements; };
  182. protected:
  183. BOOL grow(DWORD i)
  184. {
  185. // grow by one element. If it is out of capacity, allocate more.
  186. if (m_dwSize + i>= m_dwCapacity)
  187. {
  188. DWORD dwInc = ((m_dwSize + i - m_dwCapacity) / delta + 1) * delta;
  189. T *p = (T*)realloc(m_Elements, (sizeof T)*(m_dwCapacity + dwInc));
  190. if (p == NULL)
  191. {
  192. return FALSE;
  193. }
  194. m_Elements = p;
  195. m_dwCapacity += delta;
  196. }
  197. return TRUE;
  198. }
  199. protected:
  200. DWORD m_dwSize;
  201. DWORD m_dwCapacity;
  202. T * m_Elements;
  203. };
  204. /////////////////////////////////////////////////////////////////////////////
  205. // other inline functions
  206. /////////////////////////////////////////////////////////////////////////////
  207. HRESULT GetDomainControllerName(
  208. IN ULONG ulFlags,
  209. OUT WCHAR ** ppszName
  210. );
  211. HRESULT CreateDialableAddressEnumerator(
  212. IN BSTR * begin,
  213. IN BSTR * end,
  214. OUT IEnumDialableAddrs ** ppIEnum
  215. );
  216. HRESULT CreateBstrCollection(
  217. IN long nSize,
  218. IN BSTR * begin,
  219. IN BSTR * end,
  220. OUT VARIANT * pVariant,
  221. CComEnumFlags flags
  222. );
  223. HRESULT CreateDirectoryObjectEnumerator(
  224. IN ITDirectoryObject ** begin,
  225. IN ITDirectoryObject ** end,
  226. OUT IEnumDirectoryObject ** ppIEnum
  227. );
  228. HRESULT CreateEmptyUser(
  229. IN BSTR pName,
  230. OUT ITDirectoryObject ** ppDirectoryObject
  231. );
  232. HRESULT CreateEmptyConference(
  233. IN BSTR pName,
  234. OUT ITDirectoryObject ** ppDirectoryObject
  235. );
  236. HRESULT CreateConferenceWithBlob(
  237. IN BSTR pName,
  238. IN BSTR pProtocol,
  239. IN BSTR pBlob,
  240. IN CHAR * pSecurityDescriptor,
  241. IN DWORD dwSDSize,
  242. OUT ITDirectoryObject ** ppDirectoryObject
  243. );
  244. HRESULT ResolveHostName(
  245. DWORD dwInterface,
  246. TCHAR * pHost,
  247. char ** pFullName,
  248. DWORD * pdwIP
  249. );
  250. int LookupILSServiceBegin(
  251. HANDLE * pHandle
  252. );
  253. int LookupILSServiceNext(
  254. HANDLE Handle,
  255. TCHAR * pBuf,
  256. DWORD * pdwBufSize,
  257. WORD * pwPort
  258. );
  259. int LookupILSServiceEnd(
  260. HANDLE Handle
  261. );
  262. void ipAddressToStringW(
  263. WCHAR * wszDest,
  264. DWORD dwAddress
  265. );
  266. #endif // __RENDEZVOUS_GENERAL__