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.

268 lines
8.1 KiB

  1. #pragma once
  2. template <void* const* invalidValue, typename Closer>
  3. class CHandleTemplate
  4. {
  5. public:
  6. // void* instead of HANDLE to fudge views
  7. // HANDLE is void*
  8. CHandleTemplate(const void* handle = *invalidValue);
  9. ~CHandleTemplate();
  10. void Close();
  11. void* Detach();
  12. void operator=(const void*);
  13. operator void*();
  14. operator const void*() const;
  15. void* m_handle;
  16. private:
  17. CHandleTemplate(const CHandleTemplate&); // deliberately not implemented
  18. void operator=(const CHandleTemplate&); // deliberately not implemented
  19. };
  20. __declspec(selectany) extern void* const hInvalidValue = INVALID_HANDLE_VALUE;
  21. __declspec(selectany) extern void* const hNull = NULL;
  22. class COperatorDeregisterEventSource
  23. {
  24. public: void operator()(void* handle) const;
  25. };
  26. class COperatorCloseEventLog
  27. {
  28. public: void operator()(void* handle) const;
  29. };
  30. class COperatorCloseHandle
  31. {
  32. public: void operator()(void* handle) const;
  33. };
  34. class COperatorFindClose
  35. {
  36. public: void operator()(void* handle) const;
  37. };
  38. class COperatorUnmapViewOfFile
  39. {
  40. public: void operator()(void* handle) const;
  41. };
  42. class COperatorRegCloseKey
  43. {
  44. public: void operator()(void* handle) const;
  45. };
  46. class CFindFile : public CHandleTemplate<&hInvalidValue, COperatorFindClose>
  47. {
  48. private:
  49. typedef CHandleTemplate<&hInvalidValue, COperatorFindClose> Base;
  50. public:
  51. CFindFile() { }
  52. CFindFile(void* handle) : Base(handle) { }
  53. CFindFile(PCSTR nameOrWildcard, WIN32_FIND_DATA*);
  54. HRESULT HrCreate(PCSTR nameOrWildcard, WIN32_FIND_DATA*);
  55. void VCreate(PCSTR nameOrWildcard, WIN32_FIND_DATA*);
  56. void operator=(void* v) { Base::operator=(v); }
  57. };
  58. class CFusionFile : public CHandleTemplate<&hInvalidValue, COperatorCloseHandle>
  59. {
  60. private:
  61. typedef CHandleTemplate<&hInvalidValue, COperatorCloseHandle> Base;
  62. public:
  63. CFusionFile() { }
  64. CFusionFile(void* handle) : Base(handle) { }
  65. CFusionFile(PCSTR name, DWORD access, DWORD share, DWORD openOrCreate);
  66. HRESULT HrCreate(PCSTR name, DWORD access, DWORD share, DWORD openOrCreate);
  67. void VCreate(PCSTR name, DWORD access, DWORD share, DWORD openOrCreate);
  68. __int64 GetSize() const;
  69. void operator=(void* v) { Base::operator=(v); }
  70. };
  71. class CFileMapping : public CHandleTemplate<&hNull, COperatorCloseHandle>
  72. {
  73. private:
  74. typedef CHandleTemplate<&hNull, COperatorCloseHandle> Base;
  75. public:
  76. CFileMapping() { }
  77. CFileMapping(void* handle) : Base(handle) { }
  78. CFileMapping(void* file, DWORD flProtect, __int64 maximumSize=0, PCSTR name=0);
  79. void VCreate(void* file, DWORD flProtect, __int64 maximumSize=0, PCSTR name=0);
  80. HRESULT HrCreate(void* file, DWORD flProtect, __int64 maximumSize=0, PCSTR name=0);
  81. void operator=(void* v) { Base::operator=(v); }
  82. };
  83. class CMappedViewOfFile : public CHandleTemplate<&hNull, COperatorUnmapViewOfFile>
  84. {
  85. private:
  86. typedef CHandleTemplate<&hNull, COperatorUnmapViewOfFile> Base;
  87. public:
  88. CMappedViewOfFile() { }
  89. CMappedViewOfFile(void* handle) : Base(handle) { }
  90. CMappedViewOfFile(void* fileMapping, DWORD access, __int64 offset=0, SIZE_T size=0);
  91. void VCreate(void* fileMapping, DWORD access, __int64 offset=0, SIZE_T size=0);
  92. HRESULT HrCreate(void* fileMapping, DWORD access, __int64 offset=0, SIZE_T size=0);
  93. void operator=(void* v) { Base::operator=(v); }
  94. };
  95. /*--------------------------------------------------------------------------
  96. --------------------------------------------------------------------------*/
  97. inline CFindFile::CFindFile(PCSTR nameOrWildcard, WIN32_FIND_DATA* data)
  98. : Base(INVALID_HANDLE_VALUE)
  99. {
  100. VCreate(nameOrWildcard, data);
  101. }
  102. inline HRESULT CFindFile::HrCreate(PCSTR nameOrWildcard, WIN32_FIND_DATA* data)
  103. {
  104. HANDLE hTemp = FindFirstFile(nameOrWildcard, data);
  105. if (hTemp == INVALID_HANDLE_VALUE)
  106. return HRESULT_FROM_WIN32(GetLastError());
  107. operator=(hTemp);
  108. return S_OK;
  109. }
  110. inline void CFindFile::VCreate(PCSTR nameOrWildcard, WIN32_FIND_DATA* data)
  111. {
  112. CheckHresult(HrCreate(nameOrWildcard, data));
  113. }
  114. inline HRESULT CFusionFile::HrCreate(PCSTR name, DWORD access, DWORD share, DWORD openOrCreate)
  115. {
  116. HANDLE hTemp = CreateFile(name, access, share, NULL, openOrCreate, FILE_ATTRIBUTE_NORMAL, NULL);
  117. if (hTemp == INVALID_HANDLE_VALUE)
  118. return HRESULT_FROM_WIN32(GetLastError());
  119. operator=(hTemp);
  120. return S_OK;
  121. }
  122. inline CFusionFile::CFusionFile(PCSTR name, DWORD access, DWORD share, DWORD openOrCreate)
  123. : Base(INVALID_HANDLE_VALUE)
  124. {
  125. VCreate(name, access, share, openOrCreate);
  126. }
  127. inline VOID CFusionFile::VCreate(PCSTR name, DWORD access, DWORD share, DWORD openOrCreate)
  128. {
  129. CheckHresult(HrCreate(name, access, share, openOrCreate));
  130. }
  131. inline __int64 CFusionFile::GetSize() const
  132. {
  133. DWORD highPart = 0;
  134. DWORD lastError = NO_ERROR;
  135. DWORD lowPart = GetFileSize(m_handle, &highPart);
  136. if (lowPart == INVALID_FILE_SIZE && (lastError = GetLastError()) != NO_ERROR)
  137. {
  138. ThrowHresult(HRESULT_FROM_WIN32(lastError));
  139. }
  140. LARGE_INTEGER liSize;
  141. liSize.LowPart = lowPart;
  142. liSize.HighPart = highPart;
  143. return liSize.QuadPart;
  144. }
  145. inline HRESULT CFileMapping::HrCreate(void* file, DWORD flProtect, __int64 maximumSize, PCSTR name)
  146. {
  147. LARGE_INTEGER liMaximumSize;
  148. liMaximumSize.QuadPart = maximumSize;
  149. HANDLE hTemp = CreateFileMapping(file, NULL, flProtect, liMaximumSize.HighPart, liMaximumSize.LowPart, name);
  150. if (hTemp == NULL)
  151. return HRESULT_FROM_WIN32(GetLastError());
  152. Base::operator=(hTemp);
  153. return S_OK;
  154. }
  155. inline CFileMapping::CFileMapping(void* file, DWORD flProtect, __int64 maximumSize, PCSTR name)
  156. : Base(NULL)
  157. {
  158. VCreate(file, flProtect, maximumSize, name);
  159. }
  160. inline void CFileMapping::VCreate(void* file, DWORD flProtect, __int64 maximumSize, PCSTR name)
  161. {
  162. CheckHresult(HrCreate(file, flProtect, maximumSize, name));
  163. }
  164. inline CMappedViewOfFile::CMappedViewOfFile(void* fileMapping, DWORD access, __int64 offset, SIZE_T size)
  165. : Base(NULL)
  166. {
  167. VCreate(fileMapping, access, offset, size);
  168. }
  169. inline void CMappedViewOfFile::VCreate(void* fileMapping, DWORD access, __int64 offset, SIZE_T size)
  170. {
  171. CheckHresult(HrCreate(fileMapping, access, offset, size));
  172. }
  173. inline HRESULT CMappedViewOfFile::HrCreate(void* fileMapping, DWORD access, __int64 offset, SIZE_T size)
  174. {
  175. LARGE_INTEGER liOffset;
  176. liOffset.QuadPart = offset;
  177. void* pvTemp = MapViewOfFile(fileMapping, access, liOffset.HighPart, liOffset.LowPart, size);
  178. if (pvTemp == NULL)
  179. return HRESULT_FROM_WIN32(GetLastError());
  180. Base::operator=(pvTemp);
  181. return S_OK;
  182. }
  183. inline void COperatorCloseHandle::operator()(void* handle) const { CloseHandle(handle); }
  184. inline void COperatorFindClose::operator()(void* handle) const { FindClose(handle); }
  185. inline void COperatorUnmapViewOfFile::operator()(void* handle) const { UnmapViewOfFile(handle); }
  186. inline void COperatorRegCloseKey::operator()(void* handle) const { RegCloseKey(reinterpret_cast<HKEY>(handle)); }
  187. inline void COperatorCloseEventLog::operator()(void* handle) const { CloseEventLog(handle); }
  188. inline void COperatorDeregisterEventSource::operator()(void* handle) const { DeregisterEventSource(handle); }
  189. template <void* const* invalidValue, typename Closer>
  190. CHandleTemplate<invalidValue, Closer>::CHandleTemplate(const void* handle)
  191. : m_handle(const_cast<void*>(handle))
  192. {
  193. }
  194. template <void* const* invalidValue, typename Closer>
  195. void* CHandleTemplate<invalidValue, Closer>::Detach()
  196. {
  197. void* handle = m_handle;
  198. m_handle = *invalidValue;
  199. return handle;
  200. }
  201. template <void* const* invalidValue, typename Closer>
  202. void CHandleTemplate<invalidValue, Closer>::operator=(const void* handle)
  203. {
  204. m_handle = const_cast<void*>(handle);
  205. }
  206. template <void* const* invalidValue, typename Closer>
  207. void CHandleTemplate<invalidValue, Closer>::Close()
  208. {
  209. void* handle = Detach();
  210. if (handle != *invalidValue)
  211. {
  212. Closer close;
  213. close(handle);
  214. }
  215. }
  216. template <void* const* invalidValue, typename Closer>
  217. CHandleTemplate<invalidValue, Closer>::~CHandleTemplate()
  218. {
  219. Close();
  220. }
  221. template <void* const* invalidValue, typename Closer>
  222. CHandleTemplate<invalidValue, Closer>::operator void*()
  223. {
  224. return m_handle;
  225. }
  226. template <void* const* invalidValue, typename Closer>
  227. CHandleTemplate<invalidValue, Closer>::operator const void*() const
  228. {
  229. return m_handle;
  230. }