Unity3D中C#调用C++动态库总结
来源:互联网 发布:大智慧怎么导出数据 编辑:程序博客网 时间:2024/05/21 19:35
动态库是用C++和OpenCV写的。
我们可以这样给它分类,
1.普通传值,如下面代码中MotionDetect的第4个参数;
2.传引用,MotionDetect的第3个参数,nNum传进动态库后赋值再传回来;
3.引用传一个结构体,MotionDetect的第2个参数,这里其实是传一个结构体的数组,具体像加ref的传参还真不会;
4.传一个有分配内存的变量,需要用到GCHandle,因为C#是可以自动回收内存的,而GCHandle在这里的作用就是把它的内存空间Pin住,传递给C++动态库后再手动回收资源。
using UnityEngine;using System.Collections;using System.Runtime.InteropServices;public class D : MonoBehaviour {struct Color_32{public byte r;public byte g;public byte b;public byte a;}Color_32[] imageDataResult;GCHandle pixelsHandle;[DllImport("MotionDetectionDll")]private static extern bool MotionDetect( System.IntPtr colors, Color_32[] imageDataResult, ref int nNum, int nChannels );void Start() {imageDataResult = new Color_32[128*128];}void Update () {int nNum = 0;pixelsHandle = GCHandle.Alloc(pixels, GCHandleType.Pinned);bool wfDf = MotionDetect( pixelsHandle.AddrOfPinnedObject(), imageDataResult, ref nNum, 4 );pixelsHandle.Free();}}
C++中是这么写的
//头文件中多加个结构体定义#include "stdafx.h"struct Color_32{byte r;byte g;byte b;byte a;};extern "C" _declspec(dllexport) bool MotionDetect ( char* imageData, Color_32 imageDataResult[], int *nNum, int nChannels );// CPP文件中extern "C" _declspec(dllexport) bool MotionDetect ( char* imageData, Color_32 imageDataResult[], int *nNum, int nChannels ){IplImage* imgSrc = cvCreateImageHeader(cvSize(width, height), IPL_DEPTH_8U, 4);if(!imgSrc){return false;}cvSetData( imgSrc, imageData, imgSrc->widthStep );......for ( int i=0; i< ......; i++ ){imageDataResult[i].r = ......;imageDataResult[i].g = ......;imageDataResult[i].b = ......;imageDataResult[i].a = ......;}......*nNum = 5;nChannels = 4;}
- Unity3D中C#调用C++动态库总结
- 在C#中调用C语言标准动态库方法
- C#调用C/C++动态链接库
- 【c#】C#调用matlab动态链接库
- C# 中静态调用C++dll 和C# 中动态调用C++dll
- Unity3D中C#调用iOS的静态库(*.a)
- Unity3D中C#调用iOS的静态库(*.a)
- Unity3D中C#调用iOS的静态库(*.a)
- C#动态调用C++DLL
- 在C#中调用C语言写的动态链接库 DLL
- C#中调用C++ 动态链接库 dll 的方…
- C#中调用C++ 动态链接库 dll 的方…
- C程序中调用C++动态库
- HPUX环境,在WebLogic服务器中使用java调用C动态连接库异常问题总结
- C#调用C++DLL总结
- C# 中动态调用C++动态链接
- C# 中动态调用C++动态链接
- 创建和使用动态链接库 (C++)C#调用
- sql语句操作表字段(二)
- HDU3389(阶梯博弈)
- 学习openCV中tutorial_code的资料
- SQL Server 数据库查询区分大小写
- MFC获得当前应用程序目录的GetCurrentDirectory()和GetModuleFileName()函数
- Unity3D中C#调用C++动态库总结
- Python 学习入门(15)—— Tornado
- 分享一段ios数据库代码。包括创建、升级、增删查改。
- ASCILL对照表
- 使用NSKeyedUnarchiver解压数据如何防止crash
- 如何高效地阅读技术类书籍与博客
- hdu_3910 Liang Guo Sha(概率论)
- Monodevelop打开项目后引用提示“Assembly not available for Mono……” 解决办法
- Keil C51对C语言的关键词扩展之四: bit