Open cmd.exe window in UE4 C++

Hello Community!

I’m having a hard time trying to get a command prompt window to open. I’ve tried the following:



void ATestCommander::BeginPlay()
{
Super::BeginPlay();

FProcHandle WorkHandle = FPlatformProcess::CreateProc(TEXT("C:\\Windows\\System32\\cmd.exe"), nullptr, true, false, false, nullptr, 0, nullptr, nullptr);

if (WorkHandle.IsValid())
{
UE_LOG(LogTemp, Warning, TEXT("I DID IT!"));
}
else
{
UE_LOG(LogTemp, Warning, TEXT("DANG!"));
}

}


and I’ve tried



// Called when the game starts or when spawned
void ATestCommander::BeginPlay()
{
Super::BeginPlay();


bool bWorked = FPlatformProcess::ExecProcess(TEXT("C:\\Windows\\System32\\cmd.exe"), nullptr, false, false, false);

if (bWorked)
{
UE_LOG(LogTemp, Warning, TEXT("I DID IT!"));
}
else
{
UE_LOG(LogTemp, Warning, TEXT("DANG!"));
}

}


Both of them do return true with their conditions. I’ve tried other applications and they indeed do open just fine. Just Command Prompt seems to not open at all through UE4.

Is there something I need to do to open the command prompt in Windows?

Bumping this up… Anyone know why the commandline in windows won’t pop up with these methods?

I am posting as answer for future users.

The “bool bLaunchDetached” doesnt seem to be what is sounds. It activates the DETACHED_PROCESS flag for windows process creation but it isnt like the child process is detached from the parent, it’s the child CONSOLE which is detached from the parent.
If you set it to true (which you probably are if you are here), your child process wont be able to open its console and thus terminate immediatly.
Reference link: DETACHED_PROCESS (timgolden.me.uk)

So the simple solution for me was to simply set it to false and my newly created cmd outlived the game; which is what I wanted !

Bye yall !
(if it helped, a little heart?)

3 Likes

I would love to know more… I am not having any luck…

FProcHandle procHandle = FPlatformProcess::CreateProc(*app, *params, bLaunchDetached, bLaunchHidden, bLaunchReallyHidden, outProcess, priorityModifier, optionalWorkingDirectory, writePipe, readPipe);

I’ve tried both, true and false… When I set it to false, I see the command window appear for a split second, then disappear. However, setting it to true, I never see anything at all…

What I have noticed, if of it’s a console type exe, it won’t launch, but if it’s a windows based exe, it launches just fine. I even created two very simple exes to test this, one is a simple c++ console hello world app, the other is just a C# win form exe… Win form loads up and displays just fine, but the C++ console exe does not load up.