Question

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,

Was it helpful?

Solution

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.

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top