// MarshalArrayOfStructures.cpp : main project file.
#include "stdafx.h"
#include "MyNativeStruct.h"
#include "MyManagedStruct.h"
#include <stdlib.h>
#include <string.h>
using namespace System;
using namespace System::Runtime::InteropServices;
struct MyNativeStruct
{
public:
int myInt;
double myDouble;
float myFloat;
};
[StructLayout(LayoutKind::Sequential)]
public value struct MyManagedStruct
{
public:
int myInt;
double myDouble;
float myFloat;
};
array<MyManagedStruct>^ marshalNativeArrayOfMyStruct(const MyNativeStruct* vector, int size)
{
array<MyManagedStruct>^ result = gcnew array<MyManagedStruct>(static_cast<int>(size));
if (size) {
pin_ptr<MyManagedStruct> dest = &result[0];
memcpy(dest, &vector[0], size * sizeof(MyNativeStruct));
}
return result;
}
int main(array<System::String ^> ^args)
{
MyNativeStruct myNativeStructArray[5] =
{
{ 100, 34.5, 56.4f }, { 200, 66.7, 78.2f },
{ 300, 88.9, 8.566f }, { 400, 7438.3, 937589.487f },
{ 500, 0.64567, 84677.4887f }
};
array<MyManagedStruct>^ myManagedStructArray =
gcnew array<MyManagedStruct>(_countof(myNativeStructArray));
myManagedStructArray = marshalNativeArrayOfMyStruct(myNativeStructArray,
_countof(myNativeStructArray));
Console::WriteLine(L"Hello World");
return 0;
}
MarshalArrayOfStructures.zip