My struct in C++ is the following
/* this structure contains the xfoil output parameters vs angle of attack */
typedef struct xfoil_outputdata_struct
{
double *pAlfa;
double *pCL;
double *pCM;
double *pCDi;
double *pCDo;
double *pCPmax;
long nEntries;
} XFOIL_OUTPUT_DATA;
/* Here are the function prototypes for XFoil */
__declspec(dllexport) XFOIL_OUTPUT_DATA *xfoilResults(); /* get output from xfoil */
I use XFoilResults to pull this structure back into C#
My DLL Imports statement is the following:
[DllImport("xfoilapi.dll")]
public static extern void xfoilResults();
Is this correct? I have no control over the C++ code. I just need to be able to pull the struct into C#. The C# struct I have so far is the following
[StructLayout(LayoutKind.Sequential)]
public struct xfoilResults
{
IntPtr pAlfa;
IntPtr pCL;
IntPtr pCM;
IntPtr pCDi;
IntPtr pCDo;
IntPtr pCPmax;
long nEntries;
}
How can I populate this C# structure with the data from the C++ code?
StructLayout must be on a class.
This should do the trick:
[DllImport("xfoilapi.dll")]
public static extern IntPtr GetXfoilResults();
[StructLayout(LayoutKind.Sequential)]
public class XfoilResults
{
IntPtr pAlfa;
IntPtr pCL;
IntPtr pCM;
IntPtr pCDi;
IntPtr pCDo;
IntPtr pCPmax;
int nEntries; // thanks to guys for reminding me long is 4 bytes
}
XfoilResults xf == new XfoilResults();
Marshal.PtrToStructure(GetXfoilResults(), xf);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With