Unity3d调用C++ dll中的函数

一、生成dll

1.新建dll工程

  1. 不用管dllmain.cpp,添加自定义Helper.h和Helper.cpp

3.添加要在外部调用的方法

复制代码
//头文件
#define DLLEXPORT extern "C" __declspec(dllexport)
DLLEXPORT int _stdcall Addition(int x, int y);
DLLEXPORT int _stdcall create_video_decoder(int codec_id, void **handle);

//源文件
DLLEXPORT int _stdcall Addition(int x, int y)
{
	return x + y;
}

DLLEXPORT int _stdcall create_video_decoder(int codec_id, void **handle)
{
    //...
}

4.配置x64或x86,编译,得到dll

二、Unity3d中调用

1.在Unity3d工程Assets建立Plugins文件夹,将dll及依赖dll都复制到该文件夹下

2.在代码中引入函数

注意:C#函数的名字可以与C++中的函数可以不一样。但EntryPoint中的名字必须与C++函数名一样。

复制代码
[DllImport("MyFFmpegHelper")]
public static extern int Addition(int x, int y);

[DllImport("MyFFmpegHelper", EntryPoint = "create_video_decoder", CallingConvention = CallingConvention.Cdecl)]
private static extern int CreateVideoDecoder(FFmpegVideoCodecId videoCodecId, out IntPtr handle);

3.设置x64或x86以及.net版本

相关推荐
eggwyw11 小时前
C++ union 的一种妙用法
开发语言·c++
ShineWinsu12 小时前
对于Qt:QHBoxLayout常见属性以及方法的解析
c++·qt·面试·gui·客户端·布局管理器·水平布局器
小七在进步13 小时前
string的实现(1)
c++·笔记·visual studio
敲上瘾15 小时前
Qt核心机制:信号与槽详解
开发语言·c++·qt·ui·qt6.3
牢姐与蒯17 小时前
cpp算法之哈希
c++·算法·哈希算法
lcj251118 小时前
[C++] C++11
开发语言·c++·笔记
小小bugbug21 小时前
带工作窃取的线程池
c++·线程池
南风84112 天前
C++ 四种强制类型转换详解
c++
微笑星河q2 天前
C++STL【vector】常用接口函数使用和迭代器失效问题
开发语言·c++
HaiXCoder2 天前
C++ 表达式怎么读:从一行代码到类型、值类别和生命周期
c++