4

I have the following code in C++ which I need to be able to call from C#:

struct Inner
{
 double data1;
 double data2;
};

struct Outer
{
 double data3;
 SAFEARRAY innerData;
};

int WINAPI ProcessData (Outer& outer )
{
  ...
}

I tried the following but it did not work What am I doing wrong?

[StructLayoutAttribute(LayoutKind.Sequential)]
public struct Inner 
{
 public double data1;
 public double data2;
}

[StructLayoutAttribute(LayoutKind.Sequential)]
public struct Outer 
{
 public double data3;
[MarshalAsAttribute(UnmanagedType.Safearray,ArraySubType = UnmanagedType.Struct)]
 public Inner[] innerData;
}
Kristian Glass
  • 37,325
  • 7
  • 45
  • 73
SparcU
  • 742
  • 1
  • 7
  • 27

2 Answers2

1

Did you try this?

  [StructLayoutAttribute (LayoutKind.Sequential)]
  public struct Outer
  {
     public double data3;
     [MarshalAsAttribute (UnmanagedType.SafeArray, SafeArrayUserDefinedSubType=typeof(Inner))]
     public Inner [] innerData;
  }
John Knoeller
  • 33,512
  • 4
  • 61
  • 92
0

It looks as if the attribute declaration is not correct as its refusing to compile...

[StructLayoutAttribute(LayoutKind.Sequential)]
        public struct Outer
        {
            public double data3;
            [MarshalAsAttribute(UnmanagedType.SafeArray, SafeArraySubType=VarEnum.VT_SAFEARRAY)]
            public Inner[] innerData;
        }

Hope this helps, Best regards, Tom.

t0mm13b
  • 34,087
  • 8
  • 78
  • 110