ReactOS  0.4.13-dev-249-gcba1a2f
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 
12 namespace ATL
13 {
14 
15 //class CAtlFile: TODO
16 // public CHandle
17 //{
18 //};
19 
20 
21 //class CAtlTemporaryFile TODO
22 //{
23 //};
24 
25 
26 
28 {
29 private:
30  void* m_pData;
35 
36 public:
38  :m_pData(NULL)
39  ,m_nMappingSize(0)
42  {
43  m_nOffset.QuadPart = 0;
44  }
45 
47  {
48  Unmap();
49  }
50 
52  {
53  HRESULT hr;
54 
55  m_pData = NULL;
56  m_nMappingSize = 0;
57  m_hMapping = NULL;
59  m_nOffset.QuadPart = 0;
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  {
99  m_hMapping = NULL;
100  }
101  }
102  else
103  {
105  }
106 
107  return hr;
108  }
109 
111  HANDLE hFile,
112  SIZE_T nMappingSize = 0,
113  ULONGLONG nOffset = 0,
114  DWORD dwMappingProtection = PAGE_READONLY,
115  DWORD dwViewDesiredAccess = FILE_MAP_READ) throw()
116  {
117  HRESULT hr = S_OK;
119 
121  ATLASSERT(m_pData == NULL);
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  {
139  m_hMapping = NULL;
140  }
141  }
142  else
143  {
145  }
146 
147  return hr;
148  }
149 
151  SIZE_T nMappingSize,
152  LPCTSTR szName,
153  BOOL* pbAlreadyExisted = NULL,
155  DWORD dwMappingProtection = PAGE_READWRITE,
156  DWORD dwViewDesiredAccess = FILE_MAP_ALL_ACCESS) throw()
157  {
158  HRESULT hr = S_OK;
160 
161  ATLASSERT(nMappingSize > 0);
162  ATLASSERT(szName != NULL);
163  ATLASSERT(m_pData == NULL);
165 
166  m_nMappingSize = nMappingSize;
167  m_dwViewDesiredAccess = dwViewDesiredAccess;
168  m_nOffset.QuadPart = 0;
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  {
182  m_hMapping = NULL;
183  }
184  }
185  else
186  {
188  }
189 
190  return hr;
191  }
192 
194  LPCTSTR szName,
195  SIZE_T nMappingSize,
196  ULONGLONG nOffset = 0,
197  DWORD dwViewDesiredAccess = FILE_MAP_ALL_ACCESS) throw()
198  {
199  HRESULT hr = S_OK;
200 
201  ATLASSERT(szName != NULL);
202  ATLASSERT(m_pData == NULL);
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  {
217  m_hMapping = NULL;
218  }
219  }
220  else
221  {
223  }
224 
225  return hr;
226  }
227 
228  HRESULT Unmap() throw()
229  {
230  HRESULT hr = S_OK;
231 
232  if (m_pData)
233  {
234  if (!::UnmapViewOfFile(m_pData))
236 
237  m_pData = NULL;
238  }
239  if (m_hMapping)
240  {
241  // If we already had an error, do not overwrite it
242  if (!::CloseHandle(m_hMapping) && SUCCEEDED(hr))
244 
245  m_hMapping = NULL;
246  }
247 
248  return hr;
249  }
250 
251  void* GetData() const throw()
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 
269 template <typename T = char>
271  public CAtlFileMappingBase
272 {
273 public:
274  operator T*() const throw()
275  {
276  return reinterpret_cast<T*>(GetData());
277  }
278 };
279 
280 
281 }
#define TRUE
Definition: types.h:120
const CHAR * LPCTSTR
Definition: xmlstorage.h:193
ULARGE_INTEGER m_nOffset
Definition: atlfile.h:33
#define CloseHandle
Definition: compat.h:398
#define MapViewOfFile
Definition: compat.h:402
HRESULT hr
Definition: shlfolder.c:183
#define ATLASSERT(x)
Definition: CComVariant.cpp:9
CAtlFileMappingBase(CAtlFileMappingBase &orig)
Definition: atlfile.h:51
#define CreateFileMapping
Definition: winbase.h:3564
#define INVALID_HANDLE_VALUE
Definition: compat.h:391
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1059
$ULONG LowPart
Definition: ntbasedef.h:576
CAtlFileMappingBase & operator=(CAtlFileMappingBase &orig)
Definition: atlfile.h:66
#define T
Definition: mbstring.h:31
#define AtlThrow(x)
Definition: atldef.h:20
#define DUPLICATE_SAME_ACCESS
HRESULT MapFile(HANDLE hFile, SIZE_T nMappingSize=0, ULONGLONG nOffset=0, DWORD dwMappingProtection=PAGE_READONLY, DWORD dwViewDesiredAccess=FILE_MAP_READ)
Definition: atlfile.h:110
ULONGLONG QuadPart
Definition: ms-dtyp.idl:185
unsigned int BOOL
Definition: ntddk_ex.h:94
HRESULT AtlHresultFromLastError()
Definition: atlcomcli.h:56
smooth NULL
Definition: ftsmooth.c:416
#define FILE_MAP_ALL_ACCESS
Definition: winbase.h:156
#define FILE_MAP_READ
Definition: compat.h:427
Definition: rosdlgs.h:5
$ULONG HighPart
Definition: ntbasedef.h:577
LONG HRESULT
Definition: typedefs.h:77
uint64_t ULONGLONG
Definition: typedefs.h:65
HANDLE WINAPI GetCurrentProcess(VOID)
Definition: proc.c:1168
unsigned long DWORD
Definition: ntddk_ex.h:95
DWORD WINAPI GetFileSize(HANDLE hFile, LPDWORD lpFileSizeHigh)
Definition: fileinfo.c:481
ULONG LowPart
Definition: typedefs.h:104
_Must_inspect_result_ _Out_ PLARGE_INTEGER FileSize
Definition: fsrtlfuncs.h:108
IN PVOID IN PVOID IN USHORT IN USHORT Size
Definition: pci.h:359
_In_ HANDLE hFile
Definition: mswsock.h:90
HRESULT OpenMapping(LPCTSTR szName, SIZE_T nMappingSize, ULONGLONG nOffset=0, DWORD dwViewDesiredAccess=FILE_MAP_ALL_ACCESS)
Definition: atlfile.h:193
HRESULT MapSharedMem(SIZE_T nMappingSize, LPCTSTR szName, BOOL *pbAlreadyExisted=NULL, LPSECURITY_ATTRIBUTES lpsa=NULL, DWORD dwMappingProtection=PAGE_READWRITE, DWORD dwViewDesiredAccess=FILE_MAP_ALL_ACCESS)
Definition: atlfile.h:150
ULONG_PTR SIZE_T
Definition: typedefs.h:78
#define S_OK
Definition: intsafe.h:59
#define PAGE_READONLY
Definition: compat.h:127
static const WCHAR szName[]
Definition: msipriv.h:1194
HRESULT CopyFrom(CAtlFileMappingBase &orig)
Definition: atlfile.h:77
#define OpenFileMapping
Definition: winbase.h:3701
#define ERROR_ALREADY_EXISTS
Definition: disk.h:80
void * GetData() const
Definition: atlfile.h:251
#define UnmapViewOfFile
Definition: compat.h:403
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
#define SUCCEEDED(hr)
Definition: intsafe.h:57
LONGLONG QuadPart
Definition: typedefs.h:112
#define PAGE_READWRITE
Definition: nt_native.h:1304