Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VC6 C++ MFC
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);  
Posted 23-Sep-11 23:24pm
yzmcc327
Edited 23-Sep-11 23:37pm
v3
Comments
G Haranadh at 17-Nov-11 4:01am
   
did u got the solution? pls let me know.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You seem to be confused (I definitely am) about all the various values being used to specify file sizes and offsets in the above. You are also using casts to convert DWORD values to DWORDs, which is totally redundant. In the MapViewOfFile() function call you seem to think that dwBlockBytes is a 64-bit value even though you declared it as a DWORD with a value of dwGran*10;. You are also ignoring the result of GetLastError() in a couple of places which may help in diagnosing your problem.
  Permalink  
Comments
G Haranadh at 17-Nov-11 4:01am
   
Hi, i am also facing same problem. is this problem solved. please let me know. thanks.
Richard MacCutchan at 17-Nov-11 12:54pm
   
Please open a new question with the details of your problem.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Even if all goes without error, you are not closing "hFile" on return.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 495
1 OriginalGriff 339
2 ChintanShukla 305
3 Richard Deeming 260
4 RyanDev 230
0 Sergey Alexandrovich Kryukov 8,768
1 OriginalGriff 7,387
2 CPallini 2,603
3 Richard MacCutchan 2,095
4 Abhinav S 1,893


Advertise | Privacy | Mobile
Web03 | 2.8.140827.1 | Last Updated 24 Sep 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100