Solved

::shm_open()

Posted on 2009-05-03
3
941 Views
Last Modified: 2012-05-06
Hi,

I stuck up with 1 problem where i have to convert Unix code to Windows Code
Please help me to get replacement for following api in to Win32(Windows)

::shm_open() - is, POSIX shared memory objects created .


Thanks...
0
Comment
Question by:palhade
  • 2
3 Comments
 
LVL 12

Accepted Solution

by:
rajeev_devin earned 500 total points
Comment Utility
You can use the function CreateFileMapping

Check this link
http://msdn.microsoft.com/en-us/library/aa366551(VS.85).aspx
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
A little mroe details and sample code (also attached below) here: http://msdn.microsoft.com/en-us/library/aa366551(VS.85).aspx ("Creating Named Shared Memory")

Also take a look at the article at http://www.codeproject.com/KB/threads/sharedmemipc.aspx ("Interprocess Communication using Shared Memory") which cokmes wit hfull source code and explains that issue in detail.
#include <windows.h>

#include <stdio.h>

#include <conio.h>

#include <tchar.h>
 

#define BUF_SIZE 256

TCHAR szName[]=TEXT("Global\\MyFileMappingObject");

TCHAR szMsg[]=TEXT("Message from first process.");
 

int _tmain()

{

   HANDLE hMapFile;

   LPCTSTR pBuf;
 

   hMapFile = CreateFileMapping(

                 INVALID_HANDLE_VALUE,    // use paging file

                 NULL,                    // default security 

                 PAGE_READWRITE,          // read/write access

                 0,                       // max. object size 

                 BUF_SIZE,                // buffer size  

                 szName);                 // name of mapping object

 

   if (hMapFile == NULL) 

   { 

      _tprintf(TEXT("Could not create file mapping object (%d).\n"), 

             GetLastError());

      return 1;

   }

   pBuf = (LPTSTR) MapViewOfFile(hMapFile,   // handle to map object

                        FILE_MAP_ALL_ACCESS, // read/write permission

                        0,                   

                        0,                   

                        BUF_SIZE);           

 

   if (pBuf == NULL) 

   { 

      _tprintf(TEXT("Could not map view of file (%d).\n"), 

             GetLastError()); 
 

	   CloseHandle(hMapFile);
 

      return 1;

   }
 

   

   CopyMemory((PVOID)pBuf, szMsg, (_tcslen(szMsg) * sizeof(TCHAR)));

    _getch();
 

   UnmapViewOfFile(pBuf);
 

   CloseHandle(hMapFile);
 

   return 0;

}
 

--------------------------------->8-------------------------------
 

#include <windows.h>

#include <stdio.h>

#include <conio.h>

#include <tchar.h>
 

#define BUF_SIZE 256

TCHAR szName[]=TEXT("Global\\MyFileMappingObject");
 

int _tmain()

{

   HANDLE hMapFile;

   LPCTSTR pBuf;
 

   hMapFile = OpenFileMapping(

                   FILE_MAP_ALL_ACCESS,   // read/write access

                   FALSE,                 // do not inherit the name

                   szName);               // name of mapping object 

 

   if (hMapFile == NULL) 

   { 

      _tprintf(TEXT("Could not open file mapping object (%d).\n"), 

             GetLastError());

      return 1;

   } 

 

   pBuf = (LPTSTR) MapViewOfFile(hMapFile, // handle to map object

               FILE_MAP_ALL_ACCESS,  // read/write permission

               0,                    

               0,                    

               BUF_SIZE);                   

 

   if (pBuf == NULL) 

   { 

      _tprintf(TEXT("Could not map view of file (%d).\n"), 

             GetLastError()); 
 

	  CloseHandle(hMapFile);
 

      return 1;

   }
 

   MessageBox(NULL, pBuf, TEXT("Process2"), MB_OK);
 

   UnmapViewOfFile(pBuf);
 

   CloseHandle(hMapFile);

 

   return 0;

}

Open in new window

0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
???
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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 goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

771 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

8 Experts available now in Live!

Get 1:1 Help Now