亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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# 程序讀?。簆ublic 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 關注
  • 171 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號