Click here to Skip to main content
14,422,431 members
Rate this:
Please Sign up or sign in to vote.
I have a win32 project, I am calling my exe using following command in command prompt.
"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.

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 5 days ago
Rate this:
Please Sign up or sign in to vote.

Solution 2

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.


   
Comments
Rick York 5 days ago
   
It actually can be redirected by using "2>" (in windows). It's probably best not to tell anyone that though.
Rate this:
Please Sign up or sign in to vote.

Solution 3

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.
   
Rate this:
Please Sign up or sign in to vote.

Solution 4

I just tried your code and it works fine. You need to provide more details of your exact code and environment.
   

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100