Marshall management of the structure within the structure

I have the following code in C ++ that I need to have 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 that 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; } 
+4
source share
3 answers

Have you tried this?

  [StructLayoutAttribute (LayoutKind.Sequential)] public struct Outer { public double data3; [MarshalAsAttribute (UnmanagedType.SafeArray, SafeArrayUserDefinedSubType=typeof(Inner))] public Inner [] innerData; } 
+1
source

It appears that the attribute declaration is incorrect, as it refuses to compile ...

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

Hope this helps, Regards, Tom.

0
source

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

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

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

0
source

Source: https://habr.com/ru/post/1300822/


All Articles