1

Possible Duplicate:
Marshaling c structures in c#

Actual C structures:

typedef struct procedure
{
  char code[8];
}procedure;

typedef struct p45_cldxed24
{
  procedure p45_cldxed[8];
}p45_cldxed24;

What would be the equivalent C# structure for p45_cldxed24? Specifically, how would you marshal the array?

check this out once

Is this correct?

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
struct p45_cldxed24
{
    [MarshalAsAttribute(UnmanagedType.LPArray, ArraySubType=UnmanagedType.Struct,SizeConst = 8,SizeParamIndex=0)]
    procedure[] p45_cldxed;
}
Community
  • 1
  • 1
Raghuveer
  • 2,630
  • 3
  • 29
  • 59

1 Answers1

0

I believe the code would be as follows:

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
struct p45_cldxed24
{
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
    procedure[] p45_cldxed;
}

The key here is that MarshalAs attribute.

Zenexer
  • 18,788
  • 9
  • 71
  • 77
  • Is this correct? - [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] struct p45_cldxed24 { [MarshalAsAttribute(UnmanagedType.LPArray, ArraySubType=UnmanagedType.Struct,SizeConst = 8,SizeParamIndex=0)] procedure[] p45_cldxed; } – Raghuveer Feb 08 '12 at 08:22
  • No, as @Zenexer said in his answer you want ByValArray - you can read about the different values here: http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.unmanagedtype.aspx. In particular "You can only use this UnmanagedType on an array that appear as fields in a structure" – jeffora Feb 08 '12 at 08:31