Click here to Skip to main content
15,879,095 members
Articles / Desktop Programming / Win32

How to do CreateFileMapping in a C++ DLL and access it in C#, VB and C++

Rate me:
Please Sign up or sign in to vote.
5.00/5 (8 votes)
10 Mar 2009CPOL 82.6K   2.2K   57   6
This is yet another example for memory mapped files. What is cool though is unlike other samples, I have a SetData(TCHAR* Key, TCHAR* value) / GetData(TCHAR* key) pattern here.

Introduction

This is a sample of how to use a shared memory in SetData/GetData pattern like a map.

C++
void SetData(TCHAR* key, TCHAR* value)
TCHAR* GetData(TCHAR* key)

This pattern can be used to store and retrieve binary data of variable lengths (like those of images) with slight improvement to the code.

Background

For basic tutorial on CreateFileMapping, please refer here.

Using the code

The code and the samples explain how to use this shared memory from VB.NET, C#, and C++.

In the code attached, a C++ DLL (named SharedMem.DLL) mimics a map<T,T> (Dictionary/Hashtable) so that it can be used across processes. The data sharing is based on an MMF.

A C# sample will look as shown below:

C#
[DllImport("sharedmem.dll")]
extern static int GetRecordCount();

[DllImport("sharedmem.dll")]
extern static void SetValue(
[MarshalAs(UnmanagedType.LPTStr)] string key,
[MarshalAs(UnmanagedType.LPTStr)] string value);

[DllImport("sharedmem.dll")]
extern static IntPtr GetValue([MarshalAs(UnmanagedType.LPTStr)]string key);

private void OnSetClick(object sender, EventArgs e)
{
     //Set Func
     SetValue(keyText, valueText);
}

private void OnGetClick(object sender, EventArgs e)
{
     //Get
     IntPtr intPtr = GetValue(keyText);
     textBox.Text = Marshal.PtrToStringUni(intPtr);
}

The code is very much self explanatory.

The MMF is created/opened in DLLMain.cpp.

C++
// sharedmem.cpp : Defines the exported functions for the DLL application.
//
#include "stdafx.h"
#include "sharedmem.h"
#include "tchar.h"
#include "stdio.h"
 
extern HANDLE m_hFileMMF, m_pViewMMFFile, hMutex;
 
class CMutex
{
 
public:
    CMutex()
    {
        if(!hMutex){
            hMutex = CreateMutex(
                NULL,              // default security attributes
                FALSE,             // initially not owned
                L"Global\\MMFMutex");             // unnamed mutex
        }
 
        WaitForSingleObject(  hMutex, INFINITE);  
 
    }
 
    ~CMutex()
    {
        ReleaseMutex(hMutex);
    }
 
};
 
void SetRecordCount(int value)
{
    TCHAR* record = (TCHAR*)m_pViewMMFFile;
    swprintf_s(record, MAX_PATH, L"RECCNT=%d#", value);
}
 
extern "C" int GetRecordCount()
{
    TCHAR* record = (TCHAR*)m_pViewMMFFile;
    TCHAR temp[MAX_PATH];
 
    int recordCount = -1;
 
    TCHAR seps[]   = L"=#";
    TCHAR *token1 = NULL;
    TCHAR *next_token1 = NULL;
 
    _tcscpy_s(temp, MAX_PATH, record);
 
    token1 = _tcstok_s ( temp, seps, &next_token1);
 
    if(token1 && _tcscmp(token1, _T("RECCNT")) == 0)
    {
        token1 = _tcstok_s ( NULL, seps, &next_token1);
 
        recordCount = _ttoi(token1);
    }else
    {
        recordCount = 1;
        SetRecordCount(1);
    }
 
    return recordCount;
}
 
 
int nRecordCount = -1;
 
void RemoveValue(TCHAR* key)
{
    TCHAR* record = (TCHAR*)m_pViewMMFFile;
 
    TCHAR temp[MAX_PATH];
 
    nRecordCount = GetRecordCount();
 
    record+=MAX_PATH;
 
    //Try to look. If found, break out of for loop
    //Compact the memory immediately immediately
    //If you get time, strongly advice you to do a lazy compaction
    bool isRecordFound = false;
    int i;
    for(i= 1; i< nRecordCount; i++,record+=MAX_PATH)
    {
        TCHAR seps[]   = L"=#";
        TCHAR *token1 = NULL;
        TCHAR *next_token1 = NULL;
 
        _tcscpy_s(temp, MAX_PATH, record);
 
        token1 = _tcstok_s ( temp, seps, &next_token1);
 
 
        if(_tcscmp(token1, key) == 0)
        {
            isRecordFound = true;
            break;
        }
    }
 
    //start moving the records 
    for(; i< nRecordCount-1; i++, record+=MAX_PATH)
    {
        TCHAR* nextRecord = record + MAX_PATH;
        _tcscpy_s(record, MAX_PATH, nextRecord);
    }
}

TCHAR* IfExists(TCHAR* key, TCHAR** value = NULL)
{
    TCHAR* record = (TCHAR*)m_pViewMMFFile;
 
    TCHAR temp[MAX_PATH];
 
    nRecordCount = GetRecordCount();
 
    record+=MAX_PATH;
    for(int i=1; i< nRecordCount; i++,record+=MAX_PATH)
    {
        TCHAR seps[]   = L"=#";
        TCHAR *token1 = NULL;
        TCHAR *next_token1 = NULL;
 
        _tcscpy_s(temp, MAX_PATH, record);
 
        token1 = _tcstok_s ( temp, seps, &next_token1);
 
        if(_tcscmp(token1, key) == 0)
        {
            token1 = _tcstok_s ( NULL, seps, &next_token1);
 
            //return a copy of the value
            if(value!=NULL)
            {
                int len = _tcslen(token1)+1;
                *value = new TCHAR(len);
                _tcscpy_s(*value, len, token1);
            }
 
            return record;
        }
    }
 
    return NULL;
}
 
extern "C" TCHAR* GetValue(TCHAR* key)
{
    TCHAR* sRetVal = new TCHAR[MAX_PATH];
 
    CMutex mutex;
 
    TCHAR* data = NULL;
 
    if(m_pViewMMFFile)
    {
        IfExists(key, &data);
    }
 
    return data;
}
 
 
extern "C" void SetValue(TCHAR* key, TCHAR* value)
{
    CMutex mutex;
 
    if(m_pViewMMFFile )
    {
        if(value == NULL)
        {
            RemoveValue(key);
        }
        else
        {
            TCHAR* data = IfExists(key);
            if(data == NULL)
            {
                data = new TCHAR[MAX_PATH];
                swprintf_s(data, MAX_PATH, L"%s=%s#", key, value);
 
                //Add to end of the MMF
                TCHAR* record = (TCHAR*)m_pViewMMFFile;
                record += MAX_PATH*nRecordCount;
                nRecordCount++;
 
                SetRecordCount(nRecordCount);
 
                _tcscpy_s(record, MAX_PATH, data);
                delete data;
            }
            else
            {
                //Replace existing
                swprintf_s(data, MAX_PATH, L"%s=%s#", key, value);
            }
        }
    }
}

Points of interest

This code needs some update before it can be readily used in production code.

  1. Deleting a key from the MMF (and the resultant memory compaction).
  2. Variable lengths of data. (Hint: Set the size of the data in the MMF.)
  3. Automatic resizing of MMF.

I intend to post the improvements over time.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralAccess violation Exception occured while compiling with VC9 Pin
Member 56497518-Mar-09 23:25
professionalMember 56497518-Mar-09 23:25 
Hello,

I have compiled the sources by using the VSTS2009 and VC9. Everything work O.K. with no error while compiling. But when I start the C# application and press on the Get button I get the following error message:

System.AccessViolationException was unhandled
Message="Es wurde versucht, im geschützten Speicher zu lesen oder zu schreiben. Dies ist häufig ein Hinweis darauf, dass anderer Speicher beschädigt ist."
Source="Client_CSharp"
StackTrace:
bei WindowsFormsApplication1.Form1.GetValue(String key)
bei WindowsFormsApplication1.Form1.button2_Click(Object sender, EventArgs e) in F:\SOURCE\CodeProject\MMF_SetData_GetData\tubelite\CSharpConsumer\Form1.cs:Zeile 46.
bei System.Windows.Forms.Control.OnClick(EventArgs e)
bei System.Windows.Forms.Button.OnClick(EventArgs e)
bei System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
bei System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
bei System.Windows.Forms.Control.WndProc(Message& m)
bei System.Windows.Forms.ButtonBase.WndProc(Message& m)
bei System.Windows.Forms.Button.WndProc(Message& m)
bei System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
bei System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
bei System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
bei System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
bei System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
bei System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
bei System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
bei System.Windows.Forms.Application.Run(Form mainForm)
bei WindowsFormsApplication1.Program.Main() in F:\SOURCE\CodeProject\MMF_SetData_GetData\tubelite\CSharpConsumer\Program.cs:Zeile 18.
bei System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
bei System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
bei Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
bei System.Threading.ThreadHelper.ThreadStart_Context(Object state)
bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
bei System.Threading.ThreadHelper.ThreadStart()
InnerException:

Has anybody a solution for this?

Thanks,

Michael

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.