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

C# 以结构指针作为参数调用C函数,c#,reference,call,invoke,C#,Reference,Call,Invoke,你好 我试图从非托管c-dll中获取数据。c函数需要一个指向结构的指针,用一些值初始化结构并完成。错误可能在任何地方,甚至在c dll中。(我第一次这么做) 这里是c代码h文件: #ifndef MYFUNCS_H #define MYFUNCS_H __declspec(dllexport) typedef struct t_Point{ int x; int y; } Point; __declspec(dllexport) Point myFuncs(); __declspec(d

你好 我试图从非托管c-dll中获取数据。c函数需要一个指向结构的指针,用一些值初始化结构并完成。错误可能在任何地方,甚至在c dll中。(我第一次这么做)

这里是c代码h文件:

#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文件:

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

int getPoint(Point* point){
 point->x = 4;
 point->y = 2;
 return 0;
}
c#中的包装器:

以及使用该包装器的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签名的调用约定和参数是否与目标非托管签名匹配。”

这里怎么了?请帮忙! 谢谢大家!

您的C项目使用哪个?如果是(默认IIRC),则需要在属性中显式指定:

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