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.

330 lines
7.9 KiB

  1. #ifndef _INC_DSKQUOTA_UTILS_H
  2. #define _INC_DSKQUOTA_UTILS_H
  3. ///////////////////////////////////////////////////////////////////////////////
  4. /* File: utils.h
  5. Description: Header for general utilities module. It is expected that
  6. windows.h is included before this header.
  7. Revision History:
  8. Date Description Programmer
  9. -------- --------------------------------------------------- ----------
  10. 06/06/96 Initial creation. BrianAu
  11. */
  12. ///////////////////////////////////////////////////////////////////////////////
  13. #ifndef _INC_DSKQUOTA_PRIVATE_H
  14. # include "private.h"
  15. #endif
  16. //
  17. // Convert a value to a "bool".
  18. // Lower-case "boolify" is intentional to enforce relationship
  19. // to type "bool".
  20. //
  21. template <class T>
  22. inline bool boolify(const T& x)
  23. {
  24. return !!x;
  25. }
  26. template <class T>
  27. const T&
  28. MAX(const T& a, const T& b)
  29. {
  30. return a > b ? a : b;
  31. }
  32. template <class T>
  33. const T&
  34. MIN(const T& a, const T& b)
  35. {
  36. return a < b ? a : b;
  37. }
  38. template <class T>
  39. void
  40. SWAP(T& a, T& b)
  41. {
  42. T temp(a);
  43. a = b;
  44. b = temp;
  45. }
  46. //
  47. // Trivial class for ensuring window redraw is restored in the case
  48. // of an exception.
  49. //
  50. class CAutoSetRedraw
  51. {
  52. public:
  53. CAutoSetRedraw(HWND hwnd)
  54. : m_hwnd(hwnd) { }
  55. CAutoSetRedraw(HWND hwnd, bool bSet)
  56. : m_hwnd(hwnd) { Set(bSet); }
  57. ~CAutoSetRedraw(void)
  58. { Set(true); }
  59. void Set(bool bSet)
  60. { SendMessage(m_hwnd, WM_SETREDRAW, (WPARAM)bSet, 0); }
  61. private:
  62. HWND m_hwnd;
  63. };
  64. //
  65. // Trivial class for ensuring window is enabled in the case
  66. // of an exception.
  67. //
  68. class CAutoWndEnable
  69. {
  70. public:
  71. CAutoWndEnable(HWND hwnd)
  72. : m_hwnd(hwnd) { }
  73. CAutoWndEnable(HWND hwnd, bool bEnable)
  74. : m_hwnd(hwnd) { Enable(bEnable); }
  75. ~CAutoWndEnable(void)
  76. { Enable(true); }
  77. void Enable(bool bEnable)
  78. { EnableWindow(m_hwnd, bEnable); }
  79. private:
  80. HWND m_hwnd;
  81. };
  82. //
  83. // Ensure NT handles are exception safe.
  84. //
  85. class CNtHandle
  86. {
  87. public:
  88. CNtHandle(HANDLE handle)
  89. : m_handle(handle) { }
  90. CNtHandle(void)
  91. : m_handle(NULL) { }
  92. ~CNtHandle(void)
  93. { Close(); }
  94. void Close(void)
  95. { if (m_handle) NtClose(m_handle); m_handle = NULL; }
  96. operator HANDLE() const
  97. { return m_handle; }
  98. HANDLE *HandlePtr(void)
  99. { DBGASSERT((NULL == m_handle)); return &m_handle; }
  100. private:
  101. HANDLE m_handle;
  102. //
  103. // Prevent copy.
  104. // This class is only intended for automatic handle cleanup.
  105. //
  106. CNtHandle(const CNtHandle& rhs);
  107. CNtHandle& operator = (const CNtHandle& rhs);
  108. };
  109. //
  110. // Ensure Win32 handles are exception safe.
  111. //
  112. class CWin32Handle
  113. {
  114. public:
  115. CWin32Handle(HANDLE handle)
  116. : m_handle(handle) { }
  117. CWin32Handle(void)
  118. : m_handle(NULL) { }
  119. ~CWin32Handle(void)
  120. { Close(); }
  121. void Close(void)
  122. { if (m_handle) CloseHandle(m_handle); m_handle = NULL; }
  123. operator HANDLE() const
  124. { return m_handle; }
  125. HANDLE *HandlePtr(void)
  126. { DBGASSERT((NULL == m_handle)); return &m_handle; }
  127. private:
  128. HANDLE m_handle;
  129. //
  130. // Prevent copy.
  131. // This class is only intended for automatic handle cleanup.
  132. //
  133. CWin32Handle(const CWin32Handle& rhs);
  134. CWin32Handle& operator = (const CWin32Handle& rhs);
  135. };
  136. //
  137. // Trivial inline class to automate the cleanup of a STGMEDIUM
  138. // structure.
  139. //
  140. class CStgMedium : public STGMEDIUM
  141. {
  142. public:
  143. CStgMedium(void)
  144. { tymed = TYMED_NULL; hGlobal = NULL; pUnkForRelease = NULL; }
  145. ~CStgMedium(void)
  146. { ReleaseStgMedium(this); }
  147. operator LPSTGMEDIUM(void)
  148. { return (LPSTGMEDIUM)this; }
  149. operator const STGMEDIUM& (void)
  150. { return (STGMEDIUM &)*this; }
  151. };
  152. //
  153. // On mounted volumes, the parsing name and display name are different.
  154. // The parsing name contains a GUID which means nothing to the user.
  155. // This class encapsulates both strings into a single class that can
  156. // be passed as a single object to functions requiring a volume
  157. // identifier.
  158. //
  159. // Here is an example of what the strings will contain:
  160. //
  161. // Mounted volume Non-mounted volume
  162. //
  163. // Display Label (C:\FOO) C:\
  164. // Parsing \\?\Volume\{GUID} C:\
  165. // FSPath C:\FOO C:\
  166. //
  167. // I've coded this using only CString object references to leverage
  168. // the reference counting of the CString class and minimize string copying.
  169. //
  170. class CVolumeID
  171. {
  172. public:
  173. CVolumeID(void)
  174. : m_bMountedVol(false) { }
  175. CVolumeID(
  176. const CString& strForParsing,
  177. const CString& strForDisplay,
  178. const CString& strFSPath
  179. ) : m_bMountedVol(false)
  180. { SetNames(strForParsing, strForDisplay, strFSPath); }
  181. ~CVolumeID(void) { };
  182. bool IsMountedVolume(void) const
  183. { return m_bMountedVol; }
  184. void SetNames(
  185. const CString& strForParsing,
  186. const CString& strForDisplay,
  187. const CString& strFSPath)
  188. { m_strForParsing = strForParsing;
  189. m_strFSPath = strFSPath;
  190. m_strForDisplay = strForDisplay;
  191. m_bMountedVol = !!(strForParsing != strForDisplay); }
  192. const CString& ForParsing(void) const
  193. { return m_strForParsing; }
  194. void ForParsing(CString *pstr) const
  195. { *pstr = m_strForParsing; }
  196. const CString& ForDisplay(void) const
  197. { return m_strForDisplay; }
  198. void ForDisplay(CString *pstr) const
  199. { *pstr = m_strForDisplay; }
  200. const CString& FSPath(void) const
  201. { return m_strFSPath; }
  202. void FSPath(CString *pstr) const
  203. { *pstr = m_strFSPath; }
  204. private:
  205. CString m_strForParsing;
  206. CString m_strForDisplay;
  207. CString m_strFSPath;
  208. bool m_bMountedVol;
  209. };
  210. //
  211. // Don't want to include dskquota.h for these.
  212. // Including it here places the CLSIDs and IIDs in the precompiled header
  213. // which screws up the declaration/definition of the GUIDs.
  214. //
  215. struct IDiskQuotaUser; // fwd decl.
  216. #define SIDLIST FILE_GET_QUOTA_INFORMATION
  217. #define PSIDLIST PFILE_GET_QUOTA_INFORMATION
  218. BOOL SidToString(
  219. PSID pSid,
  220. LPTSTR pszSid,
  221. LPDWORD pcchSid);
  222. BOOL SidToString(
  223. PSID pSid,
  224. LPTSTR *ppszSid);
  225. HRESULT
  226. CreateSidList(
  227. PSID *rgpSids,
  228. DWORD cpSids,
  229. PSIDLIST *ppSidList,
  230. LPDWORD pcbSidList);
  231. VOID MessageBoxNYI(VOID);
  232. inline INT DiskQuotaMsgBox(HWND hWndParent,
  233. LPCTSTR pszText,
  234. LPCTSTR pszTitle,
  235. UINT uType);
  236. INT DiskQuotaMsgBox(HWND hWndParent,
  237. UINT idMsgText,
  238. UINT idMsgTitle,
  239. UINT uType);
  240. INT DiskQuotaMsgBox(HWND hWndParent,
  241. UINT idMsgText,
  242. LPCTSTR pszTitle,
  243. UINT uType);
  244. INT DiskQuotaMsgBox(HWND hWndParent,
  245. LPCTSTR pszText,
  246. UINT idMsgTitle,
  247. UINT uType);
  248. LPTSTR StringDup(LPCTSTR pszSource);
  249. PSID SidDup(PSID pSid);
  250. BOOL UserIsAdministrator(IDiskQuotaUser *pUser);
  251. VOID CenterPopupWindow(HWND hwnd, HWND hwndParent = NULL);
  252. HRESULT CallRegInstall(HINSTANCE hInstance, LPSTR szSection);
  253. void GetDlgItemText(HWND hwnd, UINT idCtl, CString *pstrText);
  254. #endif // _INC_DSKQUOTA_UTILS_H