封装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);
}

猜你喜欢

转载自blog.csdn.net/yushengqi12345/article/details/107405967
今日推荐