std vector of structs to System array & vice versa

1) given an unmanaged struct:

struct NativeStruct
{
  int i;
  char s[10];
}

and a managed struct:

[StructLayout(LayoutKind:Sequential)]
public value struct ManagedStruct
{
  int i;
  [MarshalAs(UnmanagedType::ByValTStr), SizeConst=10] String^ s;
  ...
}

How to convert from

array<ManagedStrcut>^ arr 

to

std::vector<NativeStrcut> vec;

and vice versa: vector to system array?

2) Same question when Native struct contains these two additional fields:

short *spElements;
int sp_length; // number of elements

and managed struct:

IntPtr ipElements;
int sp_length; // number of elements


Source: .net

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.