Search code examples
c++eventsunreal-engine4eventhandler

Triggering event of C++ dll in unreal engine


I am trying to use dll which reads incoming messages with a thread.Importing into unreal engine with Blueprint class, As I can't get the incoming messages. I am planning to have a event in the dll for which I'll put event handler in unreal engine code.

Can you please help me, Is this possible If it is how to work with it ?

I am trying something like this enter link description here


Solution

  • In dll code

    EventCallback   OnEvent = NULL;
    DLL_EXPORT void  WINAPI SetEventCallback(EventCallback func)
    {
        OnEvent = func;
    }
    DLL_EXPORT void WINAPI FireEvent()
    {
        if (OnEvent)
            OnEvent();
    }
    

    In Unreal engine code

    typedef void(*_getSetEventCallback)(EventCallback);
    _getSetEventCallback m_getSetEventCallbackFromDll=NULL;
    
    void CALLBACK HandleEvent()
    {   
         FString fstringVar = "Triggered by Event in dll";
         UE_LOG(LogTemp, Warning, TEXT("%s"), fstringVar);
    }
    
    void UVectorDll::importSetEventCallback()
    {
        if (c_dllHandle != NULL)
        {
            FString procName = "SetEventCallback";    // Needs to be the exact name of the DLL method.
            m_getSetEventCallbackFromDll = (_getSetEventCallback)FPlatformProcess::GetDllExport(c_dllHandle, *procName);
            m_getSetEventCallbackFromDll(HandleEvent);
        }
    }
    

    FireEvent can be done by dll or from Unreal Engine