[問題] 關於C#結構指標問題
以往在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
討論串 (同標題文章)
以下文章回應了本文:
完整討論串 (本文為第 1 之 5 篇):
C_Sharp 近期熱門文章
PTT數位生活區 即時熱門文章