封装Unity动态调用C++dll库

VS2017中创建C++桌面向导,选择动态链接库空工程后,分别在头文件和源文件下创建头文件和源文件。代码如下:

#pragma once

extern "C" __declspec(dllexport) int Add(int *a, int *b);

extern "C" __declspec(dllexport) char* testDllMethod(char* value);

extern "C" __declspec(dllexport) char* testDllLoad(char* value, char* dllPath, char * method);

extern "C" __declspec(dllexport) char* testDllLoad11(char* value, char* dllPath, char * method);

 

 

#include <Windows.h>
#include <iostream>
#include "testDLL.h"
using namespace std;

int Add(int *a, int *b){
	return *a + *b;
}

char * testDllMethod(char * value)
{
	char * v = new char[5]{ 's', 'u','c','c','e' };
	return v;
}

char * testDllLoad(char * value, char * dllPath , char * method)
{
	char * v1 = new char[5]{ 's', 'u','c','c','1' };
	char * v2 = new char[5]{ 's', 'u','c','c','2' };
	char * v3 = new char[5]{ 's', 'u','c','c','3' };
	typedef char*(*ptestDllMethod)(char *value);
	HINSTANCE hDLL = LoadLibrary(dllPath);
	if (NULL != hDLL) {
		ptestDllMethod pFun = (ptestDllMethod)GetProcAddress(hDLL, method);

		if (NULL != pFun) {
			//char * v = new char[5]{ 'c', 'y','u','p','j' };
			//cout << "cyupj " << value << "   " << pFun(value) << endl;
			//cout << "111111" << endl;
			return pFun(value);
			//delete v;
		}
		else
		{
			//cout << "222222" << endl;
			//cout << "Can't acquire Function " << "testDllMethod" << endl;
			return v1;
		}
		FreeLibrary(hDLL);
	}
	else
	{
		//cout << "3333333" << endl;
		//cout << "Can't Load DLL " << "testDLL" << endl;
		return v2;
	}
	//getchar();
	return v3;
}

char * testDllLoad11(char * value, char * dllPath, char * method)
{
	//LPCSTR aaa = (LPCSTR)dllPath;
	return testDllLoad(value, dllPath, method);
}


代码未整理,带有很多无用测试代码,自己记录为主。

 

注意debug和release还有设置中字符集是无定义还是

 

github已上传

 

另一种方式,可直接在unity使用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Windows;
using RoutePlanningData.json;
using System.IO;

namespace Brinav
{
    class TrackCallDLL
    {
        delegate string TrackDLL(string str);
        public static string GetValue(string input)
        {
            string result = "";
            int hModule = DLLWrapper.LoadLibrary(System.Windows.Forms.Application.StartupPath + "/Track/PathTracker.dll");
            if (hModule == 0)
            {
                return "0";
            }
            IntPtr intPtr = DLLWrapper.GetProcAddress(hModule, "TrackerDLL");
            TrackDLL TrackDLLFunc = (TrackDLL)Marshal.GetDelegateForFunctionPointer(intPtr, typeof(TrackDLL));
            // string value = AstarDLLFunc(str1);
            //File.WriteAllText(@"C:\Users\BriNav\Desktop\A.txt", inPut);
            result = TrackDLLFunc(input);
            DLLWrapper.FreeLibrary(hModule);
            return result;
        }
    }
}


public class DLLWrapper
{
	[DllImport("kernel32.dll", EntryPoint = "LoadLibrary")]
	public static extern int LoadLibrary(
		[MarshalAs(UnmanagedType.LPStr)] string lpLibFileName);

	[DllImport("kernel32.dll", EntryPoint = "GetProcAddress")]
	public static extern IntPtr GetProcAddress(int hModule,
		[MarshalAs(UnmanagedType.LPStr)] string lpProcName);

	[DllImport("kernel32.dll", EntryPoint = "FreeLibrary")]
	public static extern bool FreeLibrary(int hModule);
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值