0
我正在使用Robert Giesecke的Unmanaged Exports從我的本地C++應用程序調用C#託管dll中的函數。我現在需要以某種方式將函數指針從我的C++應用程序傳遞到C#dll,以便我的dll可以對C++執行回調。這讓我很難過。如何發送函數指針使用非託管導出從C++到C#dll用作回調
C++
//Normal call to CSharp dll using Unmanaged Exports
FString UHostBridgeComponent::DoCallToCCsharp()
{
FString filePath = FPaths::Combine(*FPaths::GamePluginsDir(), TEXT("ThirdParty"), TEXT("CSharp.dll"));
void *DLLHandle = NULL;
if (FPaths::FileExists(filePath))
{
DLLHandle = FPlatformProcess::GetDllHandle(*filePath); // Retrieve the DLL.
}
if (DLLHandle != NULL)
{
_DoTest DLLFuncPtr = NULL;
FString procName = "DoTest";
DLLFuncPtr = (_DoTest)FPlatformProcess::GetDllExport(DLLHandle, *procName);
if (DLLFuncPtr != NULL)
{
const char* result = DLLFuncPtr(false);
FString output(result);
return output;
}
}
return "";
}
C#
//Function called from C++ application
[DllExport("DoTest", CallingConvention = CallingConvention.StdCall)]
public static string DoTest(bool result)
{
//Do processing
//...
string result = "this is the result string";
return result;
}
我會想象我需要調用在C#中的功能時,將指針傳遞給函數在C++中。
const char * result = DLLFuncPtr(pointerToMyFunction);
即指針必須被保存到C#的變量並作爲回調當C#DLL要發送數據到C++應用程序執行。
我不確定如何定義這些函數和變量。任何幫助,將不勝感激。