繁体   English   中英

Buffer.BlockCopy Array 2d C++ to C# 共享内存

[英]Buffer.BlockCopy Array 2d c++ to C# shared memory

我编写了一个 C++ 程序,它通过内存流将数组 multi 发送到 C# 应用程序。 但我不知道热使用 BlockCopy 来排列多个:

这是我的程序 C++,它发送数组多

struct Pair {

    std::pair<int, int> players;
};

struct Pair* p;
HANDLE handle;

float dataSend[70];

bool startShare()
{
    try
    {
        handle = CreateFileMappingW(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, sizeof(Pair), L"DataSend");
        p = (struct Pair*) MapViewOfFile(handle, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, sizeof(Pair));
        return true;
    }
    catch (...)
    {
        return false;
    }

}
for (int i = 0; i < 70; i++)
{

    int r1 = rand() % 10 + 1;
    int r2 = rand() % 10 + 1;
    p->players = { r1,r2 };

}

示例输出:

 players  0 - [10][2]
 players  1 - [100][22]
 players 2 - [1][26]
 players 3 - [50][211]
 players 4 - [32][23]

我的 C# 程序读取:

public static int[,] data = new int[70, 1];
public static MemoryMappedFile mmf;
public static MemoryMappedViewStream mmfvs;

static public bool MemOpen()
{
    try
    {
        mmf = MemoryMappedFile.OpenExisting("DataSend");
        mmfvs = mmf.CreateViewStream();
        return true;
    }
    catch
    {
        return false;
    }

}

// here need be somethings like   byte[,] bPosition = new byte[70,1];
byte[] bPosition = new byte[70];
mmfvs.Read(bPosition, 0, 100);
Buffer.BlockCopy(bPosition, 0, data, 0, bPosition.Length);
 for (int i = 0; i< data.Length; i++)
 {
    for(int j=0;j<1;j++)
    {

      Console.WriteLine(data[i][j]);

    }

}

示例接收:

 data  0 - [10][2]
 data  1 - [100][22]
 data 2 - [1][26]
 data 3 - [50][211]
 data 4 - [32][23]

我编写了一个 C++ 程序,它通过内存流将数组 multi 发送到 C# 应用程序。 但我不知道热使用 BlockCopy 来排列多个:

这是我的程序 C++,它发送数组多

尝试以下:

              MemoryStream stream = new MemoryStream();
              byte[] buffer = new byte[70 * 2 * sizeof(int)];
              stream.Read(buffer, 0, 70 * 2 * sizeof(int));
              for (int i = 0; i< 70; i++)
              {
                  for (int j = 0; j < 2; j++)
                  {
                      Console.WriteLine(BitConverter.ToUInt32(buffer,(i * 2 * sizeof(int)) + (j * sizeof(int))));
                  }
              }

           IntPtr bufferPtr = Marshal.AllocHGlobal(BUFFER_SIZE);

            //you need to allocate memory before calling the routing

            byte[] buffer = new byte[BUFFER_SIZE];
            Marshal.Copy(bufferPtr, buffer, 0, BUFFER_SIZE);
            for (int i = 0; i < 70; i++)
            {
                for (int j = 0; j < 2; j++)
                {
                    Console.WriteLine(BitConverter.ToUInt32(buffer, (i * 2 * sizeof(int)) + (j * sizeof(int))));
                }
            }

            //method 2
            int[,] array = new int[40, 2];
            IntPtr bufferPtr2 = Marshal.AllocHGlobal(Marshal.SizeOf(array));

            //call routine then you code below to fill managed memory.
            Marshal.PtrToStructure(bufferPtr2, array);

暂无
暂无

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

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