Encapsulate Unity to dynamically call C++dll library

Create a C++ desktop wizard in VS2017. After selecting the dynamic link library empty project, create header files and source files under header files and source files respectively. code show as below:

#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);
}


The code is not organized, with a lot of useless test codes, mainly recorded by myself.

 

Pay attention to debug and release and whether the character set in the settings is undefined or

 

github uploaded

 

Another way, can be used directly in 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);
}

 

Guess you like

Origin blog.csdn.net/yushengqi12345/article/details/107405967