将内存文件C ++共享到C#

将内存文件C ++共享到C#

问题描述:

我试图通过内存文件共享数组,我的c ++项目将创建一个数组,然后我的c#项目将使用基于

的代码获取this.im 这里我可以得到号码而不是数组



我尝试过:



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;
}





和边c#







and side 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();
        }

    }
}

你遇到的第一个错误是C代码中myArray的字段偏移量 - 假设C ++ int 是32位,这应该是264,而不是零。

第二个错误是你做的不要将C#数组声明为 fixed 。它应声明为 public fixed int myArray [70]
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].