2010-11-19 6 views
1

こんにちはすべて 私はアンマネージc-dllからデータを取得しようとしています。 c関数はstructへのポインタを期待し、structをある値で初期化して終了します。間違いは、たとえ宣言しているc dllであっても、どこでもかまいません。パラメータとして構造体ポインタを使用してC関数を呼び出す

#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#で:

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コードH-ファイルを

を(私が初めてこれをやっています)

そしてそのラッパーを使用するC#関数:

このコードでは

は、私は、次のランタイムエラーを取得:

「のPInvoke関数の呼び出し '!CSHARP MIT OpenCVのCSharp_mit_OpenCV.Wrapper :: getPointは' アンバランスなスタックを持っています。これは、管理対象のPInvokeシグネチャがアンマネージ対象シグネチャと一致しないためです。それはcdeclをなら呼び出し規約とのPInvokeシグネチャのパラメータがターゲット管理対象外の署名と一致していることを確認してください。」

をここで間違っているのですか?助けてください! は?あなたのCのプロジェクトで使用されているcalling convention

答えて

0

!あなたのすべてに感謝します(デフォルトのIIRC)では、DllImport属性で明示的に指定する必要があります。

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