Winapi MapViewOfFile文件分块加载文件
我想将文件映射到内存中,块大小等于系统粒度。第一个块读取没有错误,所有其他块读取失败,错误5错误\u访问被拒绝。我试着用管理员权限运行这个程序 我的代码:Winapi MapViewOfFile文件分块加载文件,winapi,memory-mapped-files,Winapi,Memory Mapped Files,我想将文件映射到内存中,块大小等于系统粒度。第一个块读取没有错误,所有其他块读取失败,错误5错误\u访问被拒绝。我试着用管理员权限运行这个程序 我的代码: #include <windows.h> #include <stdio.h> int main() { HANDLE hFile = CreateFile( TEXT("db.txt"),
#include <windows.h>
#include <stdio.h>
int main() {
HANDLE hFile = CreateFile( TEXT("db.txt"),
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (hFile == INVALID_HANDLE_VALUE) {
printf("[ERROR] File opening error %d\n", GetLastError());
return 1;
}
printf("[DONE] File opened successfully.\n");
HANDLE hMap = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
if (hMap == NULL) {
printf("[ERROR] Create mapping error %d\n", GetLastError());
return 2;
}
printf("[DONE] Create mapping successfully.\n");
LARGE_INTEGER file_size = { };
if (!GetFileSizeEx(hFile, &file_size)) {
printf("[ERROR] Getiing filesize error %d\n", GetLastError());
return 3;
}
printf("[DONE] Getting file size.\n");
SYSTEM_INFO info = { };
GetSystemInfo(&info);
printf("[DONE] Getting system memory granularity %d.\n", info.dwAllocationGranularity);
DWORD offset = 0;
int size = 0;
do {
char* ENTRY = (char*)MapViewOfFile(hMap, FILE_MAP_READ, HIWORD(offset), LOWORD(offset), info.dwAllocationGranularity);
if (ENTRY == NULL) {
printf("[ERROR] Map entry error %d\n", GetLastError());
} else {
printf("[DONE] MAPPING PART WITH OFFSET %d\n", offset);
//printf("%s\n", ENTRY);
}
if (offset + info.dwAllocationGranularity < file_size.QuadPart) {
offset += info.dwAllocationGranularity;
} else {
offset = file_size.QuadPart;
}
//offset += size;
UnmapViewOfFile(ENTRY);
} while (offset < file_size.QuadPart);
CloseHandle(hMap);
CloseHandle(hFile);
system("pause");
return 0;
}
如何修复它?在调用MapViewOfFile时,您正在使用HIWORD和LOWORD作为偏移量,但它们只接受一个32位的值并将其拆分为两个16位的一半-您需要的是将64位的值拆分为两个32位的一半
相反,您需要HIDWORD和LODWORD,它们在中定义:
像这样:
char* ENTRY = (char*)MapViewOfFile(hMap, FILE_MAP_READ, HIDWORD(offset), LODWORD(offset), info.dwAllocationGranularity);
即使您的偏移量变量是32位,您也需要这样做。在这种情况下,HIDWORD将只返回0,并且偏移量的完整值作为低阶DWORD传递。MapViewOfFile的参数是高和低DWORD,而不是字。根据旧版本的结果,偏移量>>32是未定义的。政府证实了这一点。因此,如果基础变量是DWORD,我认为应该显式使用0,offset,而不是调用HIDWORD和LODWORD。文件大小使用64位变量,尽管我建议使用ULARGE_INTEGER。
char* ENTRY = (char*)MapViewOfFile(hMap, FILE_MAP_READ, HIDWORD(offset), LODWORD(offset), info.dwAllocationGranularity);