#include<windows.h>
#include<stdio.h>
#include<tchar.h>
#include <strsafe.h>
#include<locale.h>
#define BUFFSIZE 1024
#define FILE_MAP_START 138240 //135KB
LPCWCHAR file = TEXT("test.txt");
DWORD failureMessage() {
DWORD dw = GetLastError();
LPTSTR lpMsgBuf;
LPTSTR lpDisplayBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dw,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR)&lpMsgBuf,//自动在末尾加 换行符
0, NULL);
// Display the error message and exit the process
lpDisplayBuf = (LPTSTR)LocalAlloc(LMEM_ZEROINIT,
(lstrlen(lpMsgBuf) + 40) * sizeof(TCHAR));
StringCchPrintf(lpDisplayBuf,
LocalSize(lpDisplayBuf) / sizeof(TCHAR),
L"failed with error %d : %s",
dw, lpMsgBuf);
wprintf(lpDisplayBuf);
LocalFree(lpMsgBuf);
LocalFree(lpDisplayBuf);
return dw;
}
int main() {
setlocale(LC_ALL, "chs");
HANDLE hMapFile;
HANDLE hFile;
BOOL bFlag;
DWORD dByteWritten;
DWORD dwFileSize;
DWORD dwFileMapSize;
DWORD dwMapViewSize;
DWORD dwFileMapStart;
DWORD dwSysGran;
SYSTEM_INFO SysInfo;
LPVOID mapAddress;
char* pData;
int i;
int iData;
int iViewDelta;
hFile = CreateFile(file, GENERIC_READ | GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
//failureMessage();
// Get the system allocation granularity.
GetSystemInfo(&SysInfo);
//failureMessage();
dwSysGran = SysInfo.dwAllocationGranularity;
wprintf(L"dwAllocationGranularity:%d\n", dwSysGran);
dwFileMapStart = (FILE_MAP_START / dwSysGran) * dwSysGran;
_tprintf(TEXT("The file map view starts at %ld bytes into the file.\n"),
dwFileMapStart);
dwMapViewSize = (FILE_MAP_START % dwSysGran) + BUFFSIZE;
_tprintf(TEXT("The file map view is %ld bytes large.\n"),
dwMapViewSize);
dwFileMapSize = FILE_MAP_START + BUFFSIZE;
_tprintf(TEXT("The file mapping object is %ld bytes large.\n"),
dwFileMapSize);
iViewDelta = FILE_MAP_START - dwFileMapStart;
_tprintf(TEXT("The data is %d bytes into the view.\n"),
iViewDelta);
DWORD dBytesWritten;
for (i = 0; i < (int)dwSysGran; i++)
{
WriteFile(hFile, &i, sizeof(i), &dBytesWritten, NULL);
}
// Verify that the correct file size was written.
dwFileSize = GetFileSize(hFile, NULL);
_tprintf(TEXT("hFile size: %10d\n"), dwFileSize);
hMapFile = CreateFileMapping(hFile, // current file handle
NULL, // default security
PAGE_READWRITE, // read/write permission
0, // size of mapping object, high
dwFileMapSize, // size of mapping object, low
NULL); // name of mapping object
if (hMapFile == NULL)
{
_tprintf(TEXT("hMapFile is NULL: last error: %d\n"), GetLastError());
return (2);
}
_tprintf(TEXT("CreateFileMapping OK\n"));
LPVOID lpMapAddress = MapViewOfFile(hMapFile, // handle to
// mapping object
FILE_MAP_ALL_ACCESS, // read/write
0, // high-order 32
// bits of file
// offset
dwFileMapStart, // low-order 32
// bits of file
// offset
dwMapViewSize); // number of bytes
// to map
if (lpMapAddress == NULL)
{
_tprintf(TEXT("lpMapAddress is NULL: last error: %d\n"), GetLastError());
return 3;
}
_tprintf(TEXT("MapViewOfFile OK\n"));
pData = (char*)lpMapAddress + iViewDelta;
iData = *(int*)pData;
_tprintf(TEXT("The value at the pointer is %d,\nwhich %s one quarter of the desired file offset.\n"),
iData,
iData * 4 == FILE_MAP_START ? TEXT("is") : TEXT("is not"));
bFlag = UnmapViewOfFile(lpMapAddress);
_tprintf(TEXT("UnmapViewOfFile OK\n"));
bFlag = CloseHandle(hMapFile); // close the file mapping object
if (!bFlag)
{
_tprintf(TEXT("\nError %ld occurred closing the mapping object!"),
GetLastError());
}
bFlag = CloseHandle(hFile); // close the file itself
if (!bFlag)
{
_tprintf(TEXT("\nError %ld occurred closing the file!"),
GetLastError());
}
_tprintf(TEXT("CloseHandle OK\n"));
return 0;
}
dwAllocationGranularity:65536
The file map view starts at 131072 bytes into the file.
The file map view is 8192 bytes large.
The file mapping object is 139264 bytes large.
The data is 7168 bytes into the view.
hFile size: 262144
CreateFileMapping OK
MapViewOfFile OK
The value at the pointer is 34560,
which is one quarter of the desired file offset.
UnmapViewOfFile OK
CloseHandle OK