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版本

相关推荐
重启的码农23 分钟前
llama.cpp 分布式推理介绍(5) RPC 通信协议
c++·人工智能·神经网络
liulilittle25 分钟前
UTF-8 编解码可视化分析
c++·字符串·unicode·string·字符·char·utf8
重启的码农27 分钟前
llama.cpp 分布式推理介绍(6) 张量序列化 (rpc_tensor)
c++·人工智能·神经网络
让我们一起加油好吗1 小时前
【C++】模板(进阶)
c++·visualstudio·模板·泛型编程
雨落倾城夏未凉1 小时前
2.如何创建自己的类
c++·后端
Ustinian_3102 小时前
【C/C++】For 循环展开与性能优化【附代码讲解】
c语言·开发语言·c++
MZ_ZXD0018 小时前
springboot汽车租赁服务管理系统-计算机毕业设计源码58196
java·c++·spring boot·python·django·flask·php
岁忧10 小时前
(nice!!!)(LeetCode 每日一题) 679. 24 点游戏 (深度优先搜索)
java·c++·leetcode·游戏·go·深度优先
小欣加油10 小时前
leetcode 3 无重复字符的最长子串
c++·算法·leetcode
zylyehuo13 小时前
C++基础编程
c++