以结构体指针作为参数调用 C 函数
完全嗨 我尝试从非托管 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的 C 项目使用哪种调用约定?如果是 cdecl (默认 IIRC),则需要在 DllImport 属性:
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: