Problem with Memory-Mapped Files
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);
}
}