Problem with Memory-Mapped Files

milozy1983 2005-09-13 11:16:57
Hi :
When I use the code from <<Programming Microsoft Windows CE .NET, Third
Edition>> to read a file using Memory-Mapped . The return handle from
CreateFileForMapping is ok , but when I use CreateFileMapping to get the second handle, the retun handle is failed .I use GetLastError get the error value is 5 , what cause
this problem ? thank you:)
/**********************************************************/
void CMemMapDlg::OnRead()
{
// TODO: Add your control notification handler code here
HANDLE hFile, hFileMap;
PBYTE pFileMem;
TCHAR szFileName[MAX_PATH];
// Get the filename.
lstrcpy(szFileName , _T("\\test1.txt"));
hFile = CreateFileForMapping (szFileName , GENERIC_WRITE,
FILE_SHARE_READ, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,0);
if (hFile == INVALID_HANDLE_VALUE)
{
CString str;
str.Format(_T("CreateFileForMapping Failed %lu") , GetLastError());
MessageBox(str);
}
else {
hFileMap = CreateFileMapping (hFile, NULL, PAGE_READWRITE, 0, 0, NULL);
if (hFileMap) {
MessageBox(_T("Success"));
pFileMem =(PBYTE)MapViewOfFile (hFileMap, FILE_MAP_WRITE, 0, 0, 0);
if (pFileMem) {
//
// Use the data in the file.
//

// Start cleanup by unmapping view.
UnmapViewOfFile (pFileMem);
}
CloseHandle (hFileMap);
}
else
{
CString str;
str.Format(_T("CreateFileMapping Failed %lu") , GetLastError());
//error 5: ERROR_ACCESS_DENIED
MessageBox(str);
}
CloseHandle (hFile);
}
}
...全文
51 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
milozy1983 2005-09-14
  • 打赏
  • 举报
回复
Though i've solved this problem , thank you for your advice.Look at the solution.

Form the docs:

PAGE_READWRITE

Gives read-write access to the committed region of pages. The file specified
by hFile must have been created with GENERIC_READ and GENERIC_WRITE access.

Cheers
Doug Forster
西山小月 2005-09-13
  • 打赏
  • 举报
回复
error code 5 is ERROR_ACCESS_DENIED Access is denied.

19,502

社区成员

发帖
与我相关
我的任务
社区描述
硬件/嵌入开发 嵌入开发(WinCE)
社区管理员
  • 嵌入开发(WinCE)社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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