1// Windows/FileMapping.h
2
3#ifndef __WINDOWS_FILEMAPPING_H
4#define __WINDOWS_FILEMAPPING_H
5
6#include "../Common/MyTypes.h"
7
8#include "Handle.h"
9
10namespace NWindows {
11
12class CFileMapping: public CHandle
13{
14public:
15  WRes Create(DWORD protect, UInt64 maxSize, LPCTSTR name)
16  {
17    _handle = ::CreateFileMapping(INVALID_HANDLE_VALUE, NULL, protect, (DWORD)(maxSize >> 32), (DWORD)maxSize, name);
18    return ::GetLastError();
19  }
20
21  WRes Open(DWORD
22      #ifndef UNDER_CE
23      desiredAccess
24      #endif
25      , LPCTSTR name)
26  {
27    #ifdef UNDER_CE
28    WRes res = Create(PAGE_READONLY, 0, name);
29    if (res == ERROR_ALREADY_EXISTS)
30      return 0;
31    Close();
32    if (res == 0)
33      res = ERROR_FILE_NOT_FOUND;
34    return res;
35    #else
36    _handle = ::OpenFileMapping(desiredAccess, FALSE, name);
37    if (_handle != 0)
38      return 0;
39    return ::GetLastError();
40    #endif
41  }
42
43  LPVOID Map(DWORD desiredAccess, UInt64 fileOffset, SIZE_T numberOfBytesToMap)
44  {
45    return ::MapViewOfFile(_handle, desiredAccess, (DWORD)(fileOffset >> 32), (DWORD)fileOffset, numberOfBytesToMap);
46  }
47
48  #ifndef UNDER_CE
49  LPVOID Map(DWORD desiredAccess, UInt64 fileOffset, SIZE_T numberOfBytesToMap, LPVOID baseAddress)
50  {
51    return ::MapViewOfFileEx(_handle, desiredAccess, (DWORD)(fileOffset >> 32), (DWORD)fileOffset, numberOfBytesToMap, baseAddress);
52  }
53  #endif
54};
55
56class CFileUnmapper
57{
58  const void *_data;
59public:
60  CFileUnmapper(const void *data) : _data(data) {}
61  ~CFileUnmapper() { ::UnmapViewOfFile(_data); }
62};
63
64}
65
66#endif
67