我在 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 导入语句如下:
[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);
关于c# - 我正在尝试将结构从 C 编码到 C#,但不确定从哪里开始,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4464532/