如何从Unreal Engine 4 C ++调用C#函数?

问题描述 投票:0回答:1

我在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#";
        }
    }
}

并且在虚幻的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” {}

应该使用块,但我不知道如何以及在何处放置它。

有人可以帮我这个忙吗?

c# c++ unreal-engine4
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.