C#和C ++之间的指针-p/invoke [英] pointers between c# and c++ - p/invoke

查看:107
本文介绍了C#和C ++之间的指针-p/invoke的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我想从c#调用一些c ++代码.所以我遵循了一些教程,修改了我自己的dll,现在我从C#包装器中调用它.问题是,我有一个接收指针的函数,但似乎无法使其正常工作,Visual Studio只是向我显示了一些我不太了解的红色错误.有人可以告诉我我做错了什么吗? 我还有一个问题,如果c ++中的函数调用其他函数,则所有数据将保持不变,对吗?因为我传递的数组将在dll内部进行操作,然后,我将从dll调用其他函数以获取结果-我担心函数调用之间的数据会丢失!

hi im trying to call some c++ code from c#. So i followed several tutorials, amde my own dll, and now i call it from my c# wrapper. the thing is, i have a function that receives a pointer and i can't seem to make it work, visual studio just shows me red errors that i don't understand very well. can someone tell me what im doing wrong? and i have another question, if the functions in c++ call other functions, all the data will remain intact right? because this array that i pass will be manipulated inside the dll and, afterwards, im gonna call other function from the dll to get the results - my fear is that the data is lost between function calls!

谢谢!

dll .h #include

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助手,我得到了这个

btw i used p/invoke assistant and i got this

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

}

推荐答案

您似乎正在尝试调用名为calibrate_to_file的函数.接收到一个points数组.的p/调用是:

You appear to be trying to call the function named calibrate_to_file. That receives an array of points. The p/invoke for that is:

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

绝对不需要不安全的代码.只需在调用代码中创建类型为points[]的数组,然后调用calibrate_to_file.

There's absolutely no need for unsafe code. Just make an array of type points[] in your calling code and then call calibrate_to_file.

您需要确保调用约定匹配.由于您未在C ++代码中指定调用约定,因此我假定使用默认的cdecl.

You need to make sure the calling conventions match. Since you did not specify a calling convention in the C++ code I assume the default of cdecl is used.

该结构可以简单地声明为

The struct can be declared simply as

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

mapear_kinect_porto函数将变得更加棘手,因为您将返回一个指针作为函数返回值.使用参数而不是函数返回值来返回该信息会更容易.

The mapear_kinect_porto function is going to be more tricky because you are returning a pointer as the function return value. It would be easier for you to return that information using a parameter instead of the function return value.

对您来说,最好的建议是将这个问题分解为小块.获得最简单的功能.然后转到下一个功能.等等.不要试图一口气解决整个问题.然后,当错误不可避免地失败时,您将不知道在哪里寻找错误.

My best advice for you is to break this problem down into small pieces. Get the simplest function working. Then move on to the next function. And so on. Don't try to solve the entire problem in one go. Then you'll not know where to look for the error when it inevitably fails.

这篇关于C#和C ++之间的指针-p/invoke的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆