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.

256 lines
5.1 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1997 - 1999
  6. //
  7. // File: util.h
  8. //
  9. //--------------------------------------------------------------------------
  10. // Util.h : Definition of ds routines and classes
  11. //+-------------------------------------------------------------------------
  12. #ifndef __UTIL_H__
  13. #define __UTIL_H__
  14. #include <htmlhelp.h>
  15. //
  16. // Forward declarations
  17. //
  18. class CDSCookie;
  19. //
  20. // Common macros
  21. //
  22. #define ARRAYLEN(x) (sizeof(x) / sizeof((x)[0]))
  23. #define LENGTH(x) (sizeof(x)/sizeof(x[0]))
  24. /////////////////////////////////////////////////////////////////////////////////////
  25. // Variant helpers
  26. //
  27. HRESULT BinaryToVariant(DWORD Length, BYTE* pByte, VARIANT* lpVarDestObject);
  28. HRESULT HrVariantToStringList(const CComVariant& refvar, CStringList& refstringlist);
  29. HRESULT HrStringListToVariant(CComVariant& refvar, const CStringList& refstringlist);
  30. ///////////////////////////////////////////////////////////////////////////////////////
  31. BOOL LoadStringToTchar(int ids, PTSTR * pptstr);
  32. ///////////////////////////////////////////////////////////////////////////////////////
  33. // streaming helper functions
  34. //
  35. HRESULT SaveStringHelper(LPCWSTR pwsz, IStream* pStm);
  36. HRESULT LoadStringHelper(CString& sz, IStream* pStm);
  37. HRESULT SaveDWordHelper(IStream* pStm, DWORD dw);
  38. HRESULT LoadDWordHelper(IStream* pStm, DWORD* pdw);
  39. /////////////////////////////////////////////////////////////////////
  40. // CCommandLineOptions
  41. class CCommandLineOptions
  42. {
  43. public:
  44. CCommandLineOptions()
  45. {
  46. m_bInit = FALSE;
  47. #ifdef DBG
  48. m_bNoName = FALSE;
  49. #endif
  50. }
  51. void Initialize();
  52. LPCWSTR GetServerOverride()
  53. { return m_szOverrideServerName.IsEmpty() ? NULL : (LPCWSTR)m_szOverrideServerName;}
  54. LPCWSTR GetDomainOverride()
  55. { return m_szOverrideDomainName.IsEmpty() ? NULL : (LPCWSTR)m_szOverrideDomainName;}
  56. LPCWSTR GetRDNOverride()
  57. { return m_szOverrideRDN.IsEmpty() ? NULL : (LPCWSTR)m_szOverrideRDN;}
  58. LPCWSTR GetSavedQueriesXMLFile()
  59. { return m_szSavedQueriesXMLFile.IsEmpty() ? NULL : (LPCWSTR)m_szSavedQueriesXMLFile;}
  60. #ifdef DBG
  61. BOOL IsNoNameCommandLine() { return m_bNoName;}
  62. #endif
  63. private:
  64. BOOL m_bInit;
  65. CString m_szOverrideDomainName;
  66. CString m_szOverrideServerName;
  67. CString m_szOverrideRDN;
  68. CString m_szSavedQueriesXMLFile; // experiment for loading queries
  69. #ifdef DBG
  70. BOOL m_bNoName;
  71. #endif
  72. };
  73. /////////////////////////////////////////////////////////////////////
  74. // CSidHolder
  75. class CSidHolder
  76. {
  77. public:
  78. CSidHolder()
  79. {
  80. _Init();
  81. }
  82. ~CSidHolder()
  83. {
  84. _Free();
  85. }
  86. PSID Get()
  87. {
  88. return m_pSID;
  89. }
  90. BOOL Copy(PSID p)
  91. {
  92. _Free();
  93. return _Copy(p);
  94. }
  95. void Attach(PSID p, BOOL bLocalAlloc)
  96. {
  97. _Free();
  98. m_pSID = p;
  99. m_bLocalAlloc = bLocalAlloc;
  100. }
  101. void Clear()
  102. {
  103. _Free();
  104. }
  105. private:
  106. void _Init()
  107. {
  108. m_pSID = NULL;
  109. m_bLocalAlloc = TRUE;
  110. }
  111. void _Free()
  112. {
  113. if (m_pSID != NULL)
  114. {
  115. if (m_bLocalAlloc)
  116. ::LocalFree(m_pSID);
  117. else
  118. ::FreeSid(m_pSID);
  119. _Init();
  120. }
  121. }
  122. BOOL _Copy(PSID p)
  123. {
  124. if ( (p == NULL) || !::IsValidSid(p) )
  125. return FALSE;
  126. DWORD dwLen = ::GetLengthSid(p);
  127. PSID pNew = ::LocalAlloc(LPTR, dwLen);
  128. if (pNew == NULL)
  129. {
  130. return FALSE;
  131. }
  132. if (!::CopySid(dwLen, pNew, p))
  133. {
  134. ::LocalFree(pNew);
  135. return FALSE;
  136. }
  137. m_bLocalAlloc = TRUE;
  138. m_pSID = pNew;
  139. ASSERT(dwLen == ::GetLengthSid(m_pSID));
  140. ASSERT(memcmp(p, m_pSID, dwLen) == 0);
  141. return TRUE;
  142. }
  143. PSID m_pSID;
  144. BOOL m_bLocalAlloc;
  145. };
  146. //////////////////////////////////////////////////////////////////////////////
  147. // Security helpers
  148. class CSimpleSecurityDescriptorHolder
  149. {
  150. public:
  151. CSimpleSecurityDescriptorHolder()
  152. {
  153. m_pSD = NULL;
  154. }
  155. ~CSimpleSecurityDescriptorHolder()
  156. {
  157. if (m_pSD != NULL)
  158. {
  159. ::LocalFree(m_pSD);
  160. m_pSD = NULL;
  161. }
  162. }
  163. PSECURITY_DESCRIPTOR m_pSD;
  164. private:
  165. CSimpleSecurityDescriptorHolder(const CSimpleSecurityDescriptorHolder&)
  166. {}
  167. CSimpleSecurityDescriptorHolder& operator=(const CSimpleSecurityDescriptorHolder&) {}
  168. };
  169. /////////////////////////////////////////////////////////////////////
  170. // CSimpleAclHolder
  171. class CSimpleAclHolder
  172. {
  173. public:
  174. CSimpleAclHolder()
  175. {
  176. m_pAcl = NULL;
  177. }
  178. ~CSimpleAclHolder()
  179. {
  180. Clear();
  181. }
  182. void Clear()
  183. {
  184. if (m_pAcl != NULL)
  185. {
  186. ::LocalFree(m_pAcl);
  187. m_pAcl = NULL;
  188. }
  189. }
  190. PACL m_pAcl;
  191. };
  192. //////////////////////////////////////////////////////////////////////////////////
  193. // String Utilities
  194. //
  195. #if 0
  196. struct DATA_BLOB
  197. {
  198. DWORD cbData;
  199. BYTE *pbData;
  200. };
  201. #endif
  202. typedef DATA_BLOB TBLOB; // Binary large object
  203. void wtoli(LPCWSTR lpsz, LARGE_INTEGER& liOut);
  204. void litow(LARGE_INTEGER& li, CString& szResult);
  205. void GetCurrentTimeStampMinusInterval(DWORD dwDays,
  206. LARGE_INTEGER* pLI);
  207. // This wrapper function required to make prefast shut up when we are
  208. // initializing a critical section in a constructor.
  209. void ExceptionPropagatingInitializeCriticalSection(LPCRITICAL_SECTION critsec);
  210. #endif __UTIL_H__