0

こんにちは私はc#からいくつかのc++コードを呼び出そうとしています。だから私はいくつかのチュートリアルに従い、自分のdllを修正し、今ではc#ラッパーから呼び出しています。問題は、ポインターを受け取る関数があり、それを機能させることができないようです。VisualStudioは、よく理解していない赤いエラーを表示するだけです。誰かが私に何が間違っているのか教えてもらえますか?そして私は別の質問があります、c ++の関数が他の関数を呼び出す場合、すべてのデータはそのまま残りますか?私が渡すこの配列はdll内で操作され、その後、結果を取得するためにdllから他の関数​​を呼び出すので、関数呼び出しの間にデータが失われるのではないかと心配しています。

ありがとう!

dll .h #include

   #include <stdio.h> 

   #include <stdlib.h> 

   #include <math.h> 

   #include <string> 

   #include <vector> 

   #include <fstream> 

   #include <sstream> 

   #include <cstring> 

   #include <fstream>

   #include <iomanip> 

   #include <cstdlib> 

   #include <string> 

   #include <cstring> 

   #include "opencv\ml.h"

   struct points{
    double x;
    double y;
    double z;
   };

#define db at<double>


   extern "C" __declspec(dllexport) points * mapear_kinect_porto(points pontos[]);


   CvERTrees *  Rtree ;


   extern "C" __declspec(dllexport)     void calibrate_to_file(points pontos[]);

   extern "C" __declspec(dllexport)     int calibration_neutral();

   extern "C" __declspec(dllexport)       int EmotionsRecognition();

c#ラッパー

             [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct points
{

    /// double
    public double x;

    /// double
    public double y;

    /// double
    public double z;
}


class CPlusPlusWrapper
{

/// Return Type: void
    ///pontos: points*
    [System.Runtime.InteropServices.DllImportAttribute("DLLTUT.dll", EntryPoint =  "calibrate_to_file")]
    public static extern void calibrate_to_file(ref points pontos);
    public unsafe void calibrate_file()
    {

        points[] shit = new points[8];
        points*[] pBLA; //error here!!!!
        pBLA = &shit;
   //            calibrate_to_file(pbla);
    }

}

ところで、私はp / invoke Assistantを使用しましたが、これを取得しました

public partial class NativeConstants {

/// db -> at<double>
/// Error generating expression: Expression is not parsable.  Treating value as a raw string
public const string db = "at<double>";
}

[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct points {

/// double
public double x;

/// double
public double y;

/// double
public double z;
}

public partial class NativeMethods {

/// Return Type: points*
///pontos: points*
[System.Runtime.InteropServices.DllImportAttribute("<Unknown>", EntryPoint="mapear_kinect_porto")]
public static extern  System.IntPtr mapear_kinect_porto(ref points pontos) ;


/// Return Type: void
///pontos: points*
[System.Runtime.InteropServices.DllImportAttribute("<Unknown>", EntryPoint="calibrate_to_file")]
public static extern  void calibrate_to_file(ref points pontos) ;


/// Return Type: int
[System.Runtime.InteropServices.DllImportAttribute("<Unknown>", EntryPoint="calibration_neutral")]
public static extern  int calibration_neutral() ;


/// Return Type: int
[System.Runtime.InteropServices.DllImportAttribute("<Unknown>", EntryPoint="EmotionsRecognition")]
public static extern  int EmotionsRecognition() ;

}
4

1 に答える 1

2

。という名前の関数を呼び出そうとしているようcalibrate_to_fileです。の配列を受け取りpointsます。そのためのp/invokeは次のとおりです。

[DllImportAttribute("DLLTUT.dll", CallingConvention=CallingConvention.Cdecl)]
public static extern void calibrate_to_file(points[] pontos);

安全でないコードは絶対に必要ありません。呼び出し元のコードで型の配列を作成してpoints[]から、を呼び出しますcalibrate_to_file

呼び出し規約が一致していることを確認する必要があります。C ++コードで呼び出し規約を指定しなかったので、デフォルトのcdeclが使用されていると思います。

構造体は単純に次のように宣言できます

[StructLayout(LayoutKind.Sequential)]
public struct points
{
    public double x;
    public double y;
    public double z;
}

mapear_kinect_porto関数の戻り値としてポインターを返すため、関数はよりトリッキーになります。関数の戻り値の代わりにパラメーターを使用して、その情報を返す方が簡単です。

あなたへの私の最善のアドバイスは、この問題を細かく分割することです。最も単純な関数を機能させます。次に、次の機能に進みます。等々。問題全体を一度に解決しようとしないでください。そうすると、必然的に失敗したときにエラーを探す場所がわかりません。

于 2013-03-23T20:25:19.543 に答える