为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

C#
MMMHUHU 2022-07-23 17:48:17
我编写了一个 c++ 程序,它通过 memorystream 将数组 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++ 程序,它通过 memorystream 将数组 multi 发送到 c# 应用程序。但我不知道如何使用 BlockCopy 来排列多:这是我的程序 C++ 发送数组多
查看完整描述

1 回答

?
狐的传说

TA贡献1804条经验 获得超3个赞

尝试以下:


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


查看完整回答
反对 回复 2022-07-23
  • 1 回答
  • 0 关注
  • 247 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号