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