C#Marshal typedef char T_STRING [MAX_STRING_SIZE]

时间:2014-06-12 08:25:18

标签: c# pinvoke marshalling

如何制造a:

[C++]
#define MAX_STRING_SIZE 255
typedef char T_STRING[MAX_STRING_SIZE];
typedef struct
{
    unsigned long m_ID;
    T_STRING m_name;
} Result;

在C#中?

目前我正在这样做(但它不起作用):

[C#]
[StructLayout(LayoutKind.Sequential)]
public struct Result
{
    public uint m_ID;

    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 255)]
    public char[] m_name;
}

我尝试使用IntPtr而不是char []具有相同的非工作结果。有和没有[MarshalAs(...)]。

1 个答案:

答案 0 :(得分:0)

Marshal就像这样:

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct Result
{
    public uint m_ID;

    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 255)]
    public string m_name;
}