将数组从 C# 编组到 C++返回:PInvokeStackImbalance
我有一个 C++ 函数,我想从 C# 访问它。问题是我不断收到 PInvokeStackImbalance 异常,但我不知道为什么。当检查异常被关闭时,一切都运行良好并且符合预期。
我的 C++ 函数的签名是:
extern "C" double solveQP(
int32_t n, int32_t mE, int32_t mI,
double *p_G, double *p_g0,
double *p_CE, double *p_ce0,
double *p_CI, double *p_ci0,
double *p_x)
我用来访问它的方法是:
[DllImport("libQuadProg.dll")]
[return: MarshalAs(UnmanagedType.R8)]
private static extern double solveQP(
[In, MarshalAs(UnmanagedType.I4)] int n,
[In, MarshalAs(UnmanagedType.I4)] int mE,
[In, MarshalAs(UnmanagedType.I4)] int mI,
[In, MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.R8)] double[] p_G,
[In, MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.R8)] double[] p_g0,
[In, MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.R8)] double[] p_CE,
[In, MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.R8)] double[] p_ce0,
[In, MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.R8)] double[] p_CI,
[In, MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.R8)] double[] p_ci0,
[In, MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.R8)] double[] p_x);
我也尝试过仅使用 UnmanagementType.LPArray 选项,但什么也不做。我认为有一个有关 PInvoke 的细节我不明白,如果有人指出,我将不胜感激。
I have a C++ function that I'd like to access from C#. The problem is I keep getting PInvokeStackImbalance exceptions and I don't know why. Everything runs fine and as expected when checking for that exception is turned off.
The signature of my C++ function is:
extern "C" double solveQP(
int32_t n, int32_t mE, int32_t mI,
double *p_G, double *p_g0,
double *p_CE, double *p_ce0,
double *p_CI, double *p_ci0,
double *p_x)
and what I've been using to access it is:
[DllImport("libQuadProg.dll")]
[return: MarshalAs(UnmanagedType.R8)]
private static extern double solveQP(
[In, MarshalAs(UnmanagedType.I4)] int n,
[In, MarshalAs(UnmanagedType.I4)] int mE,
[In, MarshalAs(UnmanagedType.I4)] int mI,
[In, MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.R8)] double[] p_G,
[In, MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.R8)] double[] p_g0,
[In, MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.R8)] double[] p_CE,
[In, MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.R8)] double[] p_ce0,
[In, MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.R8)] double[] p_CI,
[In, MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.R8)] double[] p_ci0,
[In, MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.R8)] double[] p_x);
I've also tried it with just the UnmanagedType.LPArray option and nothing at all. I figure there is one detail about PInvoke that I just do not get and I'd appreciate it if someone pointed it out.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要使用 DllImport 的 CallingConvention 属性。这里需要 Cdecl,因为您没有将 C 函数声明为 __stdcall。您不需要 [MarshalAs],您使用的值已经是默认值。因此:
You need to use the DllImport's CallingConvention property. Cdecl is required here since you didn't declare the C function as __stdcall. You don't need [MarshalAs], the values you use are already the default. Thus: