读取内存内存映射文件C ++和C#

时间:2020-07-16 15:02:32

标签: c# c++ windows memory-mapped-files

我正在尝试使用内存映射文件共享从C ++到C#的结构。到目前为止,我设法在文件上进行写操作,但是无法读取C#中的内容。

  1. 使用C ++发送数据
struct Bus_1553 // this is the structure to send
{
    string name;
    int directions; 
};

struct Bus_1553* p_1553; // set the pointer to it
HANDLE handle; // create the handle


// here we define the data to send
string name = "IFF";
int directions = 3;

bool startShare() // Open the shared memory
{
    try
    {
        handle = CreateFileMappingW(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, sizeof(Bus_1553), L"DataSend");
        p_1553 = (struct Bus_1553*) MapViewOfFile(handle, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, sizeof(Bus_1553));
        return true;
    }
    catch (...)
    {
        return false;
    }

}


int main()
{

    if (startShare() == true)
    {

        while (true)
        {
            if (p_1553 != 0) // populate the memory
            {  

                p_1553->name = name;
                p_1553->directions = directions;
            }

            else
                puts("create shared memory error");
        }
    }
    if (handle != NULL)
        CloseHandle(handle);
    return 0;
} 
  1. 试图用C#阅读
namespace sharedMemoryGET
{
    class sharedMemoryGET
    {
        [StructLayout(LayoutKind.Sequential, Pack = 1)]
        public unsafe struct Bus_Data_1553
        {
            public string name;
            public int directions; // which directions used
        }

        public static MemoryMappedFile mmf;
        public static MemoryMappedViewStream mmfvs;

        static public bool MemOpen() // open the mapped file
        {
            try
            {
                mmf = MemoryMappedFile.OpenExisting("DataSend");
                return true;
            }
            catch
            {
                return false;
            }

        }

        public static void readData()
        {
            if (MemOpen())
                {
                    using (var accessor = mmf.CreateViewAccessor())
                    {
                    accessor.Read(0, out Bus_Data_1553 a);
                    Console.WriteLine(a.name);
                    Console.WriteLine(a.directions);
                    }
                }
            
        }
    }
} 

当要共享的结构中存在字符串时,出现以下错误: 指定的Type必须是不包含引用的结构。

当我删除字符串并仅共享int方向时,我的值为0。有人可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:1)

让我们从C ++版本的问题开始。我将对此加粗,以确保没有人忽略它,这一点非常重要:切勿将指针写入磁盘

std::string是一个指针(实际上是2个指针)的包装,该指针根据需要为您处理分配和重新分配。您绝对不能在任何地方将它们写入“文件”,而必须将这些指针的内容写入。

一种简单的方法(在C语言中很普遍)是简单地定义一个足以容纳数据的缓冲区,然后根据需要使用尽可能多的数据:

struct Bus_1553 // this is the structure to send
{
    char name[128];
    int directions; 
};

要写入name,请使用strcpy_s或您的操作系统等效版本。

现在,一旦您用C ++将此结构写入共享文件,便用C#读取它是关于让系统(编组器)将该字节信息解码为有用的托管对象。您可以通过在结构和字段定义中使用属性来做到这一点:

    [StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Ansi)]
    public struct Bus_Data_1553
    {
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
        public string name;
        public int directions; // which directions used
    }

如果正确使用了编组器,则也不需要unsafe