0

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,

Alex K.
  • 171,639
  • 30
  • 264
  • 288
Mike
  • 13
  • 2

3 Answers3

1

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.

Harry Johnston
  • 35,639
  • 6
  • 68
  • 158
  • Your not wrong its a win app! Ill convert this to a console app right away as i cant see any reason for it being a win app. Seems like the easiest thing to do :) – Mike Feb 20 '14 at 07:46
0

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)

Ferenc Deak
  • 34,348
  • 17
  • 99
  • 167
0

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

Community
  • 1
  • 1
lrineau
  • 6,036
  • 3
  • 34
  • 47
  • I looked at this thread before posting, from what i read it requires using allocconsole, this creates a new console window to put messages in where as i want them to appear in the original window it is launched from – Mike Feb 19 '14 at 18:04