WIN32硬盘文件映射到内存
2021-01-09 04:28
标签:pause window ret file getc bsp 地址 pfile orm #include WIN32硬盘文件映射到内存 标签:pause window ret file getc bsp 地址 pfile orm 原文地址:https://www.cnblogs.com/ganxiang/p/13113063.html
#include
int main()
{
//创建打开文件
HANDLE cFile = CreateFile("D:\\filterBufFile.txt",
GENERIC_READ | GENERIC_WRITE,
0,
NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
//内存申请物理页
HANDLE hMapFile = CreateFileMapping(cFile, NULL, PAGE_READWRITE, 0, 0, "共享映射");
//和虚拟地址映射
LPTSTR lpBuff = (LPTSTR)MapViewOfFile(hMapFile, FILE_MAP_WRITE, 0, 0, 0);
//*(PDWORD)lpBuff = 0x12345678;
//getchar();
//读文件
printf("%p\n", lpBuff);
printf("%x\n", *(PDWORD)lpBuff);
printf("%x\n", *((PDWORD)lpBuff+1));
//写文件
*(PDWORD)lpBuff = 0x41414141;
printf("%x\n", *(PDWORD)lpBuff);
//getchar();
//强制刷新
//FlushViewOfFile(((PDWORD)lpBuff), 4);
//关闭映射
UnmapViewOfFile(lpBuff);
//关闭句柄
CloseHandle(hMapFile);
//关闭句柄
CloseHandle(cFile);
system("pause");
return(0);
}