Thread: What does MapViewOfFile return?

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    385

    What does MapViewOfFile return?

    "MapViewOfFile", does this function map a file into the virtual memory and return the base address of the mapped memory?? If yes, then the following code should output 0X400000, beacuse by default the exe is loaded at this location, but the output is 0X360000. Why?? Also when I try to print out the ImageBase entry in the optional header it prints out 0X400000???

    Code:
    #include<iostream>
    #include<Windows.h>
    #include<stdio.h>
    #include<WinNT.h>
    
    
    int main()
    {
    
    
    HANDLE  hFile,hFileMapping;
    LPVOID lpFileBase;
    
    
    if((hFile = CreateFile(TEXT("c:\\linked list.exe"),GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0)) == INVALID_HANDLE_VALUE)
        std::cout<<"unable to open";
    
    if((hFileMapping = CreateFileMapping(hFile,NULL,PAGE_READONLY,0,0,NULL)) == 0)
    {
        CloseHandle(hFile);
        std::cout<<"unable to open for mapping";
    }
    
    if((lpFileBase = MapViewOfFile(hFileMapping,FILE_MAP_READ,0,0,0))== 0)
    {
        CloseHandle(hFile);
        CloseHandle(hFileMapping);
        std::cout<<"couldn't map view of file";
    }
    
    printf("%x\n",lpFileBase);
    
    }
    Last edited by juice; 03-15-2012 at 05:32 AM.

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    RTFM

    Return value

    If the function succeeds, the return value is the starting address of the mapped view.

    If the function fails, the return value is NULL. To get extended error information, call GetLastError.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 07-04-2007, 12:20 AM
  2. Replies: 12
    Last Post: 04-07-2007, 11:11 AM
  3. Replies: 6
    Last Post: 04-09-2006, 04:32 PM
  4. Replies: 4
    Last Post: 07-15-2005, 04:10 PM
  5. MapViewOfFile and CreateDIBSection problem with bitmaps
    By LuckY in forum Windows Programming
    Replies: 2
    Last Post: 08-18-2004, 08:37 AM