C# 调用C++链接库与回调

来源:互联网 发布:2016年淘宝用户数量 编辑:程序博客网 时间:2024/05/18 01:28

***********************************************************************************************************
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 <iostream>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);


0 0
原创粉丝点击