We help IT Professionals succeed at work.

creating shared memory

prashant_gh
prashant_gh asked
on
Medium Priority
206 Views
Last Modified: 2013-11-20
I am trying to create a block of memory, which is a file ,
which can be simultaneously accessed by two different process.
I have done it thro' createfilemapping(....) and
mapviewoffile(....) function.
I 'm also taking the current process through hmutex and locking it.
Now my problem is filemapping and view both are succesfull
but i am not able to access data thro' void * pointer returned by mapviewoffile(....) . I have tried everything typecasting and then accesing it.

thanks in advance

prashant
Comment
Watch Question

Commented:
Here's an example; hope this helps :
1. Create shared memory
 // Create a named file mapping object.
        hMapObject = CreateFileMapping(
            INVALID_HANDLE_VALUE, // use paging file
            NULL,                 // no security attributes
            PAGE_READWRITE,       // read/write access
            0,                    // size: high 32-bits
            SHMEMSIZE,            // size: low 32-bits
            "dllmemfilemap");     // name of map object
       
     if (hMapObject == NULL)                
          return FALSE;  
       
// The first process to attach initializes memory.
bInit = (GetLastError() != ERROR_ALREADY_EXISTS);  
       
/ Get a pointer to the file-mapped shared memory.
lpvMem = MapViewOfFile(
            hMapObject,     // object to map view of
            FILE_MAP_WRITE, // read/write access
            0,              // high offset:  map from
            0,              // low offset:   beginning
            0);             // default: map entire file
       
if (lpvMem == NULL)
     return FALSE;  

2. Using pointer to shared memory : I'm sharing booleans (size is 4 bytes) in the shared memory.
if (*((LPBOOL)lpvMem + 4)) //drawer already claimed
{                        
//device is claimed by other application, sleep sometime and try again later.
if (!ReleaseMutex(hMutex))
{
     dwError = GetLastError();
}
break;
}
else //drawer not claimed yet
{
*((LPBOOL)lpvMem + 4) = TRUE;
 
CERTIFIED EXPERT

Commented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
Answered by  jlsjls
Please leave any comments here within the next four days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

khkremer
EE Cleanup Volunteer

Explore More ContentExplore courses, solutions, and other research materials related to this topic.