以结构体指针作为参数调用 C 函数

发布于 2024-10-03 03:50:26 字数 1401 浏览 3 评论 0原文

完全嗨 我尝试从非托管 c-dll 取回数据。 c 函数需要一个指向结构的指针,用一些值初始化该结构并完成。错误可能出现在任何地方,甚至在 c dll 声明中。 (我第一次这样做)

这里是 c 代码 h-file:

#ifndef MYFUNCS_H
#define MYFUNCS_H

__declspec(dllexport) typedef struct t_Point{
 int x;
 int y;
} Point;

__declspec(dllexport) Point myFuncs();
__declspec(dllexport) int getPoint(Point* point);
#endif

c-file:

#include "stdafx.h"
#include "OpenCVTest.h"

int getPoint(Point* point){
 point->x = 4;
 point->y = 2;
 return 0;
}

c# 中的包装器:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace CSharp_mit_OpenCV
{
    [StructLayout(LayoutKind.Sequential)]
    public struct Point
    {
        public int x;
        public int y;
    };

    class Wrapper
    {
        [DllImport("OpenCV Test.dll", CharSet= CharSet.Auto)]
        public static extern int getPoint(ref Point point);

    }
}

以及使用该包装器的 c# 函数:

Point p = new Point();
            Wrapper.getPoint(ref p);
            textBox1.Text = p.x.ToString();
            textBox2.Text = p.y.ToString();

使用此代码,我收到以下运行时错误:

“调用PInvoke 函数“CSharp mit OpenCV!CSharp_mit_OpenCV.Wrapper::getPoint”使堆栈不平衡 这可能是因为托管 PInvoke 签名与非托管目标签名不匹配 检查 PInvoke 签名的调用约定和参数是否与目标非托管匹配。签名。”

这是怎么回事?请帮忙! 谢谢大家!

Hi at all
I try to get data back from my unmanaged c-dll. The c function expects a pointer to a struct, initialize the struct with some value and finish. The mistake could be anywhere, even in the c dll declaring. (I'm doing this the first time)

Here the c code h-file:

#ifndef MYFUNCS_H
#define MYFUNCS_H

__declspec(dllexport) typedef struct t_Point{
 int x;
 int y;
} Point;

__declspec(dllexport) Point myFuncs();
__declspec(dllexport) int getPoint(Point* point);
#endif

c-file:

#include "stdafx.h"
#include "OpenCVTest.h"

int getPoint(Point* point){
 point->x = 4;
 point->y = 2;
 return 0;
}

The wrapper in c#:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace CSharp_mit_OpenCV
{
    [StructLayout(LayoutKind.Sequential)]
    public struct Point
    {
        public int x;
        public int y;
    };

    class Wrapper
    {
        [DllImport("OpenCV Test.dll", CharSet= CharSet.Auto)]
        public static extern int getPoint(ref Point point);

    }
}

And the c# function who uses that wrapper:

Point p = new Point();
            Wrapper.getPoint(ref p);
            textBox1.Text = p.x.ToString();
            textBox2.Text = p.y.ToString();

With this code I get the following runtime error:

"A call to PInvoke function 'CSharp mit OpenCV!CSharp_mit_OpenCV.Wrapper::getPoint' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature."

What's wrong here? Please help!
Thank you all!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

相对绾红妆 2024-10-10 03:50:26

您的 C 项目使用哪种调用约定?如果是 cdecl (默认 IIRC),则需要在 DllImport 属性:

[DllImport("OpenCV Test.dll", CharSet = CharSet.Auto,
    CallingConvention = CallingConvention.Cdecl)]
public static extern int getPoint(ref Point point);

Which calling convention is used by your C project? If it's cdecl (the default IIRC), you need to explicitly specify it in your DllImport attribute:

[DllImport("OpenCV Test.dll", CharSet = CharSet.Auto,
    CallingConvention = CallingConvention.Cdecl)]
public static extern int getPoint(ref Point point);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文