MapViewOfFile映射的文件位置是由参数指定的,内存分配粒度用GetSystemInfo函数来取。看MSDN中MapViewOfFile的参数说明: dwFileOffsetHigh [in] A high-order DWORD of the file offset where the view begins. dwFileOffsetLow [in] A low-order DWORD of the file offset where the view begins. The combination of the high and low offsets must specify an offset within the file that matches the memory allocation granularity of the system, or the function fails. That is, the offset must be a multiple of the allocation granularity. To obtain the memory allocation granularity of the system, use the GetSystemInfo function, which fills in the members of a SYSTEM_INFO structure.
即:
SYSTEM_INFO::dwPageSize
不同操作系统的页大小是不一样的.
dwFileOffsetHigh
[in] A high-order DWORD of the file offset where the view begins.
dwFileOffsetLow
[in] A low-order DWORD of the file offset where the view begins. The combination of the high and low offsets must specify an offset within the file that matches the memory allocation granularity of the system, or the function fails. That is, the offset must be a multiple of the allocation granularity. To obtain the memory allocation granularity of the system, use the GetSystemInfo function, which fills in the members of a SYSTEM_INFO structure.
LPVOID pvMapHead = MapViewOfFile(...);
DWORD Offset = 5;
LPVOID pvTar = (LPVOID)((char*)pvMapHead+Offset);
那么,借用楼上的部分代码,
DWORD dwhigh = 0;
DWORD dwlow = 2 * 64 * 1024;
DWORD dwNumofBytes = 3 * 1024;
LPVOID pvMapHead = MapViewOfFile(hFile,FILE_MAP_READ,dwhigh,dwlow,dwNumofBytes);
DWORD offset = 1024;
LPVOID pvTar = (LPVOID)((char*)pvMapHead+Offset);