file_mapping.hpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. * Copyright 2010 Vicente J. Botet Escriba
  3. * Copyright 2015 Andrey Semashev
  4. * Copyright 2016 Jorge Lodos
  5. * Copyright 2017 James E. King, III
  6. *
  7. * Distributed under the Boost Software License, Version 1.0.
  8. * See http://www.boost.org/LICENSE_1_0.txt
  9. */
  10. #ifndef BOOST_WINAPI_FILE_MAPPING_HPP_INCLUDED_
  11. #define BOOST_WINAPI_FILE_MAPPING_HPP_INCLUDED_
  12. #include <boost/winapi/basic_types.hpp>
  13. #include <boost/winapi/detail/header.hpp>
  14. #ifdef BOOST_HAS_PRAGMA_ONCE
  15. #pragma once
  16. #endif
  17. /*
  18. * UWP:
  19. * API SDK 8 SDK 10
  20. * CreateFileMapping DESKTOP - DESKTOP | SYSTEM
  21. * FlushViewOfFile APP - APP | SYSTEM
  22. * MapViewOfFile DESKTOP - DESKTOP | SYSTEM
  23. * MapViewOfFileEx DESKTOP - DESKTOP | SYSTEM
  24. * OpenFileMapping DESKTOP - DESKTOP | SYSTEM
  25. * UnmapViewOfFile APP - APP | SYSTEM
  26. */
  27. #if !defined( BOOST_USE_WINDOWS_H )
  28. extern "C" {
  29. #if BOOST_WINAPI_PARTITION_DESKTOP
  30. #if !defined( BOOST_NO_ANSI_APIS )
  31. BOOST_WINAPI_IMPORT boost::winapi::HANDLE_ BOOST_WINAPI_WINAPI_CC
  32. CreateFileMappingA(
  33. boost::winapi::HANDLE_ hFile,
  34. ::_SECURITY_ATTRIBUTES* lpFileMappingAttributes,
  35. boost::winapi::DWORD_ flProtect,
  36. boost::winapi::DWORD_ dwMaximumSizeHigh,
  37. boost::winapi::DWORD_ dwMaximumSizeLow,
  38. boost::winapi::LPCSTR_ lpName);
  39. BOOST_WINAPI_IMPORT boost::winapi::HANDLE_ BOOST_WINAPI_WINAPI_CC
  40. OpenFileMappingA(
  41. boost::winapi::DWORD_ dwDesiredAccess,
  42. boost::winapi::BOOL_ bInheritHandle,
  43. boost::winapi::LPCSTR_ lpName);
  44. #endif // !defined( BOOST_NO_ANSI_APIS )
  45. #endif // BOOST_WINAPI_PARTITION_DESKTOP
  46. #if BOOST_WINAPI_PARTITION_DESKTOP || BOOST_WINAPI_PARTITION_SYSTEM
  47. BOOST_WINAPI_IMPORT_EXCEPT_WM boost::winapi::HANDLE_ BOOST_WINAPI_WINAPI_CC
  48. CreateFileMappingW(
  49. boost::winapi::HANDLE_ hFile,
  50. ::_SECURITY_ATTRIBUTES* lpFileMappingAttributes,
  51. boost::winapi::DWORD_ flProtect,
  52. boost::winapi::DWORD_ dwMaximumSizeHigh,
  53. boost::winapi::DWORD_ dwMaximumSizeLow,
  54. boost::winapi::LPCWSTR_ lpName);
  55. BOOST_WINAPI_IMPORT_EXCEPT_WM boost::winapi::LPVOID_ BOOST_WINAPI_WINAPI_CC
  56. MapViewOfFile(
  57. boost::winapi::HANDLE_ hFileMappingObject,
  58. boost::winapi::DWORD_ dwDesiredAccess,
  59. boost::winapi::DWORD_ dwFileOffsetHigh,
  60. boost::winapi::DWORD_ dwFileOffsetLow,
  61. boost::winapi::SIZE_T_ dwNumberOfBytesToMap);
  62. BOOST_WINAPI_IMPORT boost::winapi::LPVOID_ BOOST_WINAPI_WINAPI_CC
  63. MapViewOfFileEx(
  64. boost::winapi::HANDLE_ hFileMappingObject,
  65. boost::winapi::DWORD_ dwDesiredAccess,
  66. boost::winapi::DWORD_ dwFileOffsetHigh,
  67. boost::winapi::DWORD_ dwFileOffsetLow,
  68. boost::winapi::SIZE_T_ dwNumberOfBytesToMap,
  69. boost::winapi::LPVOID_ lpBaseAddress);
  70. BOOST_WINAPI_IMPORT boost::winapi::HANDLE_ BOOST_WINAPI_WINAPI_CC
  71. OpenFileMappingW(
  72. boost::winapi::DWORD_ dwDesiredAccess,
  73. boost::winapi::BOOL_ bInheritHandle,
  74. boost::winapi::LPCWSTR_ lpName);
  75. #endif // BOOST_WINAPI_PARTITION_DESKTOP || BOOST_WINAPI_PARTITION_SYSTEM
  76. #if BOOST_WINAPI_PARTITION_APP || BOOST_WINAPI_PARTITION_SYSTEM
  77. BOOST_WINAPI_IMPORT_EXCEPT_WM boost::winapi::BOOL_ BOOST_WINAPI_WINAPI_CC
  78. FlushViewOfFile(
  79. boost::winapi::LPCVOID_ lpBaseAddress,
  80. boost::winapi::SIZE_T_ dwNumberOfBytesToFlush);
  81. BOOST_WINAPI_IMPORT_EXCEPT_WM boost::winapi::BOOL_ BOOST_WINAPI_WINAPI_CC
  82. UnmapViewOfFile(boost::winapi::LPCVOID_ lpBaseAddress);
  83. #endif // BOOST_WINAPI_PARTITION_APP || BOOST_WINAPI_PARTITION_SYSTEM
  84. } // extern "C"
  85. #endif // !defined( BOOST_USE_WINDOWS_H )
  86. namespace boost {
  87. namespace winapi {
  88. #if defined( BOOST_USE_WINDOWS_H )
  89. BOOST_CONSTEXPR_OR_CONST DWORD_ SEC_FILE_ = SEC_FILE;
  90. BOOST_CONSTEXPR_OR_CONST DWORD_ SEC_IMAGE_ = SEC_IMAGE;
  91. BOOST_CONSTEXPR_OR_CONST DWORD_ SEC_RESERVE_ = SEC_RESERVE;
  92. BOOST_CONSTEXPR_OR_CONST DWORD_ SEC_COMMIT_ = SEC_COMMIT;
  93. BOOST_CONSTEXPR_OR_CONST DWORD_ SEC_NOCACHE_ = SEC_NOCACHE;
  94. // These permission flags are undocumented and some of them are equivalent to the FILE_MAP_* flags.
  95. // SECTION_QUERY enables NtQuerySection.
  96. // http://undocumented.ntinternals.net/index.html?page=UserMode%2FUndocumented%20Functions%2FNT%20Objects%2FSection%2FNtQuerySection.html
  97. BOOST_CONSTEXPR_OR_CONST DWORD_ SECTION_QUERY_ = SECTION_QUERY;
  98. BOOST_CONSTEXPR_OR_CONST DWORD_ SECTION_MAP_WRITE_ = SECTION_MAP_WRITE;
  99. BOOST_CONSTEXPR_OR_CONST DWORD_ SECTION_MAP_READ_ = SECTION_MAP_READ;
  100. BOOST_CONSTEXPR_OR_CONST DWORD_ SECTION_MAP_EXECUTE_ = SECTION_MAP_EXECUTE;
  101. BOOST_CONSTEXPR_OR_CONST DWORD_ SECTION_EXTEND_SIZE_ = SECTION_EXTEND_SIZE;
  102. BOOST_CONSTEXPR_OR_CONST DWORD_ SECTION_ALL_ACCESS_ = SECTION_ALL_ACCESS;
  103. BOOST_CONSTEXPR_OR_CONST DWORD_ FILE_MAP_COPY_ = FILE_MAP_COPY;
  104. BOOST_CONSTEXPR_OR_CONST DWORD_ FILE_MAP_WRITE_ = FILE_MAP_WRITE;
  105. BOOST_CONSTEXPR_OR_CONST DWORD_ FILE_MAP_READ_ = FILE_MAP_READ;
  106. BOOST_CONSTEXPR_OR_CONST DWORD_ FILE_MAP_ALL_ACCESS_ = FILE_MAP_ALL_ACCESS;
  107. #else // defined( BOOST_USE_WINDOWS_H )
  108. BOOST_CONSTEXPR_OR_CONST DWORD_ SEC_FILE_ = 0x800000;
  109. BOOST_CONSTEXPR_OR_CONST DWORD_ SEC_IMAGE_ = 0x1000000;
  110. BOOST_CONSTEXPR_OR_CONST DWORD_ SEC_RESERVE_ = 0x4000000;
  111. BOOST_CONSTEXPR_OR_CONST DWORD_ SEC_COMMIT_ = 0x8000000;
  112. BOOST_CONSTEXPR_OR_CONST DWORD_ SEC_NOCACHE_ = 0x10000000;
  113. // These permission flags are undocumented and some of them are equivalent to the FILE_MAP_* flags.
  114. // SECTION_QUERY enables NtQuerySection.
  115. // http://undocumented.ntinternals.net/index.html?page=UserMode%2FUndocumented%20Functions%2FNT%20Objects%2FSection%2FNtQuerySection.html
  116. BOOST_CONSTEXPR_OR_CONST DWORD_ SECTION_QUERY_ = 0x00000001;
  117. BOOST_CONSTEXPR_OR_CONST DWORD_ SECTION_MAP_WRITE_ = 0x00000002;
  118. BOOST_CONSTEXPR_OR_CONST DWORD_ SECTION_MAP_READ_ = 0x00000004;
  119. BOOST_CONSTEXPR_OR_CONST DWORD_ SECTION_MAP_EXECUTE_ = 0x00000008;
  120. BOOST_CONSTEXPR_OR_CONST DWORD_ SECTION_EXTEND_SIZE_ = 0x00000010;
  121. BOOST_CONSTEXPR_OR_CONST DWORD_ SECTION_ALL_ACCESS_ = 0x000F001F; // STANDARD_RIGHTS_REQUIRED | SECTION_*
  122. BOOST_CONSTEXPR_OR_CONST DWORD_ FILE_MAP_COPY_ = SECTION_QUERY_;
  123. BOOST_CONSTEXPR_OR_CONST DWORD_ FILE_MAP_WRITE_ = SECTION_MAP_WRITE_;
  124. BOOST_CONSTEXPR_OR_CONST DWORD_ FILE_MAP_READ_ = SECTION_MAP_READ_;
  125. BOOST_CONSTEXPR_OR_CONST DWORD_ FILE_MAP_ALL_ACCESS_ = SECTION_ALL_ACCESS_;
  126. #endif // defined( BOOST_USE_WINDOWS_H )
  127. // These constants are not defined in Windows SDK up until the one shipped with MSVC 8 and MinGW (as of 2016-02-14)
  128. BOOST_CONSTEXPR_OR_CONST DWORD_ SECTION_MAP_EXECUTE_EXPLICIT_ = 0x00000020; // not included in SECTION_ALL_ACCESS
  129. BOOST_CONSTEXPR_OR_CONST DWORD_ FILE_MAP_EXECUTE_ = SECTION_MAP_EXECUTE_EXPLICIT_; // not included in FILE_MAP_ALL_ACCESS
  130. // These constants are not defined in Windows SDK up until 6.0A and MinGW (as of 2016-02-14)
  131. BOOST_CONSTEXPR_OR_CONST DWORD_ SEC_PROTECTED_IMAGE_ = 0x2000000;
  132. BOOST_CONSTEXPR_OR_CONST DWORD_ SEC_WRITECOMBINE_ = 0x40000000;
  133. BOOST_CONSTEXPR_OR_CONST DWORD_ SEC_LARGE_PAGES_ = 0x80000000;
  134. BOOST_CONSTEXPR_OR_CONST DWORD_ SEC_IMAGE_NO_EXECUTE_ = (SEC_IMAGE_ | SEC_NOCACHE_);
  135. #if BOOST_WINAPI_PARTITION_DESKTOP
  136. #if !defined( BOOST_NO_ANSI_APIS )
  137. BOOST_FORCEINLINE HANDLE_ CreateFileMappingA(
  138. HANDLE_ hFile,
  139. SECURITY_ATTRIBUTES_* lpFileMappingAttributes,
  140. DWORD_ flProtect,
  141. DWORD_ dwMaximumSizeHigh,
  142. DWORD_ dwMaximumSizeLow,
  143. LPCSTR_ lpName)
  144. {
  145. return ::CreateFileMappingA(
  146. hFile,
  147. reinterpret_cast< ::_SECURITY_ATTRIBUTES* >(lpFileMappingAttributes),
  148. flProtect,
  149. dwMaximumSizeHigh,
  150. dwMaximumSizeLow,
  151. lpName);
  152. }
  153. BOOST_FORCEINLINE HANDLE_ create_file_mapping(
  154. HANDLE_ hFile,
  155. SECURITY_ATTRIBUTES_* lpFileMappingAttributes,
  156. DWORD_ flProtect,
  157. DWORD_ dwMaximumSizeHigh,
  158. DWORD_ dwMaximumSizeLow,
  159. LPCSTR_ lpName)
  160. {
  161. return ::CreateFileMappingA(
  162. hFile,
  163. reinterpret_cast< ::_SECURITY_ATTRIBUTES* >(lpFileMappingAttributes),
  164. flProtect,
  165. dwMaximumSizeHigh,
  166. dwMaximumSizeLow,
  167. lpName);
  168. }
  169. using ::OpenFileMappingA;
  170. BOOST_FORCEINLINE HANDLE_ open_file_mapping(DWORD_ dwDesiredAccess, BOOL_ bInheritHandle, LPCSTR_ lpName)
  171. {
  172. return ::OpenFileMappingA(dwDesiredAccess, bInheritHandle, lpName);
  173. }
  174. #endif
  175. #endif // BOOST_WINAPI_PARTITION_DESKTOP
  176. #if BOOST_WINAPI_PARTITION_DESKTOP || BOOST_WINAPI_PARTITION_SYSTEM
  177. BOOST_FORCEINLINE HANDLE_ CreateFileMappingW(
  178. HANDLE_ hFile,
  179. SECURITY_ATTRIBUTES_* lpFileMappingAttributes,
  180. DWORD_ flProtect,
  181. DWORD_ dwMaximumSizeHigh,
  182. DWORD_ dwMaximumSizeLow,
  183. LPCWSTR_ lpName)
  184. {
  185. return ::CreateFileMappingW(
  186. hFile,
  187. reinterpret_cast< ::_SECURITY_ATTRIBUTES* >(lpFileMappingAttributes),
  188. flProtect,
  189. dwMaximumSizeHigh,
  190. dwMaximumSizeLow,
  191. lpName);
  192. }
  193. BOOST_FORCEINLINE HANDLE_ create_file_mapping(
  194. HANDLE_ hFile,
  195. SECURITY_ATTRIBUTES_* lpFileMappingAttributes,
  196. DWORD_ flProtect,
  197. DWORD_ dwMaximumSizeHigh,
  198. DWORD_ dwMaximumSizeLow,
  199. LPCWSTR_ lpName)
  200. {
  201. return ::CreateFileMappingW(
  202. hFile,
  203. reinterpret_cast< ::_SECURITY_ATTRIBUTES* >(lpFileMappingAttributes),
  204. flProtect,
  205. dwMaximumSizeHigh,
  206. dwMaximumSizeLow,
  207. lpName);
  208. }
  209. using ::MapViewOfFile;
  210. using ::MapViewOfFileEx;
  211. using ::OpenFileMappingW;
  212. BOOST_FORCEINLINE HANDLE_ open_file_mapping(DWORD_ dwDesiredAccess, BOOL_ bInheritHandle, LPCWSTR_ lpName)
  213. {
  214. return ::OpenFileMappingW(dwDesiredAccess, bInheritHandle, lpName);
  215. }
  216. #endif // BOOST_WINAPI_PARTITION_DESKTOP || BOOST_WINAPI_PARTITION_SYSTEM
  217. #if BOOST_WINAPI_PARTITION_APP || BOOST_WINAPI_PARTITION_SYSTEM
  218. using ::FlushViewOfFile;
  219. using ::UnmapViewOfFile;
  220. #endif // BOOST_WINAPI_PARTITION_APP || BOOST_WINAPI_PARTITION_SYSTEM
  221. }
  222. }
  223. #include <boost/winapi/detail/footer.hpp>
  224. #endif // BOOST_WINAPI_FILE_MAPPING_HPP_INCLUDED_