Solved

::shm_open()

Posted on 2009-05-03
3
948 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
ID: 24292852
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
ID: 24296632
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
ID: 24316774
???
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

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…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
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.

912 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

17 Experts available now in Live!

Get 1:1 Help Now