繁体   English   中英

读取 Memory Memory 映射文件 C++ 和 ZD7EFA19FBE7D39724FD5ADB60242

[英]Read Memory Memory Mapped File C++ and C#

我正在尝试使用 memory 映射文件共享从 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);
                    }
                }
            
        }
    }
} 

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

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

让我们从 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM