Thread: Problem with CreateFileMapping() function

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    99

    Problem with CreateFileMapping() function

    Dear Experts,

    i have a problem with CreateFileMapping() function ,
    i am trying in this way,

    Code:
    hnd = (Mbyte*)CreateFile(fname, GENERIC_WRITE, NULL, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL,NULL);
    
    printf("\n in if _hMap==============%d",(int)_hnd);//which gives me some 2060 
    _hMap = (char *)CreateFileMapping(_hnd,NULL,MAPPROTWRMODE,_fileSize,NULL);
    
    printf("\n in if mapHandle==============%d",(int)_hmap);//which gives me NULL
    why my map is getting failed when i can succesfully create a file,
    i want the map for the file the "fname" specified in createfile please help me what can be the reason please provide me some solution.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Did you just type that out of your head?

    1. Remove all the casts, and declare the variables properly.
    2. http://msdn.microsoft.com/en-us/libr...37(VS.85).aspx - 6 params, you have 5.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    BTW, since this question is clearly Windows specific (because you're talking about Windows API functions), you should be posting it in the Windows Programming forum instead of the general C++ forum. Just to keep things more organized and get the right kind of help.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Oh yes, now that you mention it... moved.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    99

    Problem with creartedFilemapping

    dear Experts,
    sorry that i might have posted in the wrong forum,
    but please tell me the solution to my problem,
    if i remove the casts also the createFileMapping() is getting failed,
    please tell me what is wrong because i used many examples to test ,
    but there was not output please tell me what to do,

    Code:
    hnd = CreateFile(fname, GENERIC_WRITE, NULL, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL,NULL);
    
    _hMap =CreateFileMapping(_hnd,NULL,MAPPROTWRMODE,_fileSize,NULL);
    this _hmap is failing,what is wrong plz help me.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You couldn't have found any examples like that, since as Salem points out it takes six arguments not five. (The last being "optional" only means you can pass in 0/NULL, not that you can pass in nothing at all.) Also, MAPPROTWRMODE is not listed as a valid value for the third value. (Actually, it looks like you're trying to make "_fileSize" play the part of the fourth and fifth arguments together.)

  7. #7
    Registered User
    Join Date
    Nov 2007
    Posts
    99
    Dear experts,

    sorry for sending that code actually it was

    Code:
    #define MAPPROTWRMODE	PAGE_READWRITE
    
    hnd = CreateFile(fname, GENERIC_WRITE, NULL, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL,NULL);
    
    _hMap =CreateFileMapping(_hnd,NULL,MAPPROTWRMODE,0,_fileSize,NULL);
    this is the actual one which is syntactically correct but returning NULL,
    so know any corrections you suggest.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You should check GetLastError.

    But I would guess that since you opened the file with write privileges, trying to map it with read/write privileges may not work.

  9. #9
    Registered User
    Join Date
    Nov 2007
    Posts
    99

    createFileMapping()

    Dear experts,
    thanks for reply,
    but that is not the solution as iam opening the file in GENERIC_WRITE,so it will write instead of read,
    any more solutions.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I doubt that's what GetLastError said.

    (Edit: by that I mean "but that is not the solution as iam opening the file in GENERIC_WRITE,so it will write instead of read" is not a Windows system message.)

    Edit: And I've been assuming that the fact that one line says "hnd" and the next line says "_hnd" is just a copy-n-paste error. If not, well.
    Last edited by tabstop; 10-16-2008 at 11:06 PM.

  11. #11
    Registered User
    Join Date
    Nov 2007
    Posts
    99

    problem with CreateFileMapping()

    Thank you for reply ,

    i am using the getLastError() in this way

    Code:
    #define MAPPROTWRMODE	PAGE_READWRITE
    
    _hnd = CreateFile(fname, GENERIC_WRITE, NULL, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL,NULL);
    
    _hMap =CreateFileMapping(_hnd,NULL,MAPPROTWRMODE,0,_fileSize,NULL);
    
    register int errstat=0;
     if(_hMap == NULL)
    {
       errstat=GetLastError();
        if(errstat)
       printf("\n ERROR* CreateFileMapping: error=%d\n",errstat);//which  returns me 5 as number 	
    }
    i am using like this which gives me error=5 what does it mean ,
    is it the right way i am using or any other way but does 5 indicates.
    Last edited by vin_pll; 10-17-2008 at 10:25 PM.

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well if you clicked around where tabstop pointed, you would get to
    http://msdn.microsoft.com/en-us/libr...82(VS.85).aspx

    ERROR_ACCESS_DENIED 5 0x5 Access is denied.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I still say GENERIC_WRITE should be GENERIC_READ|GENERIC_WRITE. Especially since the page that has been linked two or three times now says this:
    Quote Originally Posted by msdn
    PAGE_READWRITE Allows views to be mapped for read-only, copy-on-write, or read/write access.

    The file handle that the hFile parameter specifies must be created with the GENERIC_READ and GENERIC_WRITE access rights.

  14. #14
    Registered User
    Join Date
    Nov 2007
    Posts
    99

    problem with CreateFileMapping()

    Thank you experts thanks a lot,

    thanks especially to tabstop who guessed the problem exactly, thanks to you specially,
    the problem got solved , really very pleasurefull morning to me.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. Problem with function pointers
    By vNvNation in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2004, 06:49 AM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM