How to run the command and display it in the console? Topic is solved

Do you have a typical platform dependent issue you're battling with ? Ask it here. Make sure you mention your platform, compiler, and wxWidgets version.
Post Reply
qtSucks
In need of some credit
In need of some credit
Posts: 9
Joined: Fri Jan 22, 2021 6:32 pm

How to run the command and display it in the console?

Post by qtSucks »

Hi!
I have half a month of experience with wxWidgets past me and I came across this problem:

I want to execute some commands and display them in command window. But when I run them, they are just ran, not shown.

Example:

Code: Select all

#include<Windows.h>
// Some unneccesary wxWidgets code
system("cd C:\someFolder");
system("g++ test.cpp -o test.exe");
system("test.exe");
// Some unneccesary wxWidgets code
But I can only see some windows to open for 0.1 s each. These commands work in normal conditions (in the console), but not here.

How can I solve this problem?

I have g++ compiler (9.2.0), Windows 10 and wxWidgets 3.1.
Kvaz1r
Super wx Problem Solver
Super wx Problem Solver
Posts: 357
Joined: Tue Jun 07, 2016 1:07 pm

Re: How to run the command and display it in the console?

Post by Kvaz1r »

You can use wxExecute for running other process.
Also try:

Code: Select all

system("cd /d C:\someFolder");
qtSucks
In need of some credit
In need of some credit
Posts: 9
Joined: Fri Jan 22, 2021 6:32 pm

Re: How to run the command and display it in the console?

Post by qtSucks »

First solution:

I ran:

Code: Select all

wxString directory = "C:\someFolder";
wxExecute("cd " + directory, wxEXEC_SHOW_CONSOLE);
I get the error: Execution of command 'cd C:\someFolder' failed (error 2: The system cannot find the file specified.)

But the folder exists (I made sure.)

--------------------

Second solution:

Works same as my original solution (= fails).
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4182
Joined: Sun Jan 03, 2010 5:45 pm

Re: How to run the command and display it in the console?

Post by PB »

I think the file not found is "cd" which wxExecute takes as the name of the executable. However, "cd" is a shell command. I would try calling wxExecute with something like

Code: Select all

cmd /c cd c:\someFolder
I recommend playing with the bundled "exec" sample, where one can easily test wxExecute with various parameters.

I would also consider using "cd /d" to also switch the current drive. If you use a parameter for cd, and if the folder name you are changing into contains spaces, you should double/quote the name. See https://ss64.com/nt/cmd.html for more information about invoking cmd.exe.

BTW, I find odd that it works for you without doubling the slash in the string literal (i.e., "C:\someFolder") in the code snipet?
qtSucks
In need of some credit
In need of some credit
Posts: 9
Joined: Fri Jan 22, 2021 6:32 pm

Re: How to run the command and display it in the console?

Post by qtSucks »

It works:

Code: Select all

wxExecute("cmd /k cd \"" + directory + "\" && g++ " + file + "." + extension + " -o " + file + ".exe && " + file + ".exe", wxEXEC_SHOW_CONSOLE);
First I run cmd.exe, set option /k, move to directory, build and run the program and run the .exe file.

Thank you for all the help!
Post Reply