Application logs on the command prompt from where application is being launched

1.2k views Asked by At

I have an MFC application and launching its .exe from command prompt. What I want to achieve is that I want to show any logs from this launched application on the same command prompt.

I have written this code:

AttachConsole(ATTACH_PARENT_PROCESS);
WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), errorMessage, ((string)errorMessage).length(), NULL, NULL);
FreeConsole();

But this code writes the log at the next prompt like this:

c:\Users>application.exe

c:\Users>log line

Here I want the logs on the same prompt, like this:

c:\Users>application.exe

log line

I am stuck with this issue.

2

There are 2 answers

5
Marcus Müller On BEST ANSWER

The program works just fine; what you see is the command prompt that cmd.exe has prepared for you already, because your program detached and ran isolatedly from the console. As long as you don't detach, your program will continously occupy the console window.

0
marom On

I would suggest you to implement logging via OutputDebugString function. It allows you to see easily the result in the debugger and you can see that text also without a debugger using tools like DebugView (once upon a time on sysinternals.com, now available on Microsoft's site)