ReactOS 0.4.15-dev-8021-g7ce96fd
atlfile.h
Go to the documentation of this file.
1/*
2* PROJECT: ReactOS ATL
3* LICENSE: GPL-2.0-or-later (https://spdx.org/licenses/GPL-2.0-or-later)
4* PURPOSE: ATL File implementation
5* COPYRIGHT: Copyright 2019 Mark Jansen (mark.jansen@reactos.org)
6*/
7
8#pragma once
9
10#include <atlbase.h>
11
12namespace ATL
13{
14
15//class CAtlFile: TODO
16// public CHandle
17//{
18//};
19
20
21//class CAtlTemporaryFile TODO
22//{
23//};
24
25
26
28{
29private:
30 void* m_pData;
35
36public:
42 {
44 }
45
47 {
48 Unmap();
49 }
50
52 {
53 HRESULT hr;
54
55 m_pData = NULL;
60
61 hr = CopyFrom(orig);
62 if (FAILED(hr))
63 AtlThrow(hr);
64 }
65
67 {
68 HRESULT hr;
69
70 hr = CopyFrom(orig);
71 if (FAILED(hr))
72 AtlThrow(hr);
73
74 return *this;
75 }
76
78 {
79 HRESULT hr = S_OK;
80
81 if (&orig == this)
82 return S_OK;
83
86 ATLASSERT(orig.m_pData != NULL);
87
88 m_nMappingSize = orig.m_nMappingSize;
89 m_nOffset.QuadPart = orig.m_nOffset.QuadPart;
90 m_dwViewDesiredAccess = orig.m_dwViewDesiredAccess;
91
93 {
95 if (!m_pData)
96 {
100 }
101 }
102 else
103 {
105 }
106
107 return hr;
108 }
109
112 SIZE_T nMappingSize = 0,
113 ULONGLONG nOffset = 0,
114 DWORD dwMappingProtection = PAGE_READONLY,
115 DWORD dwViewDesiredAccess = FILE_MAP_READ) noexcept
116 {
117 HRESULT hr = S_OK;
119
123
125 FileSize.QuadPart = nMappingSize > FileSize.QuadPart ? nMappingSize : FileSize.QuadPart;
126
128 if (m_hMapping)
129 {
130 m_nMappingSize = nMappingSize == 0 ? (SIZE_T)(FileSize.QuadPart - nOffset) : nMappingSize;
131 m_nOffset.QuadPart = nOffset;
132 m_dwViewDesiredAccess = dwViewDesiredAccess;
133
135 if (!m_pData)
136 {
140 }
141 }
142 else
143 {
145 }
146
147 return hr;
148 }
149
151 SIZE_T nMappingSize,
153 BOOL* pbAlreadyExisted = NULL,
155 DWORD dwMappingProtection = PAGE_READWRITE,
156 DWORD dwViewDesiredAccess = FILE_MAP_ALL_ACCESS) noexcept
157 {
158 HRESULT hr = S_OK;
160
161 ATLASSERT(nMappingSize > 0);
165
166 m_nMappingSize = nMappingSize;
167 m_dwViewDesiredAccess = dwViewDesiredAccess;
169 Size.QuadPart = nMappingSize;
170
171 m_hMapping = ::CreateFileMapping(NULL, lpsa, dwMappingProtection, Size.HighPart, Size.LowPart, szName);
172 if (m_hMapping != NULL)
173 {
174 if (pbAlreadyExisted)
175 *pbAlreadyExisted = GetLastError() == ERROR_ALREADY_EXISTS;
176
178 if (!m_pData)
179 {
183 }
184 }
185 else
186 {
188 }
189
190 return hr;
191 }
192
195 SIZE_T nMappingSize,
196 ULONGLONG nOffset = 0,
197 DWORD dwViewDesiredAccess = FILE_MAP_ALL_ACCESS) noexcept
198 {
199 HRESULT hr = S_OK;
200
204
205 m_nMappingSize = nMappingSize;
206 m_dwViewDesiredAccess = dwViewDesiredAccess;
207 m_nOffset.QuadPart = nOffset;
208
210 if (m_hMapping)
211 {
213 if (!m_pData)
214 {
218 }
219 }
220 else
221 {
223 }
224
225 return hr;
226 }
227
228 HRESULT Unmap() noexcept
229 {
230 HRESULT hr = S_OK;
231
232 if (m_pData)
233 {
236
237 m_pData = NULL;
238 }
239 if (m_hMapping)
240 {
241 // If we already had an error, do not overwrite it
244
246 }
247
248 return hr;
249 }
250
251 void* GetData() const noexcept
252 {
253 return m_pData;
254 }
255
256 HANDLE GetHandle() throw ()
257 {
258 return m_hMapping;
259 }
260
262 {
263 return m_nMappingSize;
264 }
265
266};
267
268
269template <typename T = char>
272{
273public:
274 operator T*() const noexcept
275 {
276 return reinterpret_cast<T*>(GetData());
277 }
278};
279
280
281}
#define ATLASSERT(x)
Definition: CComVariant.cpp:10
#define AtlThrow(x)
Definition: atldef.h:20
CAtlFileMappingBase() noexcept
Definition: atlfile.h:37
HRESULT Unmap() noexcept
Definition: atlfile.h:228
~CAtlFileMappingBase() noexcept
Definition: atlfile.h:46
HRESULT OpenMapping(LPCTSTR szName, SIZE_T nMappingSize, ULONGLONG nOffset=0, DWORD dwViewDesiredAccess=FILE_MAP_ALL_ACCESS) noexcept
Definition: atlfile.h:193
CAtlFileMappingBase(CAtlFileMappingBase &orig)
Definition: atlfile.h:51
CAtlFileMappingBase & operator=(CAtlFileMappingBase &orig)
Definition: atlfile.h:66
HRESULT MapFile(HANDLE hFile, SIZE_T nMappingSize=0, ULONGLONG nOffset=0, DWORD dwMappingProtection=PAGE_READONLY, DWORD dwViewDesiredAccess=FILE_MAP_READ) noexcept
Definition: atlfile.h:110
SIZE_T GetMappingSize() noexcept
Definition: atlfile.h:261
HRESULT MapSharedMem(SIZE_T nMappingSize, LPCTSTR szName, BOOL *pbAlreadyExisted=NULL, LPSECURITY_ATTRIBUTES lpsa=NULL, DWORD dwMappingProtection=PAGE_READWRITE, DWORD dwViewDesiredAccess=FILE_MAP_ALL_ACCESS) noexcept
Definition: atlfile.h:150
void * GetData() const noexcept
Definition: atlfile.h:251
HRESULT CopyFrom(CAtlFileMappingBase &orig) noexcept
Definition: atlfile.h:77
ULARGE_INTEGER m_nOffset
Definition: atlfile.h:33
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define CloseHandle
Definition: compat.h:739
#define PAGE_READONLY
Definition: compat.h:138
#define UnmapViewOfFile
Definition: compat.h:746
#define INVALID_HANDLE_VALUE
Definition: compat.h:731
#define GetCurrentProcess()
Definition: compat.h:759
#define FILE_MAP_READ
Definition: compat.h:776
#define MapViewOfFile
Definition: compat.h:745
DWORD WINAPI GetFileSize(HANDLE hFile, LPDWORD lpFileSizeHigh)
Definition: fileinfo.c:331
BOOL WINAPI DuplicateHandle(IN HANDLE hSourceProcessHandle, IN HANDLE hSourceHandle, IN HANDLE hTargetProcessHandle, OUT LPHANDLE lpTargetHandle, IN DWORD dwDesiredAccess, IN BOOL bInheritHandle, IN DWORD dwOptions)
Definition: handle.c:149
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
_Must_inspect_result_ _Out_ PLARGE_INTEGER FileSize
Definition: fsrtlfuncs.h:108
#define S_OK
Definition: intsafe.h:52
#define SUCCEEDED(hr)
Definition: intsafe.h:50
#define FAILED(hr)
Definition: intsafe.h:51
#define T
Definition: mbstring.h:31
#define ERROR_ALREADY_EXISTS
Definition: disk.h:80
_In_ HANDLE hFile
Definition: mswsock.h:90
Definition: rosdlgs.h:6
HRESULT AtlHresultFromLastError() noexcept
Definition: atlcomcli.h:56
#define PAGE_READWRITE
Definition: nt_native.h:1304
static const WCHAR szName[]
Definition: powrprof.c:45
HRESULT hr
Definition: shlfolder.c:183
$ULONG LowPart
Definition: ntbasedef.h:569
ULONGLONG QuadPart
Definition: ms-dtyp.idl:185
$ULONG HighPart
Definition: ntbasedef.h:570
ULONG_PTR SIZE_T
Definition: typedefs.h:80
uint64_t ULONGLONG
Definition: typedefs.h:67
LONGLONG QuadPart
Definition: typedefs.h:114
ULONG LowPart
Definition: typedefs.h:106
_Must_inspect_result_ _In_ WDFDEVICE _In_ PWDF_DEVICE_PROPERTY_DATA _In_ DEVPROPTYPE _In_ ULONG Size
Definition: wdfdevice.h:4533
DWORD WINAPI GetLastError(void)
Definition: except.c:1042
#define CreateFileMapping
Definition: winbase.h:3750
#define FILE_MAP_ALL_ACCESS
Definition: winbase.h:156
#define OpenFileMapping
Definition: winbase.h:3887
#define DUPLICATE_SAME_ACCESS
const CHAR * LPCTSTR
Definition: xmlstorage.h:193
#define const
Definition: zconf.h:233