I'm trying to marshal a struct that contains a float-Array from a C++ DLL to C#.
I created the C++ DLL from the following code:
//MarshalTest.h
namespace mTest{
typedef struct {
float data[3];
int otherStuff;
} dataStruct;
extern "C" __declspec(dllexport) dataStruct getData();
}
//MarshalTest.cpp
#include "MarshallTest.h"
using namespace std;
namespace mTest{
dataStruct getData(){
dataStruct d = {{ 16, 2, 77 }, 5};
return d;
}
}
I use the following code to make the getData-Function available in C#:
public unsafe struct dataStruct{
public fixed byte data[3];
public int otherStuff;
public unsafe float[] Data{
get{
fixed (byte* ptr = data){
IntPtr ptr2 = (IntPtr)ptr;
float[] array = new float[3];
Marshal.Copy(ptr2, array, 0, 3);
return array;
}
}
set{
fixed (byte* ptr = data){
//not needed
}
}
}
}
[DllImport("MarshallTest", CallingConvention = CallingConvention.Cdecl)]
private static extern dataStruct getData ();
When printing data[] in C# I get the following output: 1.175494E-38 1.610935E-32 8.255635E-20
What am I doing wrong?