I tried to create a blueprint function using C++, but failed.
The function is not visible in the blueprint.
RunExternalTest.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "RunExternalTest.h"
//Sets default values
ARunExternalTest::ARunExternalTest()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
}
// Called when the game starts or when spawned
void ARunExternalTest::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void ARunExternalTest::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
/// <summary>
/// Execute external file
/// </summary>
UFUNCTION(BlueprintCallable, Category = "ControlExternalFiles")
void ARunExternalTest::RunExternalExeFile(const FString& AppPath, bool& Result, int& ProcessId)
{
//Process ID
uint32 OutProcessId = 0;
ProcessId = 0;
Result = false;
//Execute file
FProcHandle Proc = FPlatformProcess::CreateProc(*AppPath, NULL, true, false, false, &OutProcessId, -1, nullptr, nullptr);
if (Proc.IsValid())
{
//If executed, return True to Result
ProcessId = OutProcessId;
Result = true;
}
else {
//If it does not run, rerun with administrator privileges
Result = FPlatformProcess::ExecElevatedProcess(*AppPath, NULL, nullptr);
}
}
/// <summary>
/// Close the running external file
/// </summary>
UFUNCTION(BlueprintCallable, Category = "ControlExternalFiles")
void ARunExternalTest::TerminateExternalProgram(const int& ProcessId, bool& IsRunning)
{
//Receive ProcHandle as Uint
uint32 ProcessIdUint = ProcessId;
FProcHandle Proc = FPlatformProcess::OpenProcess(ProcessIdUint);
//Check if it is running
IsRunning = FPlatformProcess::IsProcRunning(Proc);
if (IsRunning)
{
//Close if running
FPlatformProcess::TerminateProc(Proc);
FPlatformProcess::CloseProc(Proc);
}
}
/// <summary>
/// Check file execution
/// </summary>
UFUNCTION(BlueprintCallable, Category = "ControlExternalFiles")
void ARunExternalTest::GetExecutableName(const FString& FileName, bool& result)
{
FStringName = FileName;
result = FPlatformProcess::IsApplicationRunning(*Name);
}
RunExternalTest.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "RunExternalTest.generated.h"
UCLASS()
class MAIN_API ARunExternalTest : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
ARunExternalTest();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
UFUNCTION(BlueprintCallable, Category = "ControlExternalFiles")
void RunExternalExeFile(const FString& AppPath, bool& Result, int& ProcessId);
UFUNCTION(BlueprintCallable, Category = "ControlExternalFiles")
void TerminateExternalProgram(const int& ProcessId, bool& IsRunning);
UFUNCTION(BlueprintCallable, Category = "ControlExternalFiles")
void GetExecutableName(const FString& FileName, bool& result);
};