I have a dll containing a function that returns a structure with a byte, and an array of 7 doubles:
public struct myStruct
{
public byte v1;
public Blob v2;
}
the dll function:
[DllImport("myDLL", EntryPoint = "?myDLLFuntion@@YA?AUmyStruct@@XZ", ExactSpelling = true)]
public static extern MyStruct myDLLFunction();
this is my blob:
[StructLayout(LayoutKind.Sequential, Size = ((sizeof(double))*7))]
public struct Blob
{
// Intentionally left empty. It's just a blob
}
I created a function to get the array:
public double[] GetArray(Blob NameBlob)
{
IntPtr dPtr = IntPtr.Zero;
try
{
dPtr = Marshal.AllocHGlobal(((sizeof(double))*7));
Marshal.StructureToPtr(NameBlob, dPtr, false);
double[] r = new double[((sizeof(double))*7)];
Marshal.Copy(dPtr, r, 0, ((sizeof(double))*7));
return r;
}
finally
{
if (dPtr != IntPtr.Zero)
{
Marshal.FreeHGlobal(dPtr);
}
}
}
I called it like this:
myStruct s1=myDLLFunction();
GetArray(s1.v2)[0]; // print it
I get the wrong value, please if someone can help me that would be great, I’m stating the use of Marshal… I think there is where I’m doing it wrong.
As Hans Passant pointed out,
StructureToPtr()doesn’t do anything when the struct doesn’t have any fields. I think we was talking about this: