从 C++/CLI 调用 c# class
Call c# class from C++/CLI
我想用 c++/cli 导出我的 c# class:
这是我的头文件 (.h) 和我的 .cpp
using namespace System;
using namespace Microsoft::Kinect;
using namespace std;
using namespace System::Runtime::InteropServices;
class clrPrivate;
class __declspec(dllexport) clr
{
private: clrPrivate* _private;
public: clr();
public: ~clr();
public: String^ calculate(Double* Robot_Points_Values[], CameraSpacePoint* human_point_cloud, unsigned char* bodyindexdata[]);
};
和我的 .cpp 文件
// This is the main DLL file.
#using "FULLPATH\MYDLL.dll"
#include <msclr\auto_gcroot.h>
#include "ClassLibrary4.h"
#include <array>
//#include "stdafx.h"
using namespace Distance_Test;
using namespace std;
using namespace System::Runtime::InteropServices;
using namespace Microsoft::Kinect;
using namespace System;
class clrPrivate
{
public: msclr::auto_gcroot<MINIMUM_DISTANCE^> out;
};
clrPrivate* _private;
clr::clr()
{
_private = new clrPrivate();
_private->out = gcnew MINIMUM_DISTANCE;
};
String^ clr::calculate(Double* Robot_Points_Values[], CameraSpacePoint* human_point_cloud, unsigned char* bodyindexdata[])
{
String^ output;
return output = _private->out->calculate(*Robot_Points_Values, *human_point_cloud, **bodyindexdata);
//return output = _private->out->value_return;
};
clr::~clr()
{
delete _private;
};
当我 运行 调试模式时,它显示 2 个错误:
1) 错误 C3395“clr::calculate”:__declspec(dllexport) 无法应用于具有 __clrcall 调用约定的函数
2) '.value_return' 左边的错误 C2228 必须有 class/struct/union
我现在正在尝试 2 天来找到解决方案,但我 stuck.If 有人可以帮助我,我将不胜感激。
对于托管 classes,您不需要 __declspec(dllexport),因为它们会自动导出,除非您声明它们 private/internal。只需像这样更改 class 签名:
public ref class clr
此外,如果您想在 C# 中使用它,您应该在计算方法的签名中使用托管数组:
String^ calculate(array<Double*>^ Robot_Points_Values, CameraSpacePoint* human_point_cloud, array<unsigned char*>^ bodyindexdata);
我想用 c++/cli 导出我的 c# class:
这是我的头文件 (.h) 和我的 .cpp
using namespace System;
using namespace Microsoft::Kinect;
using namespace std;
using namespace System::Runtime::InteropServices;
class clrPrivate;
class __declspec(dllexport) clr
{
private: clrPrivate* _private;
public: clr();
public: ~clr();
public: String^ calculate(Double* Robot_Points_Values[], CameraSpacePoint* human_point_cloud, unsigned char* bodyindexdata[]);
};
和我的 .cpp 文件
// This is the main DLL file.
#using "FULLPATH\MYDLL.dll"
#include <msclr\auto_gcroot.h>
#include "ClassLibrary4.h"
#include <array>
//#include "stdafx.h"
using namespace Distance_Test;
using namespace std;
using namespace System::Runtime::InteropServices;
using namespace Microsoft::Kinect;
using namespace System;
class clrPrivate
{
public: msclr::auto_gcroot<MINIMUM_DISTANCE^> out;
};
clrPrivate* _private;
clr::clr()
{
_private = new clrPrivate();
_private->out = gcnew MINIMUM_DISTANCE;
};
String^ clr::calculate(Double* Robot_Points_Values[], CameraSpacePoint* human_point_cloud, unsigned char* bodyindexdata[])
{
String^ output;
return output = _private->out->calculate(*Robot_Points_Values, *human_point_cloud, **bodyindexdata);
//return output = _private->out->value_return;
};
clr::~clr()
{
delete _private;
};
当我 运行 调试模式时,它显示 2 个错误:
1) 错误 C3395“clr::calculate”:__declspec(dllexport) 无法应用于具有 __clrcall 调用约定的函数
2) '.value_return' 左边的错误 C2228 必须有 class/struct/union
我现在正在尝试 2 天来找到解决方案,但我 stuck.If 有人可以帮助我,我将不胜感激。
对于托管 classes,您不需要 __declspec(dllexport),因为它们会自动导出,除非您声明它们 private/internal。只需像这样更改 class 签名:
public ref class clr
此外,如果您想在 C# 中使用它,您应该在计算方法的签名中使用托管数组:
String^ calculate(array<Double*>^ Robot_Points_Values, CameraSpacePoint* human_point_cloud, array<unsigned char*>^ bodyindexdata);