Shared memory problem------>>If ... Give you 100!!!!
Hi:
I use the same method for IPC on NT 4.0 SP 5, VC++6.0
I have built two applications, one running as NT service. the other running as a normal Windows application for monitoring the service.
I follow same procedure in both applications. First calling CreateFileMapping() followed by MapViewOfFile(). The problem is if I run NT service first, then run the Windows application, I get a failure saying ERROR_ACCESS_DENIED (05). But this problem doesn't occure if I swap the running order.
The snapshot is provided.
In NT service:
bool CMyService::CreateSharedMem()
{
m_hMap =::CreateFileMapping((HANDLE)0xffffffff,
0,
PAGE_READWRITE,
0,
0x100000,
"mysharedmem");
if(m_hMap == NULL)
return false;
m_pSharedData =::MapViewOfFile(m_hMap,
FILE_MAP_WRITE,
0,
0,
0);
if(m_pSharedData == NULL)
return false;
return true;
}
In monitoring Win application.
bool CSpconsoleApp::CreateSharedMem()
{
m_hMap =::CreateFileMapping((HANDLE)0xffffffff, //or can be an open file handle
0,
PAGE_READWRITE,
0,
0x100000,
"mysharedmem");
if(m_hMap == NULL)
return false;
m_pSharedData =::MapViewOfFile(m_hMap,
FILE_MAP_WRITE,
0,
0,
0);
if(m_pSharedData == NULL)
{
//Get ERROR_ACCESS_DENIED here
int error = GetLastError();
return false;
}
sharedMemAllocated = true;
return true;
}
There is no sync. done in the 2 applications. But if that's the culprit, the problem shall happen regardless of the running order. And VC++ document says if file-mapping object already exist, then CreateFileMapping() is equal to OpenFileMapping().
What could be the reason? Thank you in advance.