Using memory mapped file from .net to C++

Naampie2000 20 Reputation points
2023-04-20T08:35:50.8733333+00:00

Hi, I was attemting to hack together a crude memory mapped bus from VB.net to C++. The way it´s constructed, is trying to be always present on the server side. Which supplies the data on the server side as well. At this point mismatching function calls and stock C++ actually landed me a mapped file that opens without errors. But no discerible data at the client side. Is there a way to match these together so that it actually works?


Imports System.IO.MemoryMappedFiles
Imports System.Threading
Public Class MemoryMapped

    Dim wEn As Integer
    Dim mMF As MemoryMappedFile = MemoryMappedFile.CreateNew("MM", 512)
    Dim mTex As New Mutex(True, "Mutexed", True)

    Public Sub Mapper()
        Try
            If GUIMain.OnLoad = True Then 'inherent to mother program "run once"
               Using vFind = mMF.CreateViewAccessor(0, 4) '(offset, length)

                    'mTex.WaitOne()
                    vFind.Write(0, (100)) '(offset, item "long")                   
                    mTex.ReleaseMutex()

                End Using
            End If

        Catch ex As Exception
            MsgBox("Memory File Error!")
        End Try

    End Sub
End Class



#include <windows.h>
#include <iostream>
#include <conio.h>
#include <string>

using namespace std;

//int Init;

int main()
{
	HANDLE mMF;
	PCHAR vFind = NULL;

	mMF = OpenFileMapping(
		FILE_MAP_ALL_ACCESS,
		FALSE,
		L"MM");

	if (mMF == NULL)
	{
		cout << "Failed File Mapping!" << endl;
	}

	vFind = (PCHAR)MapViewOfFile(
		mMF,
		FILE_MAP_ALL_ACCESS,
		0,
		0,    //
		256); //array size

	if (vFind == NULL)
	{
	    cout << "Failed Map View!" << endl;
	}

    cout << mFind << endl;
    CloseHandle();
    Return 0;
}

Developer technologies | C++
Developer technologies | VB
0 comments No comments
{count} votes

Accepted answer
  1. RLWA32 49,551 Reputation points
    2023-04-20T10:59:34.3333333+00:00

    It appears that your VB.Net code is writing a 32-bit integer value to the memory mapped file. However, in the C++ code the pointer returned by MapViewOfFile is declared as a PCHAR instead of a pointer to an int (i.e., PINT). Following worked for me to print the integer value 100 written by VB to the mapped memory.

    #include <windows.h>
    #include <iostream>
    #include <conio.h>
    #include <string>
    
    using namespace std;
    
    //int Init;
    
    int main()
    {
        HANDLE mMF;
        PINT vFind = NULL;
    
        mMF = OpenFileMapping(
            FILE_MAP_ALL_ACCESS,
            FALSE,
            L"MM");
    
        if (mMF == NULL)
        {
            cout << "Failed File Mapping!" << endl;
        }
    
        vFind = (PINT)MapViewOfFile(
            mMF,
            FILE_MAP_ALL_ACCESS,
            0,
            0,    //
            256); //array size
    
        if (vFind == NULL)
        {
            cout << "Failed Map View!" << endl;
        }
    
        cout << *vFind << endl;
    
        UnmapViewOfFile(vFind);
        CloseHandle(mMF);
        return 0;
    }
    
    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Naampie2000 20 Reputation points
    2023-04-20T09:00:26.0166667+00:00

    Hard to type this in without auto formatting/messes things up. Formatted here to be slightly more legible. mFind at the last cout not it.. supposed to be vFind.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.