编组C结构的C# [英] Marshaling c structures in c#
问题描述
我有一个'C'DLL,可以采取结构作为输入,我要调用的dll从我的C#程序
休耕是样品的结构,我有在C,我有元帅这些结构在我的C#code
是我在做正确的还是错误?
实际的C结构:
typedef结构的过程
{
字符code [8];
}程序;
typedef结构的DateField
{
CHAR日[10];
}的DateField;
typedef结构p45_clsgs
{
程序p45_clsg;
的DateField p45_clsgdte;
} p45_clsgs;
C#:
[StructLayout(LayoutKind.Sequential),序列化]
结构程序
{
//字符code [];
[MarshalAsAttribute(UnmanagedType.ByValTStr,SizeConst = 8)]
公共字符串code;
}
[StructLayout(LayoutKind.Sequential),序列化]
结构的DateField
{
// CHAR日[10];
[MarshalAsAttribute(UnmanagedType.ByValTStr,SizeConst = 10)]
公共字符串日期;
}结构p45_clsgs
{
公共过程p45_clsg;
公众的DateField p45_clsgdte;
}
是我的C#结构是正确的C结构?
什么是休耕C语言结构相当于C#结构
typedef结构p45_cldxed24
{
过程p45_cldxed [8];
} p45_cldxed24;
的(划伤我原来的答复)的
您需要在每个结构的顶部有这样的:
[StructLayout(LayoutKind.Sequential,字符集= CharSet.Ansi)
请注意在字符集
命名参数。这一点很重要,否则你会与wchar_t的数组,而不是字符数组结束。
i have one 'C' 'DLL' which can take structures as input, i have to call that dll from my c# program
fallowing are the sample structures i have in c, i have to marshal those structures in my c# code
is i'm doing correct or wrong?
Actual C structures:
typedef struct procedure
{
char code[8];
}procedure;
typedef struct datefield
{
char date[10];
}datefield;
typedef struct p45_clsgs
{
procedure p45_clsg;
datefield p45_clsgdte;
}p45_clsgs;
C#:
[StructLayout(LayoutKind.Sequential), Serializable]
struct procedure
{
//char code[];
[MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 8)]
public string code;
}
[StructLayout(LayoutKind.Sequential), Serializable]
struct datefield
{
//char date[10];
[MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 10)]
public string date;
}
struct p45_clsgs
{
public procedure p45_clsg;
public datefield p45_clsgdte;
}
Is my C# structs are correct for the C structs?
What is the equivalent C# structure for the fallowing c structure
typedef struct p45_cldxed24
{
procedure p45_cldxed[8];
}p45_cldxed24;
(Scratch my original answer)
You need to have this at the top of each struct:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
Note the CharSet
named parameter. That's important, or you'll end up with wchar_t arrays instead of char arrays.
这篇关于编组C结构的C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!