Outputting from a .exe to cmd - c++

I am writing a little program in c++ which creates an .exe which i then run by calling it with parameters in cmd. I want to be able to display output from the .exe into the cmd that I ran it from. I currently have this code which opens a new cmd window to display output which is close but not what i want. any help with this would be great! thanks.
AllocConsole();
DWORD NumberOfBytesWritten = 0;
WriteFile( GetStdHandle(STD_OUTPUT_HANDLE), strLog1, lstrlen(strLog1), &NumberOfBytesWritten, 0);
Update:
I have also been able to write to a text file using dir > log.txt in the command window when calling the program, is there a way I can change this so that it directs outputs to the console window? Thanks,

My psychic debugging powers tell me that your build tools are configured to create your application in GUI rather than console mode.
If you reconfigure the build so that it generates a console mode application, you won't need to call AllocConsole or do anything special; you'll automatically be assigned to the console of the parent process.

Did you try simple operations, such as:
std::cout << "Print me" ;
or
std::cerr << "Print me too";
?
(I hope I understood correctly that you want to print to the same console where you started your app)

That question has already been asked: How to output to the console in C++/Windows. Here is an answer that seems to be useful in your case: https://stackoverflow.com/a/587792/1728537

Related

c++ problem | i want to NOT open the console while an program running

Ya know the console opens everytime when u execute an c++ script in any way i am making currently an GDI effect and this Console shows in the background up Here is the script but i dont think the script affects everything :/enter image description here
You should use /SUBSYSTEM:WINDOWS with WinMain.
Alternatively, you could hide the console.
ShowWindow(GetConsoleWindow(), SW_HIDE);

QProcess doesn't show the command window

I have the following code which runs an exectuable using QProcess. The code all runs fine and the new executable runs and is all fine.
QString fileName = ui.textBrowser_csvFile->toPlainText();
QString tableName = ui.textBrowser_2->toPlainText();
QString program = "resources/myExe.exe";
QStringList arguments;
arguments << tableName << fileName;
bool res = QProcess::startDetached(program, arguments);
It is a Qt Console Application using QCoreApplication and there it doesn't spawn the terminal window like it would if I run it normally. It would be useful to monitor the progress of the executable so how do I get my Application to run the new program and display the terminal window?
Edit Possible duplicate does technically answer the question, but I have answered this question with a working solution.
So as discussed in the comments on my questions this StackOverflow post explains that this is infact correct behaviour when using the startDetached() function.
I'm not entirely sure what the answer to that question was suggesting to do but here is my working solution.
system() is a windows specific function which "can execute any command that can run on terminal if operating system allows" link
If I replace this line:
bool res = QProcess::startDetached(program, arguments);
with the following, then it works:
system(QString("D:\\Qt\\5.9.1\\msvc2017_64\\bin\\myApp.exe " +tableName +" " + fileName).toStdString().c_str());
In the short term I have simply moved this application into the Qt folder because it needs the DLLs however with a proper release of this app you can run it from wherever, including from next to the application that is running it.
I do then get a terminal window and my app runns correctly.
When migrating from Qt 5.7.0.0(x86) to 5.10.0.0(x64) I was really surprised to see that using the new Qt version, a child (launched with "QProcess::startDetached") process will not show up it's console (even though it's a console application! (SubSystem:CONSOLE))
MS documentation regarding "AllocConsole" says:
Console applications are initialized with a console, unless they are
created as detached processes (by calling the CreateProcess function
with the DETACHED_PROCESS flag).
https://learn.microsoft.com/en-us/windows/console/allocconsole
Console processes are not attached to a console if they are created
using CreateProcess with DETACHED_PROCESS
https://learn.microsoft.com/en-us/windows/console/creation-of-a-console
So I'm assuming that new Qt versions are using the "CreateProcess" with the "DETACHED_PROCESS" flag.
What I've ended up doing:
For child process I'm now using "SubSystem:WINDOWS". (Really
important)
Inside the child process I'm creating the new console by using "AllocConsole()"
Using: "freopen("CONOUT$", "w", stderr);" and "freopen("CONOUT$",
"w", stdout);" ("stderr" is really important if you want to capture qDebug, qInfo, etc...)
P.s.
If you would need to use "SubSystem:CONSOLE", be sure to call "FreeConsole" before calling "AllocConsole". This is required, because child process will by default be using parent process console...

Simple "Hello World" code does not appear on Command Prompt

So I have been watching a tutorial (here: https://youtu.be/yKATaptz3Dc) to complete my first line of code. My code is identical to the one in the video yet nothing appears on my console. I'm assuming there is some settings issue in my version of Code::Blocks but I have no idea where to begin. Ideas?
#include<iostream>
using namespace std;
int main ()
{
cout << "Hello World!";
return 0;
}
This is an exceedingly common problem, and almost always goes to this issue:
You are executing your program incorrectly.
A console program expects to be run from a console. Yet you are probably running it from your IDE or double-clicking its icon from Explorer, which causes Windows to create a new console, run your program, and then destroy the console. Sometimes you will see the new console window flash briefly on the screen, sometimes you will not.
To run the program correctly, use Explorer to find your program's exe, click on the location bar at the top and type "cmd" and press Enter. You'll get a console with the current directory at your exe. Type the name of your exe file. Enjoy the output.
Another common response is to add code that tries to do a "Press enter to continue..." kind of thing before return 0;, but that is really not the correct way to do it.
Hope this helps.

Open console inside gui application

I'm trying to create a program in C++, which open with him, within the form, the CMD, so that I can run a program within the CMD that is inside the program. As an example, in this photo:
I found some examples, but could not implement them in code, so, I ask your help... To be more precise, I want to create a function in a dll, so I can call this CMD, starting from any language
basically an embedding a Console
I see two methods:
Write your own console widget and pass all entered command to the std::system command
Do what is described in How to open a console window in a Win32 application

C++ on Windows - the console window just flashes and disappears. What's going on? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Visual Studio Console App - Prevent window from closing.
I'm starting to learn C++ on Windows and I'm trying a few different development environments:
1. Netbeans with Cygwin compiler
2. MS Visual Studio 2010
For either of them, when I write a very simple Hello World program, I build it and it's fine. But when I try to run the program, the command prompt window pops up really quick and then disappears right away.
This happens whether it's in Debug or Release config. Please help with this - I can't see my program output! :(
Thanks.
EDIT1:
Thanks for the replies. This is my code:
#include <iostream>
int main()
{
std::cout << "This is a test." << std::endl;
return 0;
}
I tried Ctrl+F5 for "Start without Debugging" and that doesn't work. It still flashes the black console screen then disappears right away.
I also tried adding in std::cin.get(); and this works with Ctrl+F5, but isn't that a really... inelegant workaround solution? I'd rather have my program in the final form.
The breakpoint works, but then I have to run with debugging and the console window flashes and disappears, but then it stays in the background. Any way to get the console to stay in the foreground so I can see program output right away? Seems like that's how it should work.
Any more ideas? Why wouldn't Ctrl+F5 work?
After you are done with your program, press Ctrl + F5 ( Run without debugging). This will prompt before closing the window and this is what you want.
Write cin.get() at the end of the program.
use Ctrl+F5 to run your program or set a break point in the last line or write cin>> to any vraiable at the end....etc
I think your program just prints Hello World and then exits. That's the reason the console closes immediately. You can run the executable from Command Prompt (Start Menu > Run and type cmd.exe). Otherwise, you can put std::cin.get() in your code so that program waits for user's input and hence the console window remains open until a key is pressed.
Your application is probably working. Make the last command in your console application wait for user input: e.g int i;
string i;
cout<<"Hello";
cin<<i;
Issue a getchar() before returning or run from cmd.exe