CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Aug 2001
    Posts
    51

    Angry CreateFileMapping Problem

    This is perplexing. I set up a test of shared memory in a down & dirty application, and it worked fine. Now I'm going back through and cleaning it up. I'm attempting to move the shared memory to a class I can then use in other applications -- only the class instantiation is failing.

    Here is the down & dirty code:

    Code:
    CString sharedMemFile = "shared.mem"; DWORD memSize = 0x80000; HANDLE mapHandle = ::CreateFileMapping(INVALID_HANDLE_VALUE,NULL,PAGE_READWRITE,0,memSize,sharedMemFile); char *bufr = (char *)MapViewOfFile(mapHandle,FILE_MAP_WRITE,0,0,memSize);
    This is working fine (though its ugly). mapHandle is assigned an address, which I then use in to map the view. I've run tests and I'm able to copy data to and from the bufr, and I'm using a mutex to lock & unlock it before doing so.

    From this, I created a class header:

    Code:
    class SharedMem
    {
    
    HANDLE memHandle; DWORD memSize; char *memBufr; CString memName; CMutex mutex; CString error; DWORD errnum; SharedMem(const SharedMem &c); SharedMem &operator=(const SharedMem &c);
    public:
    enum OpenShared { OPEN_READ, OPEN_WRITE }; SharedMem(CString sharedMemName,OpenShared shared=OPEN_READ,DWORD sharedMemSize=0x80000); ~SharedMem(); void Put(LPCSTR data,DWORD dsize); LPCSTR Get();
    };
    and the .cpp file:
    Code:
    SharedMem::SharedMem(CString sharedMemName,OpenShared shared/* =OPEN_READ */,DWORD sharedMemSize/* =0x80000 */)
    : memHandle(NULL),memName(sharedMemName),memSize(sharedMemSize),memBufr(0),mutex(FALSE,memName),error(""),errnum(0)
    {
    
    if( shared == OPEN_READ ) {
    memHandle = OpenFileMapping(FILE_MAP_READ,FALSE,memName); if( memHandle && memHandle != HANDLE(-1) ) {
    memBufr = (char *)MapViewOfFile(memHandle,FILE_MAP_READ,0,0,memSize);
    }
    } else {
    memHandle = CreateFileMapping(INVALID_HANDLE_VALUE,NULL,FILE_MAP_WRITE,0,sharedMemSize,sharedMemName); if( memHandle && memHandle != HANDLE(-1) ) {
    memBufr= (char *)MapViewOfFile(memHandle,FILE_MAP_WRITE,0,0,memSize);
    }
    } if( memHandle == NULL || memHandle == HANDLE(-1) || memBufr == 0 ) {
    // handle error here
    }
    }
    The instantiation for writing to the shared memory is:
    Code:
    SharedMem sharedMem("share.mem",SharedMem::OPEN_WRITE);
    When I walk through debug, the CreateFileMapping is returning NULL, indicating an error. GetLastError is saying "The handle is invalid"

    I don't understand why it's failing; I've compared the code and the code between the down & dirty and the class and its the same, as are the arguments. Perhaps I've been stairing at it too long and can't see it.

    Can any one see what I'm obviously missing?

    Thanks

  2. #2
    Join Date
    May 2006
    Posts
    327

    Re: CreateFileMapping Problem

    Maybe the third argument of CreateFileMapping is invalid and should be replaced with PAGE_READWRITE?

    I hope it helps.

  3. #3
    Join Date
    Aug 2001
    Posts
    51

    Re: CreateFileMapping Problem

    Thanks, that didn't make any difference.

    I actually had it as PAGE_READWRITE and was testing to see if changing it made any difference. When I put everything back so that I could post it here, I missed changing that part back. It should read:

    Code:
    memHandle = CreateFileMapping(INVALID_HANDLE_VALUE,NULL,
    PAGE_READWRITE,0,memSize,memName);

  4. #4
    Join Date
    May 2006
    Posts
    327

    Re: CreateFileMapping Problem

    It seems the problem is caused by name collision. Try to assign a different name to your named mutex in the constructor of SharedMem class. For instance:

    Code:
    SharedMem::SharedMem(CString sharedMemName, . . .)
    : mutex(FALSE, sharedMemName + _T(".mutex")), . . .
    {
        . . .
    }
    I hope it helps.
    Last edited by Viorel; July 13th, 2006 at 01:53 AM.

  5. #5
    Join Date
    Aug 2001
    Posts
    51

    Re: CreateFileMapping Problem

    Thanks, that's exactly what it was.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured