I try make dll callback. I make worked solution, but it use void function without my class, and i cant make some logic here. I try rework logic CatchErrorFunc to UWebClientHandler::CatchErrorFunc, and no see right way.
#include "WebClientHandler.h"
#include "WebController.h"
#include "Runtime/Core/Public/Misc/Paths.h"
void *DLLHandlerGet;
typedef void (* StringCallbackVoidF)(const char *data);
StringCallbackVoidF PtrCatchErrorFunc;
void CatchErrorFunc(const char* msg);
typedef void(*GetErrorCallbackVoidF)(StringCallbackVoidF callback);
void UWebClientHandler::Init()
{
Super::Init();
UE_LOG(LogTemp, Warning,TEXT("UWebClientHandler: init"));
ImportDLL();
RegCallBack();
TestCallback();
}
bool UWebClientHandler::ImportDLL()
{
FString folder = "WebSocketClient";
FString fileName = "websocket-sharp.dll";
FString filePath = *FPaths::ProjectPluginsDir() + folder + "/" + fileName;
if (FPaths::FileExists(filePath))
{
//UE_LOG(LogTemp, Warning,TEXT("UWebClientHandler:ImportDLL: dll path exist"));
DLLHandlerGet = FPlatformProcess::GetDllHandle(*filePath); // Retrieve the DLL.
if (DLLHandlerGet != nullptr)
{
//UE_LOG(LogTemp, Warning,TEXT("UWebClientHandler:ImportDLL: dll TRUE"));
return true;
}
//UE_LOG(LogTemp, Warning,TEXT("UWebClientHandler:ImportDLL: dll FALSE"));
}
return false;
}
void UWebClientHandler::RegCallBack()
{
if (DLLHandlerGet == nullptr)
{
UE_LOG(LogTemp, Warning,TEXT("UWebClientHandler: DLL is Null"));
return;
}
const FString RegisterErrorString = "RegisterErrorCB";
GetErrorCallbackVoidF GetCatchErrorVoid = (GetErrorCallbackVoidF)FPlatformProcess::GetDllExport(PtrCatchErrorFunc, *RegisterErrorString);
if (GetCatchErrorVoid != nullptr)
{
PtrCatchErrorFunc = &CatchErrorFunc;
GetCatchErrorVoid(PtrCatchErrorFunc);
UE_LOG(LogTemp, Warning,TEXT("UWebClientHandler: PtrCatchError reg = DONE"));
}
}
void UWebClientHandler::TestCallback()
{
if (DLLHandlerGet == nullptr)
{
UE_LOG(LogTemp, Warning,TEXT("UWebClientHandler: DLL is Null"));
return;
}
const FString ConnectString = "Connect";
typedef void (* ActionCallbackVoidF)();
ActionCallbackVoidF PtrConnect = (ActionCallbackVoidF)FPlatformProcess::GetDllExport(DLLHandlerGet, *ConnectString);
if (PtrConnect != nullptr)
{
PtrConnect();
UE_LOG(LogTemp, Warning,TEXT("UWebClientHandler: Connect()"));
}
}
//NOT WORKING
//void UWebClientHandler::CatchErrorFunc(const char* msg){ UE_LOG(LogTemp, Warning,TEXT("UWebClientHandler. EVENT ERROR: %hs"),msg); }
//WORK FINE
void CatchErrorFunc(const char* msg){ UE_LOG(LogTemp, Warning,TEXT("UWebClientHandler. EVENT ERROR: %hs"),msg); }