我在C中的结构如下
/* 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 */
我使用XFoilResults将这个结构拉回到C#
我的DLL Imports语句如下:
[DllImport("xfoilapi.dll")]
public static extern void xfoilResults();
这个对吗?我无法控制C代码.我只需要能够将结构拖入C#中即可.到目前为止,我拥有的C#结构如下
[StructLayout(LayoutKind.Sequential)]
public struct xfoilResults
{
IntPtr pAlfa;
IntPtr pCL;
IntPtr pCM;
IntPtr pCDi;
IntPtr pCDo;
IntPtr pCPmax;
long nEntries;
}
如何用C代码中的数据填充此C#结构?
解决方法:
StructLayout必须在类上.
这应该可以解决问题:
[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);