[英]C#: How to receive a double pointer to struct from a C++ unmanaged function callback
[英]How to send a function pointer using Unmanaged Exports from C++ to C# dll to be used as callback
我正在使用Robert Giesecke的Unmanaged Exports從我的本機C ++應用程序中調用C#managed dll中的函數。 我現在需要以某種方式將函數指針從我的C ++應用程序傳遞給C#dll,以便我的dll可以回調到c ++。 這讓我很難過。
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 ++應用程序時作為回調執行。
我不確定如何定義這些函數和變量。 任何幫助,將不勝感激。
我設法弄明白了。 見下面的答案
C ++
//Function pointer typedef
typedef bool(*functionPointer)(const char* data);
//the actual function that gets pointed to
bool UHostBridgeComponent::realFunction(const char * data)
{
FString output(data);
UE_LOG(LogEGM, Log, TEXT("CALLBACK FUNCTION data= %s"), *output);
return true;
}
//function pointer as variable (not yet pointing to realFunction)
functionPointer myFunc;
//function to call in C# that passes the function pointer
typedef bool(*_DoSendCallbackFunction)(functionPointer callback);
bool UHostBridgeComponent::DoCallbackTest()
{
FString filePath = FPaths::Combine(*FPaths::GamePluginsDir(), TEXT("ThirdParty"), TEXT("CSharp.dll"));
void *DLLHandle = NULL;
if (FPaths::FileExists(filePath))
{
DLLHandle = FPlatformProcess::GetDllHandle(*filePath);
}
if (DLLHandle != NULL)
{
_DoSendCallbackFunction DLLFuncPtr = NULL;
FString procName = "DoSendCallbackFunction";
DLLFuncPtr = (_DoSendCallbackFunction)FPlatformProcess::GetDllExport(DLLHandle, *procName);
if (DLLFuncPtr != NULL)
{
myFunc = &realFunction; //point myFunc to the function realFunction
return DLLFuncPtr(myFunc);
}
}
return false;
}
C#
public delegate bool FooDelegate(string data);
[DllExport("DoSendCallbackFunction", CallingConvention = CallingConvention.StdCall)]
public static bool DoSendCallbackFunction(IntPtr callback)
{
FooDelegate myFooDelegate = (FooDelegate)Marshal.GetDelegateForFunctionPointer(callback,typeof(FooDelegate));
string theString = "This is the data!!";
myFooDelegate(theString);
return true;
}
並將結果打印到我的日志文件中:
LogEGM: CALLBACK FUNCTION data= This is the data!!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.