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.

331 lines
7.9 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. #if _ATL_VER >= 0x0203
  93. static HRESULT copy(BSTR *p1, BSTR *p2)
  94. {
  95. (*p1) = SysAllocString(*p2);
  96. if (*p1)
  97. return S_OK;
  98. else
  99. return E_OUTOFMEMORY;
  100. }
  101. #else
  102. static void copy(BSTR *p1, BSTR *p2)
  103. {
  104. (*p1) = SysAllocString(*p2);
  105. }
  106. #endif
  107. static void init(BSTR* p) {*p = NULL;}
  108. static void destroy(BSTR* p) { SysFreeString(*p);}
  109. };
  110. /////////////////////////////////////////////////////////////////////////////
  111. // my critical section
  112. /////////////////////////////////////////////////////////////////////////////
  113. class CCritSection
  114. {
  115. private:
  116. CRITICAL_SECTION m_CritSec;
  117. public:
  118. CCritSection()
  119. {
  120. InitializeCriticalSection(&m_CritSec);
  121. }
  122. ~CCritSection()
  123. {
  124. DeleteCriticalSection(&m_CritSec);
  125. }
  126. void Lock()
  127. {
  128. EnterCriticalSection(&m_CritSec);
  129. }
  130. BOOL TryLock()
  131. {
  132. return TryEnterCriticalSection(&m_CritSec);
  133. }
  134. void Unlock()
  135. {
  136. LeaveCriticalSection(&m_CritSec);
  137. }
  138. };
  139. /////////////////////////////////////////////////////////////////////////////
  140. // an auto lock that uses my critical section
  141. /////////////////////////////////////////////////////////////////////////////
  142. class CLock
  143. {
  144. private:
  145. CCritSection &m_CriticalSection;
  146. public:
  147. CLock(CCritSection &CriticalSection)
  148. : m_CriticalSection(CriticalSection)
  149. {
  150. m_CriticalSection.Lock();
  151. }
  152. ~CLock()
  153. {
  154. m_CriticalSection.Unlock();
  155. }
  156. };
  157. /////////////////////////////////////////////////////////////////////////////
  158. // an simple vector implementation
  159. /////////////////////////////////////////////////////////////////////////////
  160. const DWORD DELTA = 8;
  161. template <class T, DWORD delta = DELTA>
  162. class SimpleVector
  163. {
  164. public:
  165. SimpleVector() : m_dwSize(0), m_dwCapacity(0), m_Elements(NULL) {};
  166. ~SimpleVector() {if (m_Elements) free(m_Elements); }
  167. BOOL add(T& elem)
  168. {
  169. return grow(1) ? (m_Elements[m_dwSize ++] = elem, TRUE) : FALSE;
  170. }
  171. BOOL add()
  172. {
  173. return grow(1) ? (m_dwSize ++, TRUE) : FALSE;
  174. }
  175. DWORD shrink(DWORD i = 1)
  176. {
  177. return m_dwSize -= i;
  178. }
  179. void removeAt(DWORD i)
  180. {
  181. m_Elements[ i ] = m_Elements[ --m_dwSize ];
  182. }
  183. void reset()
  184. {
  185. m_dwSize = 0;
  186. m_dwCapacity = 0;
  187. if (m_Elements) free(m_Elements);
  188. m_Elements = NULL;
  189. }
  190. DWORD size() const { return m_dwSize; }
  191. T& operator [] (DWORD index) { return m_Elements[index]; }
  192. const T* elements() const { return m_Elements; };
  193. protected:
  194. BOOL grow(DWORD i)
  195. {
  196. // grow by one element. If it is out of capacity, allocate more.
  197. if (m_dwSize + i>= m_dwCapacity)
  198. {
  199. DWORD dwInc = ((m_dwSize + i - m_dwCapacity) / delta + 1) * delta;
  200. T *p = (T*)realloc(m_Elements, (sizeof T)*(m_dwCapacity + dwInc));
  201. if (p == NULL)
  202. {
  203. return FALSE;
  204. }
  205. m_Elements = p;
  206. m_dwCapacity += delta;
  207. }
  208. return TRUE;
  209. }
  210. protected:
  211. DWORD m_dwSize;
  212. DWORD m_dwCapacity;
  213. T * m_Elements;
  214. };
  215. /////////////////////////////////////////////////////////////////////////////
  216. // other inline functions
  217. /////////////////////////////////////////////////////////////////////////////
  218. HRESULT GetDomainControllerName(
  219. IN ULONG ulFlags,
  220. OUT WCHAR ** ppszName
  221. );
  222. HRESULT CreateDialableAddressEnumerator(
  223. IN BSTR * begin,
  224. IN BSTR * end,
  225. OUT IEnumDialableAddrs ** ppIEnum
  226. );
  227. HRESULT CreateBstrCollection(
  228. IN long nSize,
  229. IN BSTR * begin,
  230. IN BSTR * end,
  231. OUT VARIANT * pVariant,
  232. CComEnumFlags flags
  233. );
  234. HRESULT CreateDirectoryObjectEnumerator(
  235. IN ITDirectoryObject ** begin,
  236. IN ITDirectoryObject ** end,
  237. OUT IEnumDirectoryObject ** ppIEnum
  238. );
  239. HRESULT CreateEmptyUser(
  240. IN BSTR pName,
  241. OUT ITDirectoryObject ** ppDirectoryObject
  242. );
  243. HRESULT CreateEmptyConference(
  244. IN BSTR pName,
  245. OUT ITDirectoryObject ** ppDirectoryObject
  246. );
  247. HRESULT CreateConferenceWithBlob(
  248. IN BSTR pName,
  249. IN BSTR pProtocol,
  250. IN BSTR pBlob,
  251. IN CHAR * pSecurityDescriptor,
  252. IN DWORD dwSDSize,
  253. OUT ITDirectoryObject ** ppDirectoryObject
  254. );
  255. HRESULT ResolveHostName(
  256. DWORD dwInterface,
  257. TCHAR * pHost,
  258. char ** pFullName,
  259. DWORD * pdwIP
  260. );
  261. int LookupILSServiceBegin(
  262. HANDLE * pHandle
  263. );
  264. int LookupILSServiceNext(
  265. HANDLE Handle,
  266. TCHAR * pBuf,
  267. DWORD * pdwBufSize,
  268. WORD * pwPort
  269. );
  270. int LookupILSServiceEnd(
  271. HANDLE Handle
  272. );
  273. void ipAddressToStringW(
  274. WCHAR * wszDest,
  275. DWORD dwAddress
  276. );
  277. #endif // __RENDEZVOUS_GENERAL__