2016-08-12 58 views
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++應用程序執行。

我不確定如何定義這些函數和變量。任何幫助,將不勝感激。

回答

0

我設法弄清楚了自己。一看便知下面

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!! 
相關問題