Create Blueprint Library, print to log and using windows.h in a Unreal Engine C++ project

When you need to use some specific Windows API functions,
such as LoadLibrary for loading DLL, or CreateFileMapping for using shared memory,
you need to use #include <windows.h> in your project's C++ code.

To be sure all building successfully, please insert this with "WIN32_LEAN_AND_MEAN" define, and use it as early as possible (I use it as the second #include in CPP code):

#include "MyBlueprintFunctionLibrary.h"
#define WIN32_LEAN_AND_MEAN
#include <windows.h>


For advanced capabilites, such as using GEngine class, it's required to include "Engine.h". It's required to do before including <windows.h> :

#include "MyBlueprintFunctionLibrary.h"
#include "Engine.h"
#define WIN32_LEAN_AND_MEAN
#include <windows.h>



Here is example how to load DLL using Windows API in Blueprint Function Library C++ class.
Also, it demonstrates print to screen and to log in Unreal Engine C++ and convert FString to std::string.

.h-file
Here you can see how to declare functions in Blueprint Function Library.



#pragma once

#include "CoreMinimal.h"
#include "Kismet/BlueprintFunctionLibrary.h"
#include "MyBlueprintFunctionLibrary.generated.h"

UCLASS()
class OSCSEND_API UMyBlueprintFunctionLibrary : public UBlueprintFunctionLibrary
{
    GENERATED_BODY()


    UFUNCTION(BlueprintCallable, Category = "OSC",    meta = (DisplayName = "OSC Receive Start"))
        static void osc_receive_start(int port);
    //NOTE: for further usage also need stop too

    UFUNCTION(BlueprintCallable, Category = "OSC", meta = (DisplayName = "OSC Receive Message"))
        static FString osc_receive();

    UFUNCTION(BlueprintCallable, Category = "OSC", meta = (DisplayName = "OSC Send Message"))
        static void osc_send(FString host, int port, FString message);


};

 


.cpp-file






Here  is demonstrated how to print to screen and log, convert FString to std::string

#include "MyBlueprintFunctionLibrary.h"

#include "Engine.h"
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
 

#include <string>

//------------------------------------------------------------------
//Common functions
#define _PRINT(text) { if (GEngine) GEngine->AddOnScreenDebugMessage(-1, 2, FColor::White,std::string(text).c_str()); }
#define _PRINT_FSTRING(text) { if (GEngine) GEngine->AddOnScreenDebugMessage(-1, 2, FColor::White,text); }
#define _LOG(text) UE_LOG(LogTemp, Warning, TEXT(text));

std::string toString(FString v) {
    return TCHAR_TO_UTF8(*v);
}
//------------------------------------------------------------------

HMODULE hModule_ = 0;    //handle to DLL

typedef  void(_cdecl* send_command_DEF)  (const char *s, const char *host, int port);
typedef void (_cdecl* start_receiver_DEF) (int port);
typedef  const char *(_cdecl* receive_message_DEF)  ();

send_command_DEF            send_command = 0;
start_receiver_DEF            start_receiver = 0;
receive_message_DEF         receive_message = 0;


//------------------------------------------------------------------
void load_dll() {
    if (!hModule_) {
        FString file_name = FPaths::GameDir() + "Binaries/Win64/SimpleOSC.dll";
        if ((hModule_ = LoadLibrary(*file_name)) == NULL) {
            _PRINT_FSTRING("ERROR: Can't load OSC DLL: " + file_name);
        }
        else {
            _PRINT("OSC DLL is loaded");
        }

        send_command = (send_command_DEF)GetProcAddress(hModule_, "send_command");
        start_receiver = (start_receiver_DEF)GetProcAddress(hModule_, "start_receiver");
        receive_message = (receive_message_DEF)GetProcAddress(hModule_, "receive_message");
        if (!send_command) {
            _PRINT("OSC DLL ERROR: no send_command");
        }
        if (!start_receiver) {
            _PRINT("OSC DLL ERROR: no start_receiver");
        }
        if (!receive_message) {
            _PRINT("OSC DLL ERROR: no receive_message");
        }

    }
}


//------------------------------------------------------------------
void UMyBlueprintFunctionLibrary::osc_receive_start(int port) {
    load_dll();
    if (start_receiver) {
        start_receiver(port);
    }
}

//------------------------------------------------------------------
FString UMyBlueprintFunctionLibrary::osc_receive() {
    if (receive_message) {
        return FString(receive_message());
    }
    return FString("");
}

//------------------------------------------------------------------
void UMyBlueprintFunctionLibrary::osc_send(FString host, int port, FString message) {
    load_dll();
    if (send_command) {
        std::string s = toString(message);
        std::string shost = toString(host);
        send_command(s.c_str(), shost.c_str(), port);
    }

}

//------------------------------------------------------------------


Comments

Popular posts from this blog

Computing ray origin and direction from Model View Projection matrices for raymarching

Forward and backward alpha blending for raymarching

Forward, Deferred and Raytracing rendering in openFrameworks and web