C# 调用C++链接库与回调
发布日期:2021-06-30 19:37:52 浏览次数:2 分类:技术文章

本文共 1302 字,大约阅读时间需要 4 分钟。

***********************************************************************************************************
C++部分
//MyTest.h
#ifdef  _WIN32_#ifdef  MY_TEST_EXPORTS#define MY_TEST_API __declspec(dllexport)#define API_STDCALL __stdcall#else#define MY_TEST_API __declspec(dllimport)#endif#else#define MY_TEST_API#define CALLBACK#define API_STDCALL#endif#ifndef _MY_TEST_H_#define _MY_TEST_H_#endif
//MyTest.cpp
#include "MyTest.h"#include 
typedef void (CALLBACK *MyTestCallBack)(char *szBuffer);MyTestCallBack m_myTestCallBack;extern "C" MY_TEST_API void TestApi(const char* str,MyTestCallBack callback){ callback("hello,I recv your str:"+str);}
***********************************************************************************************************
C#部分
using System.Collections;using System.Runtime.InteropServices;using System;using System.Collections.Generic;using UnityEngine;public class CSTest{public delegate void Cpp2CsCallBack(string str);#if UNITY_ANDROID[DllImport("u3dmydll")]public static extern void CallCppDllApi(string str,Cpp2CsCallBack cb);#else[DllImport("u3dmydll.dll")]public static extern void CallCppDllApi(string str,Cpp2CsCallBack cb);#endifpublic static void onDllCallBack(string str){Debug.Log(str);}}
调用:
CSTest.CallCppDllApi("hello dll",CSTest.onDllCallBack);

转载地址:https://linxinfa.blog.csdn.net/article/details/46481803 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:C#给C++传参的兼容问题
下一篇:C# 数据封装和解析

发表评论

最新留言

很好
[***.229.124.182]2024年04月11日 19时27分02秒