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