Accessing Shared Memory in a Win32 dll from a C# application

In interesting question was posted on www.gotdotnet.com.  The post centered around the issue of accessing shared memory located in a win32 c++ dll from a c# application. I answered the post and I felt it was an interesting enough piece of code to warrent a blog.

The example is really straightforward and doesn't require you to be a rocket scientest to figure it out, but it does demonstrate the power and flexibility of .Net and C#.

//====================           Code Excerpt from the main cpp file              ======================
#include "stdafx.h"
//=================           Here we are setting up the shared memory area   =====================
#pragma data_seg (".SHAREDMEMORY")
     int
gshared_nTest=0;
#pragma data_seg
()
#pragma comment(linker,"/SECTION:.SHAREDMEMORY,RWS")

BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved )
{

     return
TRUE;
}
//=============================================================================================
//====================== Here we are writing wrappers to the shared memory area ===========================
//=You must declare it as an Extern "C" to prevent name mangling. This is absolutely necessary in order to import it into c#  =
//=============================================================================================
extern
"C" __declspec(dllexport) int __stdcall getMyData()
{
     return
gshared_nTest;
}
extern "C" __declspec(dllexport) void __stdcall setMyData( int
data )
{
     gshared_nTest = data;
}

//==============  Here is the main piece of code from a simple c# console application =================
using
System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace TestShared {
     class TestShared 
     {  
          //==============  here we are importing the methods from the win32 dll into the c# console application =================
          [DllImport(@"C:\VS Projects\Shared\Debug\Shared.dll")]  
          public static extern int getMyData();  
          [DllImport(@"C:\VS Projects\Shared\Debug\Shared.dll")]  
          public static extern void setMyData(int data);  


          [STAThread]  
          static void Main(string[] args)  
          {  
               //==============  here i am incrementing the value =================
     
         
setMyData( getMyData() + 100 );  
               //== i use a message box so that i can have multiple console applications running at once and it will pause at the messagebox (if i don't click ok)
               //== i do this so i can see the values changing in the shared memory.
              
MessageBox.Show( getMyData().ToString() );  
         
     }
}

That's it. Accessing shared memory in a c++ dll is really not hard. If you are building an application from scratch, then I would probably create a complete .net from the start; however, if you have a great deal of logic in a c++ dll that you wish to have work side-by-side with a new .Net application, this is a plausable approach. It enables you to evolve your existing application while allowing you to use the latest .Net technologies.

 

2 Comments

Comments have been disabled for this content.