[問題] 關於C#結構指標問題

看板C_Sharp (C#)作者 (...)時間18年前 (2007/07/08 19:10), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串1/5 (看更多)
以往在C++下需要把整筆byte array填入struct裡 我是用下面寫法 struct test_data { int a; int b[2]; }*test; unsigned char buf[12]; test=(struct test_data *)&buf[0]; 可是在C#上好像完全不能這樣寫 原本也想說把整個struct寫到DLL然後C#上呼叫DLL檔 但是對DLL寫法不是很熟 而就算寫完了也不知道C#上該怎嚜寫比較好 這是DLL檔部份程式碼 我也不確定這樣寫對不對: struct test_data { int a; int b[2]; }; extern "C" __declspec(dllexport) struct test_data test(unsigned char *ptr) { test_data *test; test=(struct test_data *)ptr; return *test ; } 至於C#上 我只知道要先用using System.Runtime.InteropServices; 然後用 [DllImport("dll_test.dll")] 至於 [ StructLayout( LayoutKind.Sequential)] 的東西 我不確定是不是必須寫 因為如果必須寫在C#裡宣告struct裡如果有array(如int b[2]那項)好像挺麻煩 想請問各位大大有何方法可以快速在C#裡把array的值丟入struct呢 謝謝 <(_ _)> -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.116.202.152
文章代碼(AID): #16aCOfqm (C_Sharp)
文章代碼(AID): #16aCOfqm (C_Sharp)