15,471
社区成员
发帖
与我相关
我的任务
分享
#include <windows.h>
#include <iostream>
#include <TCHAR.h>
using namespace std;
// Open the file that we want to map.
int main()//这儿也是有问题:如果改成 int WINAPI _tWinMain(HINSTANCE hinstExe, HINSTANCE,PTSTR pszCmdLine, //int nCmdShow)那么程序就跑步起来,希望你们能帮我调试一下.
{
HANDLE hFile = CreateFile(_T("d:\\a.txt"), GENERIC_READ | GENERIC_WRITE, GENERIC_READ | GENERIC_WRITE, NULL,
OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
// Create a file-mapping object for the file.
HANDLE hFileMapping = CreateFileMapping(hFile, NULL, PAGE_WRITECOPY,
0, 0, NULL);
// Map a copy-on-write view of the file; the system will commit
// enough physical storage from the paging file to accommodate
// the entire file. All pages in the view will initially have
// PAGE_WRITECOPY access.
PBYTE pbFile = (PBYTE) MapViewOfFile(hFileMapping, FILE_MAP_COPY,
0, 0, 0);
// Read a byte from the mapped view.
BYTE bSomeByte = pbFile[0];
cout<<bSomeByte<<endl;;
// When reading, the system does not touch the committed pages in
// the paging file. The page keeps its PAGE_WRITECOPY attribute.
// Write a byte to the mapped view.
pbFile[0] ='b';
cout<<pbFile[0]<<endl;
// When writing for the first time, the system grabs a committed
// page from the paging file, copies the original contents of the
// page at the accessed memory address, and maps the new page
// (the copy) into the process's address space. The new page has
// an attribute of PAGE_READWRITE.
// Write another byte to the mapped view.
pbFile[1] = 0;//这内存访问冲突
// Because this byte is now in a PAGE_READWRITE page, the system
// simply writes the byte to the page (backed by the paging file).
// When finished using the file's mapped view, unmap it.
// UnmapViewOfFile is discussed in the next section.
cout<<pbFile[0]<<endl;
UnmapViewOfFile(pbFile);
// The system decommits the physical storage from the paging file.
// Any writes to the pages are lost.
// Clean up after ourselves.
CloseHandle(hFileMapping);
CloseHandle(hFile);
return 0;
}