Solved

Shared memory

Posted on 2001-06-19
20
389 Views
Last Modified: 2008-02-26
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.
0
Comment
Question by:vinodpilley
  • 12
  • 5
  • 3
20 Comments
 
LVL 5

Expert Comment

by:robpitt
ID: 6207065
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
0
 

Author Comment

by:vinodpilley
ID: 6207078
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
                    );    
0
 

Author Comment

by:vinodpilley
ID: 6207097
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
                    );    
0
 

Author Comment

by:vinodpilley
ID: 6207160
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
                    );    
0
 

Author Comment

by:vinodpilley
ID: 6207180
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
                    );    
0
 

Author Comment

by:vinodpilley
ID: 6207213
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
                    );    
0
 

Author Comment

by:vinodpilley
ID: 6207224
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
                    );    
0
 

Author Comment

by:vinodpilley
ID: 6207259
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
                    );    
0
 
LVL 30

Expert Comment

by:Axter
ID: 6207423
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.
0
 
LVL 30

Expert Comment

by:Axter
ID: 6207437
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.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:vinodpilley
ID: 6211050
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.
0
 
LVL 30

Expert Comment

by:Axter
ID: 6211081
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.

0
 
LVL 30

Expert Comment

by:Axter
ID: 6211088
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));
0
 

Author Comment

by:vinodpilley
ID: 6211111
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.
0
 

Author Comment

by:vinodpilley
ID: 6211148
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.
0
 
LVL 30

Expert Comment

by:Axter
ID: 6211169
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.
0
 

Author Comment

by:vinodpilley
ID: 6211272
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.
0
 
LVL 5

Expert Comment

by:robpitt
ID: 6212310
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,....
0
 
LVL 5

Accepted Solution

by:
robpitt earned 20 total points
ID: 6212338
PS there used to be a KB article for this but it seems to have disappeared. The number is Q193073.
0
 

Author Comment

by:vinodpilley
ID: 6212550
Thanks very much for helping me ,I really appreciate
it.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now