Click here to Skip to main content
15,889,874 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a win32 project, I am calling my exe using following command in command prompt.
C++
"MyApp.exe /Print"


If I execute above in command prompt, I want to display "Welcome to MyApp" in the same command prompt window.

I am able to get the given command line using CWinApp, but I cannot able to display my message in command prompt. How to do this?

What I have tried:

I tried using "WriteConsole" like below, it's not working.

C++
LPCWSTR lpBuff = L"Welcome to MyApp";
DWORD dwSize = 0;
AttachConsole(ATTACH_PARENT_PROCESS);
WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), lpBuff, lstrlen(lpBuff), &dwSize, NULL);


Is there any other way to do this?
Posted
Updated 14-Jan-20 6:18am

If cout fails you, take the extreme route. Do a stream write to stderr.

I haven't done the above since my DOS days but the typical rule was that both stdin and stdout (and thus, possibly cin and cout, for you) can be redirected but stderr could not.


 
Share this answer
 
Comments
Rick York 14-Jan-20 10:46am    
It actually can be redirected by using "2>" (in windows). It's probably best not to tell anyone that though.
It's easy and a good example can be found here

Visual C++ Enable Console - Stack Overflow[^]

Do a little search and you will find plenty of examples more.
 
Share this answer
 
I just tried your code and it works fine. You need to provide more details of your exact code and environment.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900