Click here to Skip to main content
15,893,663 members

question about the memery map in vc++

yzmcc asked:

Open original thread
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);  
Tags: C++, Visual C++ 6.0, MFC

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



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