这有点困难,因为我甚至不知道我的C是否实现得很好.
我想从Unity3D调用一个函数,我传递一些参数,例如float * points和我的C写入该指针.
好吧,从C部分开始,我写道:
main_function.cpp这是我在C中的主要功能,例如,它可以完美地用作二进制文件.
#include "main_function.h"
extern "C" void getPoints (uchar *data, float *points, int rows, int cols, const char *trackername) {
//load detector model
face_tracker tracker = load_ft(trackername);
//create tracker parameters
face_tracker_params p; p.robust = false;
p.ssize.resize(3);
p.ssize[0] = Size(21,21);
p.ssize[1] = Size(11,11);
p.ssize[2] = Size(5,5);
Mat im = Mat(Size(cols, rows), CV_8U, data);
if(tracker.track(im,p))
tracker.draw(im, points);
}
main_function.h
#include "opencv_hotshots/ft/ft.hpp"
#include "opencv2/highgui/highgui.hpp"
#include
extern "C" {
void getPoints (uchar *data, float *points, int rows, int cols, const char *trackername);
}
然后我想将我的C代码包装到C#中,所以我做了:
AEDwrapper.h
// AEDwrapper.h
#pragma once
#ifdef TESTFUNCDLL_EXPORT
#define TESTFUNCDLL_API __declspec(dllexport)
#else
#define TESTFUNCDLL_API __declspec(dllimport)
#endif
using namespace System;
#include "stdafx.h"
#include "C:\Users\Rafael\Desktop\AEDlibrary\main_function.h"
extern "C" {
namespace AEDwrapper {
TESTFUNCDLL_API public ref class AED
{
void getPointsAED (uchar *data, float *points, int rows, int cols, const char *trackername);
};
}
}
和AEDwrapper.cpp
// Archivo DLL principal.
#include "stdafx.h"
#include
#include
#include
#include
#include "AEDwrapper.h"
using namespace std::chrono;
extern "C" {
void AEDwrapper::AED::getPointsAED (uchar *data, float *points, int rows, int cols, const char *trackername){
getPoints(data, points, rows, cols, trackername);
}
}
我构建它,它变成了AEDwrapper.dll.
现在在Unity3D中,我在C#部分(脚本)中尝试:
[DllImport ("/Assets/plugins/AEDwrapper.dll")]
// or [DllImport ("AEDwrapper")]
private static extern unsafe void getPointsAED (byte* data, float* points, int rows, int cols, sbyte* trackername);
//DECLARA METODO DEL DLL
DllNotFoundException: /Assets/plugins/AEDwrapper.dll
我知道在我的代码的早期步骤中我可能是错的,但你曾经在那之前做过吗???我有点迷茫.
先感谢您.
拉斐尔.
编辑:
我尝试过一个简单的函数(例如:int gimmetwo();在标题中:
int gimmetwo() {
return 2;
}
结果是一样的:(
编辑!!!! 7月11日
在看到像@ k-gkinis和@ nikola-dimitroff这样的答案后,我明白我必须为不同的环境编译不同的库.
在这种情况下,我为Mac OS x创建了一个Plugin.bundle:
Plugin.pch:
extern "C" {
string getmytext();
}
Plugin.cpp
#include "Plugin.pch"
string getmytext() {
return "weowoewoeowoew";
}
我构建它并将其导入Unity3D:
[DllImport ("AED")]
private static extern string getmytext ();
static void obtenerPuntos()
{
Debug.Log (getmytext());
}
但我仍然得到同样的错误!!
我猜这个插件在正确的位置
怎么了?
作者在尝试从Unity3D调用C++编写的函数时遇到困难,函数需要处理图像数据并返回结果。已经尝试了使用DllImport进行调用,但遇到了DllNotFoundException。尝试了不同平台的库编译,包括为MacOSx创建的Plugin.bundle,但问题依然存在。作者寻求帮助以解决库加载和跨平台调用的问题。
926

被折叠的 条评论
为什么被折叠?



