内存访问冲突。

mengde007 2009-03-31 11:34:40

#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;
}
...全文
229 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
noWen 2009-10-10
  • 打赏
  • 举报
回复
mark
mengde007 2009-03-31
  • 打赏
  • 举报
回复
大哥,我就是按照你上面的写法啊,难道VS……
Chivalry 2009-03-31
  • 打赏
  • 举报
回复
HANDLE hFile = CreateFile(_T("d:\\a.txt"), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

你CreateFile的时候,是这么写的么?
Chivalry 2009-03-31
  • 打赏
  • 举报
回复
HANDLE hFile = CreateFile(_T("d:\\a.txt"), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

HANDLE hFileMapping = CreateFileMapping(hFile, NULL, PAGE_WRITECOPY,
0, 0, NULL);

PBYTE pbFile = (PBYTE) MapViewOfFile(hFileMapping, FILE_MAP_COPY,
0, 0, 0);
BYTE bSomeByte = pbFile[0];
pbFile[0] ='b';
pbFile[1] = 0;//这内存访问冲突
UnmapViewOfFile(pbFile);

CloseHandle(hFileMapping);
CloseHandle(hFile);

运行的这段代码,没发现什么问题啊
mengde007 2009-03-31
  • 打赏
  • 举报
回复
我用的是VS2005,因为hFileMapping在调试的时候一直为NULL,所以就访问冲突了
Chivalry 2009-03-31
  • 打赏
  • 举报
回复
pbFile[1] = 0;//这内存访问冲突
什么意思?我试了一下,没出现什么错误提示啊
mengde007 2009-03-31
  • 打赏
  • 举报
回复
还是不行啊……
fishion 2009-03-31
  • 打赏
  • 举报
回复
试试CreateFile(_T("d:\\a.txt"), GENERIC_READ | GENERIC_WRITE, 0, NULL,
OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

mengde007 2009-03-31
  • 打赏
  • 举报
回复
好友,仍然不行……
Chivalry 2009-03-31
  • 打赏
  • 举报
回复
HANDLE hFile = CreateFile(_T("d:\\a.txt"), GENERIC_READ | GENERIC_WRITE, GENERIC_READ | GENERIC_WRITE, NULL,
OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

FILE_SHARE_READ | FILE_SHARE_WRITE

mengde007 2009-03-31
  • 打赏
  • 举报
回复
终于搞定了,原来是自己的文件空了。

15,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 进程/线程/DLL
社区管理员
  • 进程/线程/DLL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧