在 C 和 C# 之间传递包含结构数组的结构(DLL 和 P 调用)
我有一个带有一些复杂结构的 C dll,我确实是 C# 的新手:
typedef struct {
int a;
int b;
} simple_struct;
typedef struct {
int d;
int e;
simple_struct f[20];
short g;
simple_struct h[20];
short i;
} complex_struct;
问题是我无法将我的 C# 应用程序与此结构连接起来!
在DLL中有一个函数GetData(complex_struct* myStruct),我应该从C#调用它,所以我创建了:
[StructLayout(LayoutKind.Sequential, Pack = 1)]
unsafe struct simple_struct {
public int a;
public int b;
} ;
[StructLayout(LayoutKind.Sequential, Pack = 1)]
unsafe struct complex_struct {
public int d;
public int e;
public simple_struct[] f;
public short g;
public simple_struct[] h;
public short i;
} ;
但问题是,当我将complex_struct作为GetData的参数传递时,所有字段都从我那里填充回来,但不是我的两个 simple_struct 数组(我的意思是 f 和 h)!他们的值是空的!
有人可以帮助我吗,谢谢
你好,感谢你的回复,
我已经按照你所说的做了,但是当我调用 GetData 时,我仍然遇到另一个问题,进程崩溃,没有任何消息(一种异常):
这是我的C 锐代码: 命名空间 dll_test_import_c_sharp { 班级计划 { [StructLayout(LayoutKind.Sequential, Pack = 1)] 结构简单_结构{ 公共整数a; 公共 int b; };
[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct complex_struct {
public int d;
public int e;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
public simple_struct[] f;
public short g;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
public simple_struct[] h;
public short i;
} ;
[DllImport("test_dll.dll", CharSet = CharSet.Unicode)]
static extern int GetData(ref complex_struct a);
static void Main(string[] args)
{
complex_struct a = new complex_struct();
GetData(ref a);
return;
}
}
}
我已经做了很多 printf i GetData 并且所有这些都执行得很好,看起来“返回”指令崩溃了!
我尝试通过 ref 或 by out 调用 GetData,但它们都不起作用...
您好,感谢您的回复,
我已经按照您所说的做了,但是当我调用 GetData 时,我仍然遇到另一个问题,进程崩溃没有任何消息(一种异常):
这是我的 C Sharp 代码: 命名空间 dll_test_import_c_sharp { 班级计划 { [StructLayout(LayoutKind.Sequential, Pack = 1)] 结构简单_结构{ 公共整数a; 公共 int b; };
[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct complex_struct {
public int d;
public int e;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
public simple_struct[] f;
public short g;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
public simple_struct[] h;
public short i;
} ;
[DllImport("test_dll.dll", CharSet = CharSet.Unicode)]
static extern int GetData(ref complex_struct a);
static void Main(string[] args)
{
complex_struct a = new complex_struct();
GetData(ref a);
return;
}
}
}
我已经做了很多 printf i GetData 并且所有这些都执行得很好,看起来“返回”指令崩溃了!
我尝试通过 ref 或 out 调用 GetData,但它们都不起作用......
I have C dll with some complicated struct and I ma really a newbie in C#:
typedef struct {
int a;
int b;
} simple_struct;
typedef struct {
int d;
int e;
simple_struct f[20];
short g;
simple_struct h[20];
short i;
} complex_struct;
The issue is that I am not able to interface my C# application with this structure!!
In the DLL there is a function GetData(complex_struct* myStruct) and I shoud call it from C#, so I created:
[StructLayout(LayoutKind.Sequential, Pack = 1)]
unsafe struct simple_struct {
public int a;
public int b;
} ;
[StructLayout(LayoutKind.Sequential, Pack = 1)]
unsafe struct complex_struct {
public int d;
public int e;
public simple_struct[] f;
public short g;
public simple_struct[] h;
public short i;
} ;
but the problem is that when I pass complex_struct as argument of GetData, all the fields are filled back form me, but not my two array of simple_struct (I mean f and h)!! Their values are null!!
Can some one help me please, thanks
Hi and thanks for your reply,
I have done like what you said, but I still have another issue when I call GetData, the process crashes without any message (a kind of Exception):
This is my C sharp code:
namespace dll_test_import_c_sharp
{
class Program
{
[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct simple_struct {
public int a;
public int b;
} ;
[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct complex_struct {
public int d;
public int e;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
public simple_struct[] f;
public short g;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
public simple_struct[] h;
public short i;
} ;
[DllImport("test_dll.dll", CharSet = CharSet.Unicode)]
static extern int GetData(ref complex_struct a);
static void Main(string[] args)
{
complex_struct a = new complex_struct();
GetData(ref a);
return;
}
}
}
I have done a lot of printf i GetData and all of them are well executed, it seems like the 'return' instruction crashes!!
I tried to call GetData by ref or by out and both of them don't work...
Hi and thanks for your reply,
I have done like what you said, but I still have another issue when I call GetData, the process crashes without any message (a kind of Exception):
This is my C sharp code:
namespace dll_test_import_c_sharp
{
class Program
{
[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct simple_struct {
public int a;
public int b;
} ;
[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct complex_struct {
public int d;
public int e;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
public simple_struct[] f;
public short g;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
public simple_struct[] h;
public short i;
} ;
[DllImport("test_dll.dll", CharSet = CharSet.Unicode)]
static extern int GetData(ref complex_struct a);
static void Main(string[] args)
{
complex_struct a = new complex_struct();
GetData(ref a);
return;
}
}
}
I have done a lot of printf i GetData and all of them are well executed, it seems like the 'return' instruction crashes!!
I tried to call GetData by ref or by out and both of them don't work...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要更改 struct 上的数组定义以指定它是按值/内联数组
You need to change the array definition on the
struct
to specify that it's a by value / inline array