Link to home
Start Free TrialLog in
Avatar of vinodpilley
vinodpilley

asked on

Shared memory

Hi I am using a shared memory.I created the shared memory
in one application by using CreateFileMapping.
When I open this shared memory in another application
by OpenFileMapping , it returning NULL handle and the errorcode is 5.

 I am using the following code
     hMemory = OpenFileMapping(
                      FILE_MAP_ALL_ACCESS,
                   FALSE,
                   MEMORY_TABLE_NAME      // name of map  object
     
                       );

Can somebody tell me how I can open the share memory.

thanks in advance.
Avatar of robpitt
robpitt

Is the call to CreateFileMapping working ok?

Also what did you specifiy for the hFile when you called CreateFileMapping? If you specified a custom file, what access was that file opened with?

Rob
Avatar of vinodpilley

ASKER

Yes the CreateFileMapping worked ok.
Following code I used to create shared memory.

     *phMemory = CreateFileMapping(
                      (HANDLE)INVALID_HANDLE_VALUE, // use paging file
                   NULL,                 // no security attributes
                   PAGE_READWRITE,       // read/write access
                   0,                    // size: high 32-bits
                   (sizeof SharedReqRespTable) * MAX_TABLE_SIZE,      // size: low 32-bits
                   MEMORY_TABLE_NAME      // name of map object
                    );    
Yes the CreateFileMapping worked ok.
Following code I used to create shared memory.

     *phMemory = CreateFileMapping(
                      (HANDLE)INVALID_HANDLE_VALUE, // use paging file
                   NULL,                 // no security attributes
                   PAGE_READWRITE,       // read/write access
                   0,                    // size: high 32-bits
                   (sizeof SharedReqRespTable) * MAX_TABLE_SIZE,      // size: low 32-bits
                   MEMORY_TABLE_NAME      // name of map object
                    );    
Yes the CreateFileMapping worked ok.
Following code I used to create shared memory.

     *phMemory = CreateFileMapping(
                      (HANDLE)INVALID_HANDLE_VALUE, // use paging file
                   NULL,                 // no security attributes
                   PAGE_READWRITE,       // read/write access
                   0,                    // size: high 32-bits
                   (sizeof SharedReqRespTable) * MAX_TABLE_SIZE,      // size: low 32-bits
                   MEMORY_TABLE_NAME      // name of map object
                    );    
Yes the CreateFileMapping worked ok.
Following code I used to create shared memory.

     *phMemory = CreateFileMapping(
                      (HANDLE)INVALID_HANDLE_VALUE, // use paging file
                   NULL,                 // no security attributes
                   PAGE_READWRITE,       // read/write access
                   0,                    // size: high 32-bits
                   (sizeof SharedReqRespTable) * MAX_TABLE_SIZE,      // size: low 32-bits
                   MEMORY_TABLE_NAME      // name of map object
                    );    
Yes the CreateFileMapping worked ok.
Following code I used to create shared memory.

     *phMemory = CreateFileMapping(
                      (HANDLE)INVALID_HANDLE_VALUE, // use paging file
                   NULL,                 // no security attributes
                   PAGE_READWRITE,       // read/write access
                   0,                    // size: high 32-bits
                   (sizeof SharedReqRespTable) * MAX_TABLE_SIZE,      // size: low 32-bits
                   MEMORY_TABLE_NAME      // name of map object
                    );    
Yes the CreateFileMapping worked ok.
Following code I used to create shared memory.

     *phMemory = CreateFileMapping(
                      (HANDLE)INVALID_HANDLE_VALUE, // use paging file
                   NULL,                 // no security attributes
                   PAGE_READWRITE,       // read/write access
                   0,                    // size: high 32-bits
                   (sizeof SharedReqRespTable) * MAX_TABLE_SIZE,      // size: low 32-bits
                   MEMORY_TABLE_NAME      // name of map object
                    );    
Yes the CreateFileMapping worked ok.
Following code I used to create shared memory.

     *phMemory = CreateFileMapping(
                      (HANDLE)INVALID_HANDLE_VALUE, // use paging file
                   NULL,                 // no security attributes
                   PAGE_READWRITE,       // read/write access
                   0,                    // size: high 32-bits
                   (sizeof SharedReqRespTable) * MAX_TABLE_SIZE,      // size: low 32-bits
                   MEMORY_TABLE_NAME      // name of map object
                    );    
Avatar of Axter
vinodpilley,
When openning your question on a current session, please use the RELOAD link on the upper left corner of your question.  That will stop the repeat post.
How are you verifing that CreateFileMapping() is occuring before the other application does the OpenFileMapping()?

Make sure that you are using the same name for both functions.
Check the following veriable MEMORY_TABLE_NAME?
Make sure it's exactly the same.
I am using CreateFileMapping() in one service program.
First I am running that service and then I am running my second exe which is opening that table.Now what I  found that when I open the table with read permission , it will return successfull.but when I am open that table with  FILE_MAP_ALL_ACCESS
permission ,it will returning access deny error code.
So there must be some permission problem.
lpFileMappingAttributes
Pointer to a SECURITY_ATTRIBUTES structure that determines whether the returned handle can be inherited by child processes. If lpFileMappingAttributes is NULL, the handle cannot be inherited.
Windows NT: The lpSecurityDescriptor member of the structure specifies a security descriptor for the new file-mapping object. If lpFileMappingAttributes is NULL, the file-mapping object gets a default security descriptor.

Try declaring a SECURITY_ATTRIBUTES variable, and setting it to zero.  Then pass that instead of NULL.

SECURITY_ATTRIBUTES sa;
memset(&sa,0,sizeof(sa));
I am using CreateFileMapping() in one service program.
First I am running that service and then I am running my second exe which is opening that table.Now what I  found that when I open the table with read permission , it will return successfull.but when I am open that table with  FILE_MAP_ALL_ACCESS
permission ,it will returning access deny error code.
So there must be some permission problem.
I am using CreateFileMapping() in one service program.
First I am running that service and then I am running my second exe which is opening that table.Now what I  found that when I open the table with read permission , it will return successfull.but when I am open that table with  FILE_MAP_ALL_ACCESS
permission ,it will returning access deny error code.
So there must be some permission problem.
vinodpilley,
When openning your question on a current session, please use the RELOAD link on the upper left corner
of your question.  That will stop the repeat post.

You will continue to repost your same message over and over again, if you don't use the Reload Question link.
I am using CreateFileMapping() in one service program.
First I am running that service and then I am running my second exe which is opening that table.Now what I  found that when I open the table with read permission , it will return successfull.but when I am open that table with  FILE_MAP_ALL_ACCESS
permission ,it will returning access deny error code.
So there must be some permission problem.
A service? Ahhh, in that case you need to ensure you use a null DACL in the security descriptor when you create the mapping, otherwise all non-services only be allowed read access.

Try this:

SECURITY_DESCRIPTOR sd;
SECURITY_ATTRIBUTES sa;    

InitializeSecurityDescriptor(&sd,SECURITY_DESCRIPTOR_REVISION);
SetSecurityDescriptorDacl(&sd,TRUE,(PACL)NULL,FALSE);

sa.nLength=sizeof(sa);
sa.lpSecurityDescriptor=&sd;
sa.bInheritHandle=TRUE;

CreateFileMapping(INVALID_HANDLE_VALUE,&sa,....
ASKER CERTIFIED SOLUTION
Avatar of robpitt
robpitt

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks very much for helping me ,I really appreciate
it.