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