嗨即时尝试从c#调用一些c + +代码。所以我跟着几个教程,amde我自己的dll,现在我从我的c#包装器调用它。事情是,我有一个函数接收一个指针,我似乎无法使它工作,视觉工作室只是显示我红色的错误,我不明白。有人能告诉我什么即时做错了吗? 我还有另外一个问题,如果C++中的函数调用其他函数,那么所有的数据都会保持不变吗?因为我传递的这个数组将在dll内部被操纵,之后,我将调用dll中的其他函数来获得结果 - 我担心数据在函数调用之间会丢失!c#和C++之间的指针 - p/invoke
谢谢!
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助理,我得到这个
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() ;
}
你可以尝试发布错误;) – cubuspl42 2013-03-23 20:15:52
sry,“不能接受地址,获取大小或声明指向受管理类型的指针[namespace.points []] – virgula24 2013-03-23 20:23:23
我认为这只是我的错,因为我不知道那么多关于指针,但我认为我做正确 – virgula24 2013-03-23 20:23:49