Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将C#struct转换为C struct?_C#_C_Struct_Marshalling - Fatal编程技术网

如何将C#struct转换为C struct?

如何将C#struct转换为C struct?,c#,c,struct,marshalling,C#,C,Struct,Marshalling,这是我到目前为止的C#struct。每个字段上方的注释都是用C输入的。 如果有人能验证一下,我会很高兴。听起来你好像在试图获取成员内容中定义的C结构的C结构。如果是这样的话,那么我相信你需要以下几点 [StructLayout(LayoutKind.Sequential, Size = 280), Serializable] public struct AESContext { /// int nr; [MarshalAsAttribute(UnmanagedType.I4,

这是我到目前为止的C#struct。每个字段上方的注释都是用C输入的。
如果有人能验证一下,我会很高兴。

听起来你好像在试图获取成员内容中定义的C结构的C结构。如果是这样的话,那么我相信你需要以下几点

[StructLayout(LayoutKind.Sequential, Size = 280), Serializable]
public struct AESContext
{
    /// int nr; 
    [MarshalAsAttribute(UnmanagedType.I4, SizeConst = 4)]
    public int nr;

    /// unsigned long *rk;
    [MarshalAsAttribute(UnmanagedType.U4, SizeConst = 4)]
    public uint rk;

    // unsigned long buf[68];
    [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 68)]
    public uint[] buf;
}
基本变化

  • 不要在
    StructLayout
    中指定
    SizeConst
    ,除非您试图创建一个大小不同(通常)大于其内容的结构。这样做并不常见
  • 基本类型通常不需要使用
    MarshalAs
  • 使用
    IntPtr
    UIntPtr
    来输入指针类型。它们的大小在32位和64位平台之间有适当的差异

您的目标也是来自C的Windows吗?非常感谢。正是我想要的。
[StructLayout(LayoutKind.Sequential), Serializable]
public struct AESContext
{
    /// int nr; 
    public int nr;

    /// unsigned long *rk;
    public UIntPtr rk;

    // unsigned long buf[68];
    [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 68)]
    public uint[] buf;
}