将数组从 C# 编组到 C++返回:PInvokeStackImbalance

发布于 2024-09-08 11:05:11 字数 1450 浏览 6 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独孤求败 2024-09-15 11:05:11

您需要使用 DllImport 的 CallingConvention 属性。这里需要 Cdecl,因为您没有将 C 函数声明为 __stdcall。您不需要 [MarshalAs],您使用的值已经是默认值。因此:

    [DllImport("libQuadProg.dll", CallingConvention = CallingConvention.Cdecl)]
    private static extern double solveQP(
        int n, int mE, int mI,
        double[] p_G,
        // etc...
    }

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:

    [DllImport("libQuadProg.dll", CallingConvention = CallingConvention.Cdecl)]
    private static extern double solveQP(
        int n, int mE, int mI,
        double[] p_G,
        // etc...
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文