我在C#dll上课:
namespace MyNamespace
{
public class Class1
{
[DllExport("TestFunction", CallingConvention = CallingConvention.StdCall)]
static public void TestFunction()
{
}
[DllExport("PrintHello", CallingConvention = CallingConvention.StdCall)]
static public string PrintHello()
{
return "Hello Unreal from C#";
}
}
}
在Unreal的C ++代码中,我有一个调用它的函数:
typedef const char*(*_PrintHello)();
void AMXPlayerController::TestCSarpCall()
{
FString filePath = FPaths::Combine(*FPaths::GamePluginsDir(), TEXT("MyNamespace.dll"));
void *DLLHandle = NULL;
if (FPaths::FileExists(filePath))
{
DLLHandle = FPlatformProcess::GetDllHandle(*filePath); // Retrieve the DLL.
}
if (DLLHandle != NULL)
{
_PrintHello DLLFuncPtr = NULL;
FString procName = "PrintHello";
DLLFuncPtr = (_PrintHello)FPlatformProcess::GetDllExport(DLLHandle, *procName);
if (DLLFuncPtr != NULL)
{
const char* result = DLLFuncPtr();
FString output(result);
FString test = output;
UE_LOG(LogTemp, Error, TEXT("%s"), *test);
}
}
}
问题出在这一行:
DLLFuncPtr = (_PrintHello)FPlatformProcess::GetDllExport(DLLHandle, *procName);
它返回NULL,因此找不到C#中的函数。我已经阅读过这个问题了,他们说这是因为函数名称错误和
应该使用extern“C”{}
块,但我不知道如何以及在何处放置它。
有人可以帮帮我吗?