I have an MFC application that generates some reports and shows the same in the GUI. I have a requirement of running it as a console application, when passed with certain commandline argument. In the console mode it will generate the reports/errors in standard output/error and I should be able to redirect the same to any file.
For Ex.
C:/temp MyApp.exe --console > report.txt
should run my exe in console mode and redirect all the output to a text file.
But if I run it without any console argument, it should be like a default MFC application.
To achieve my requirement, so far I have done is, changed the Linker > System > Subsytem
from Windows
to Console
and added WinMainCRTStartup
at
Linker > Advanced > Entry Point
So now my app works fine when I run it with --console
parameter from console/batch files.
But when I run it directly, it still opens a cmd window (of course because it is now a console application). However, I am using FreeConsole()
method to get rid of it but it still flashes for a brief second.
So I am just curious if there is a way to get rid of it completely, either by deciding the application's subsytem at run time or any other trick? Any suggestion will be appreciated.