views:

357

answers:

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;
}
A: 

Did you try this?

  [StructLayoutAttribute (LayoutKind.Sequential)]
  public struct Outer
  {
     public double data3;
     [MarshalAsAttribute (UnmanagedType.SafeArray, SafeArrayUserDefinedSubType=typeof(Inner))]
     public Inner [] innerData;
  }
John Knoeller
A: 

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.

tommieb75
A: 

Tried both and it did not work.

SparcU
what is the VT_xxx type of your SAFEARRAY on the C++ side?
John Knoeller
A: 

When I try this: [MarshalAsAttribute(UnmanagedType.SafeArray, SafeArrayUserDefinedSubType = typeof(Inner))]

I get: An unhandled exception of type 'System.ArgumentException' occurred in Driver.exe

Additional information: The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))

SparcU