2
[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;
}

This is C# struct I have so far. Comment above each field is type in C. I would love if someone could verify.

1
  • Are you targeting Windows from C as well? Commented Feb 18, 2012 at 22:11

1 Answer 1

6

It sounds like you're trying to get the C# struct for the C struct defined in the member contents. If so then I believe you want the following

[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;
}

Basic changes

  • Don't specify SizeConst in StructLayout unless you are trying to create a struct whose size is different (typically) bigger than it's contents. It's not very common to do this
  • MarshalAs isn't usually needed for primitive types
  • Use IntPtr or UIntPtr to PInvoke pointer types. They vary properly in size between 32 and 64 bit platforms
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.