开发者

invoke C function with struct pointer as parameter

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!


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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜