the code is as follows, the first time MapViewOfFile can return a sucessful value,but the second time in the loop, the value is NULL,and the error code is 5, which means access is denied. I can't find which part is wrong, please help me ,thanks a lot!
HANDLE hFile = CreateFile(fileName,
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
DWORD error = GetLastError();
if (hFile == NULL || hFile == INVALID_HANDLE_VALUE)
{
CString msg;
msg.Format("handle is wrong,code is %d!",error);
AfxMessageBox(msg);
return FALSE;
}
m_hFileMapping = CreateFileMapping(hFile,
NULL,PAGE_READONLY,
0,
0,
NULL);
if (m_hFileMapping == NULL)
{
error = GetLastError();
CloseHandle(hFile);
return FALSE;
}
SYSTEM_INFO SysInfo;
GetSystemInfo(&SysInfo);
DWORD dwGran = SysInfo.dwAllocationGranularity;
DWORD dwFileSizeHigh = 0;
m_iFileByteCount = GetFileSize(hFile,&dwFileSizeHigh);
DWORD qwFileOffset = 0;
DWORD dwBlockBytes = dwGran*10;
DWORD qwFileSize = (DWORD)m_iFileByteCount;
LPCVOID lpBaseAdress = NULL;
while (qwFileSize - qwFileOffset> 0)
{
if (qwFileSize-qwFileOffset < dwBlockBytes)
{
dwBlockBytes = (DWORD)(qwFileSize-qwFileOffset);
}
m_pBeginPointer = (BYTE*)MapViewOfFile(m_hFileMapping,
FILE_MAP_READ,
DWORD(dwBlockBytes>>32),
DWORD(dwBlockBytes&0xFFFFFFFF),
dwBlockBytes);
DWORD error = GetLastError();
if (m_pBeginPointer == NULL)
{
CloseHandle(m_hFileMapping);
m_hFileMapping = NULL;
return FALSE;
}
BOOL b = UnmapViewOfFile(m_pBeginPointer);
qwFileOffset += dwBlockBytes;
}
CloseHandle(m_hFileMapping);