Click here to Skip to main content
15,889,840 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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!

C++
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
Updated 23-Sep-11 23:37pm
v3
Comments
G Haranadh 17-Nov-11 4:01am    
did u got the solution? pls let me know.

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.
 
Share this answer
 
Comments
G Haranadh 17-Nov-11 4:01am    
Hi, i am also facing same problem. is this problem solved. please let me know. thanks.
Richard MacCutchan 17-Nov-11 12:54pm    
Please open a new question with the details of your problem.
Even if all goes without error, you are not closing "hFile" on return.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900