Solved

Shared memory

Posted on 2001-06-19
20
388 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

757 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