gpt4 book ai didi

c# - 如何使用非托管导出将函数指针从 C++ 发送到 C# dll 以用作回调

转载 作者:太空宇宙 更新时间:2023-11-04 13:12:51 25 4
gpt4 key购买 nike

我正在使用 Robert Giesecke 的非托管导出从我的 native C++ 应用程序调用我的 C# 托管 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!!

关于c# - 如何使用非托管导出将函数指针从 C++ 发送到 C# dll 以用作回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38917043/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com