[reply]#include <wininet.h>
class FileView
{
public:
int Init(const std::wstring& wFileName, const ULARGE_INTEGER& uiSize)
{
// Get the system allocation granularity.
::GetNativeSystemInfo(&m_si);
::memcpy(&m_uiFileMapping, &uiSize, sizeof(uiSize));
::memcpy(&m_uiMapViewSize, &uiSize, sizeof(uiSize));
// Create the test file. Open it "Create Always" to overwrite any
// existing file. The data is re-created below
m_hFile = ::CreateFileW(wFileName.c_str(), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_FLAG_SEQUENTIAL_SCAN, NULL);
if (m_hFile == INVALID_HANDLE_VALUE)
{
_tprintf(TEXT("hFile is NULL\n"));
_tprintf(TEXT("Target file is %s\n"), L"test.txt");
return(-1);
}
// Create a file mapping object for the file
// Note that it is a good idea to ensure the file size is not zero
m_hMapFile = ::CreateFileMappingW(m_hFile, // current file handle
NULL, // default security
PAGE_READWRITE, // read/write permission
m_uiFileMapping.HighPart, // size of mapping object, high
m_uiFileMapping.LowPart, // size of mapping object, low
wFileName.substr(wFileName.rfind(L'.')).c_str()); // name of mapping object
if (m_hMapFile == NULL)
{
_tprintf(TEXT("hMapFile is NULL: last error: %d\n"), GetLastError());
return(-2);
}
// Map the view and test the results.
m_lpMapAddress = ::MapViewOfFile(m_hMapFile, // handle to mapping object
FILE_MAP_ALL_ACCESS, // read/write
m_uiFileOffset.HighPart, // high-order 32 bits of file offset
m_uiFileOffset.LowPart, // low-order 32 bits of file offset
m_uiMapViewSize.QuadPart); // number of bytes to map
if (m_lpMapAddress == NULL)
{
_tprintf(TEXT("lpMapAddress is NULL: last error: %d\n"), GetLastError());
return(-3);
}
return(0);
}
int Exit(const ULARGE_INTEGER& uiSize)
{
BOOL bRet = FALSE;
// Close the file mapping object and the open file
if (m_lpMapAddress == NULL)
{
return(0);
}
::FlushViewOfFile(m_lpMapAddress, 0);
bRet = ::UnmapViewOfFile(m_lpMapAddress);
if (m_hMapFile == NULL)
{
return(0);
}
bRet = ::CloseHandle(m_hMapFile); // close the file mapping object
if (!bRet)
{
_tprintf(TEXT("\nError %ld occurred closing the mapping object!"), GetLastError());
}
if (m_hFile == NULL)
{
return(0);
}
::SetFilePointer(m_hFile, uiSize.LowPart, (PLONG)&uiSize.HighPart, FILE_BEGIN);
::SetFileValidData(m_hFile, uiSize.QuadPart);
::SetEndOfFile(m_hFile);
::FlushFileBuffers(m_hFile);
bRet = ::CloseHandle(m_hFile); // close the file itself
if (!bRet)
{
_tprintf(TEXT("\nError %ld occurred closing the file!"), GetLastError());
}
return(0);
}
public:
SYSTEM_INFO m_si = {}; // system information; used to get granularity
HANDLE m_hFile = NULL; // the file handle
HANDLE m_hMapFile = NULL; // handle for the file's memory-mapped region
LPVOID m_lpMapAddress = NULL; // pointer to the base address of the
ULARGE_INTEGER m_uiFileMapping = { 0UL,0UL, };
ULARGE_INTEGER m_uiFileOffset = { 0UL,0UL, };
ULARGE_INTEGER m_uiMapViewSize = { 0UL,0UL, };
};
[/reply]
#include <wininet.h>
class FileView
{
public:
int Init(const std::wstring& wFileName, const ULARGE_INTEGER& uiSize)
{
// Get the system allocation granularity.
::GetNativeSystemInfo(&m_si);
::memcpy(&m_uiFileMapping, &uiSize, sizeof(uiSize));
::memcpy(&m_uiMapViewSize, &uiSize, sizeof(uiSize));
// Create the test file. Open it "Create Always" to overwrite any
// existing file. The data is re-created below
m_hFile = ::CreateFileW(wFileName.c_str(), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_FLAG_SEQUENTIAL_SCAN, NULL);
if (m_hFile == INVALID_HANDLE_VALUE)
{
_tprintf(TEXT("hFile is NULL\n"));
_tprintf(TEXT("Target file is %s\n"), L"test.txt");
return(-1);
}
// Create a file mapping object for the file
// Note that it is a good idea to ensure the file size is not zero
m_hMapFile = ::CreateFileMappingW(m_hFile, // current file handle
NULL, // default security
PAGE_READWRITE, // read/write permission
m_uiFileMapping.HighPart, // size of mapping object, high
m_uiFileMapping.LowPart, // size of mapping object, low
wFileName.substr(wFileName.rfind(L'.')).c_str()); // name of mapping object
if (m_hMapFile == NULL)
{
_tprintf(TEXT("hMapFile is NULL: last error: %d\n"), GetLastError());
return(-2);
}
// Map the view and test the results.
m_lpMapAddress = ::MapViewOfFile(m_hMapFile, // handle to mapping object
FILE_MAP_ALL_ACCESS, // read/write
m_uiFileOffset.HighPart, // high-order 32 bits of file offset
m_uiFileOffset.LowPart, // low-order 32 bits of file offset
m_uiMapViewSize.QuadPart); // number of bytes to map
if (m_lpMapAddress == NULL)
{
_tprintf(TEXT("lpMapAddress is NULL: last error: %d\n"), GetLastError());
return(-3);
}
return(0);
}
int Exit(const ULARGE_INTEGER& uiSize)
{
BOOL bRet = FALSE;
// Close the file mapping object and the open file
if (m_lpMapAddress == NULL)
{
return(0);
}
::FlushViewOfFile(m_lpMapAddress, 0);
bRet = ::UnmapViewOfFile(m_lpMapAddress);
if (m_hMapFile == NULL)
{
return(0);
}
bRet = ::CloseHandle(m_hMapFile); // close the file mapping object
if (!bRet)
{
_tprintf(TEXT("\nError %ld occurred closing the mapping object!"), GetLastError());
}
if (m_hFile == NULL)
{
return(0);
}
::SetFilePointer(m_hFile, uiSize.LowPart, (PLONG)&uiSize.HighPart, FILE_BEGIN);
::SetFileValidData(m_hFile, uiSize.QuadPart);
::SetEndOfFile(m_hFile);
::FlushFileBuffers(m_hFile);
bRet = ::CloseHandle(m_hFile); // close the file itself
if (!bRet)
{
_tprintf(TEXT("\nError %ld occurred closing the file!"), GetLastError());
}
return(0);
}
public:
SYSTEM_INFO m_si = {}; // system information; used to get granularity
HANDLE m_hFile = NULL; // the file handle
HANDLE m_hMapFile = NULL; // handle for the file's memory-mapped region
LPVOID m_lpMapAddress = NULL; // pointer to the base address of the
ULARGE_INTEGER m_uiFileMapping = { 0UL,0UL, };
ULARGE_INTEGER m_uiFileOffset = { 0UL,0UL, };
ULARGE_INTEGER m_uiMapViewSize = { 0UL,0UL, };
};