Click here to Skip to main content
15,892,737 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i trying to share a array via memory file, my c++ project will create a array then my c# project will take this.im using code based from
Here i can get number but not the array

What I have tried:

C++

<pre lang="c++"><pre>#include <stdio.h>
#include <tchar.h>
#include <iostream>
using namespace std;

#define WIN32_LEAN_AND_MEAN
#define _WIN32_WINNT _WIN32_WINNT_WINXP

// System Include
#include <windows.h>
#include <winsock2.h>

struct INFO
{
	char Name[MAX_PATH];
	int Number;
	int myAray[70];
};

HANDLE FileMappingHandle;
INFO* FileMapping;

void EntryProc()
{
	if ((FileMappingHandle = CreateFileMapping(INVALID_HANDLE_VALUE, 0, PAGE_READWRITE, 0, sizeof(INFO), "Local\\INFO_MAPPING")) == 0)
	{
		return;
	}

	if ((FileMapping = (INFO*)MapViewOfFile(FileMappingHandle, FILE_MAP_ALL_ACCESS, 0, 0, sizeof(INFO))) == 0)
	{
		return;
	}

	for (int i = 0; i < 70; i++)
	{
		FileMapping->myAray[i] = i;
		cout << '\n' << "Number->" << FileMapping->myAray[i] << endl;

	}

	FileMapping->Number = 1337;
	printf("FileMapping->Number: %d", FileMapping->Number);
}


int main()
{
	EntryProc();

	
	do {
		cout << '\n' << "Press the Enter key to continue.";
	} while (cin.get() != '\n');
	return 0;
}


and side c#


C#
<pre>
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using System.IO.MemoryMappedFiles;
using System.Security.Principal;
using System.IO;

namespace ReadyFilememoryCSharp
{
    class Program
    {
        [StructLayout(LayoutKind.Explicit, CharSet = CharSet.Ansi,Size =12)]
        public unsafe struct INFO
        {
            [FieldOffset(0)]
            public fixed byte Name[260];
            [FieldOffset(260)]
            public int Number;
            [FieldOffset(0)]
            public int[] myArray;
        }

        private static MemoryStream Convert(int[] Num, byte[] Bytes)
        {
            Buffer.BlockCopy(Num, 0, Bytes, 0, Bytes.Length);
            MemoryStream stream = new MemoryStream(Bytes);
            return stream;
        }
        static void Main(string[] args)
        {
            using (var memMapFile = MemoryMappedFile.CreateOrOpen(
                "Local\\INFO_MAPPING",
                1024,
                MemoryMappedFileAccess.ReadWriteExecute,
                MemoryMappedFileOptions.None,
                System.IO.HandleInheritability.Inheritable))
            {
                using (var accessor = memMapFile.CreateViewAccessor())
                {
                    accessor.Read<INFO>(0, out INFO data);
                    string name;
            
                    unsafe
                    {
                        name = new String((sbyte*)data.Name, 0, 260);
         

                    }
                 
                    Console.WriteLine(data.Number);
                    Console.WriteLine(data.myArray);
                }
            }

            Console.ReadKey();
        }

    }
}
Posted
Updated 17-Nov-18 10:19am

1 solution

The first error that you have is the field offset for myArray in the C code - assuming that the C++ int is 32-bit, this should be 264, not zero.
The second error is that you did not declare the C# array as fixed. It should be declared as public fixed int myArray[70].
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900