近期收到一个语音识别需求,查阅一系列资料测试后,ue可使用的识别插件精度感人,但是unity的语音识别精度在可接受范围内,经过查询unity调用的Windows.Media.SpeechRecognition,属于uwp层面,各种资料都是c#,那么能不能用c#封装导出给c++呢,所以有了这条博文

 

新建一个类库

项目中–管理NuGet程序包,添加UnmanagedExports

 

编写导出代码

using System.Runtime.InteropServices;
using RGiesecke.DllExport;

namespace dllforue4
{
    internal class testdll
    {
        [DllExport("GetStr", CallingConvention = CallingConvention.StdCall)]
        static public string Getstr(string path)
        {
            string str = "hello";
            return str + path;
        }

    }
}

进行编译

 

在ue4中调用  创建一个c++蓝图方法库

bool UCreateAndLinkDLLTutBFL::importDLL(FString folder, FString name)
{
	FString filePath = *FPaths::GamePluginsDir() + folder + "/" + name;
	
	if (FPaths::FileExists(filePath))
	{
		v_dllHandle = FPlatformProcess::GetDllHandle(*filePath); // Retrieve the DLL.
		if (v_dllHandle != NULL)
		{
			typedef char* (*_GetDomain)(char*);
			_GetDomain DLLFuncPtr = NULL;
			FString procName = "GetStr";
			DLLFuncPtr = (_GetDomain)FPlatformProcess::GetDllExport(v_dllHandle, *procName);
			if (DLLFuncPtr != NULL)
			{
				char* input = "hello, test info";
				char* result = DLLFuncPtr(input);
				//GEngine->AddOnScreenDebugMessage(-1, 20.0, FColor::Red, input);
				GEngine->AddOnScreenDebugMessage(-1, 10.0, FColor::Red, result);
				return true;
			}
			return true;
		}
	}
	return false;	// Return an error.
}

 

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐