作者sky777 (...)
看板C_Sharp
标题[问题] 关於C#结构指标问题
时间Sun Jul 8 19:10:32 2007
以往在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