Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

printf in a WinMain?

Status
Not open for further replies.

davidchardonnet

Programmer
Mar 21, 2001
167
0
0
FR
Hello,

I have a Win32 Application which I launch from the command line, and I want to display text in the MS-DOS Window from which I launched it.

I tried to put printf() in the WinMain function, but nothing appears, and I have the same behaviour with cout << &quot;text&quot;;

Does anybody have an idea?

Thank you

David
 
Try printing to cerr, which is standard error, instead of cout, or standard out.

Disclaimer:
Beware: Studies have shown that research causes cancer in lab rats.
 
Thank you for your help.

I've just tried it, it doesn't work...
Do you have another idea?
 
This is kind of a cheap... really cheap and not recommended work around for this. Keep in mind that there probably is some way to do this from within WinMain. However, I don't have my compiler here and can't test anything so I'm just giving an example of something to try.

Create 1 MS-DOS based program

DOSFile.cpp | DOSFile.exe

#include <iostream.h>
#include <process.h>

int main(int argv, char *argc[])
{

cout << &quot;What you want to say goes here&quot; << endl;
system(argc[1]);

return 0;

}


Save this file, compile and link it.
Create your Win32 program, compile and link it.
(WINFile.cpp | WINFile.exe)

At the command prompt, type this
copy /B DOSFile.exe+WINFile.exe Final.exe

Then run Final.


What this does is take DOSFile.exe and append WINFile.exe after it. The file DOSFile.exe when executed prints your text to the dos prompt, then takes the first argument (in this case it will ALWAYS be WINFile.exe because it's appended to the end of DOSFile.exe.) and runs it.

NOTE: Just a workaround, try not to do it this way if you don't have to... BUT it does make an interesting experiment if you're bored.

Best Regards,
Nathan Martini
 
function like printf, cout, cin, scanf doesn't work in Win32Applications. They work only in Win32ConsoleApplications. John Fill
1c.bmp


ivfmd@mail.md
 
On the contrary, printf works find in all Win32 applications. Every Win32 application has a console, which it can access by calling AllocConsole(). Printf() will output to the standard output handle.

The problem is that the standard output can't be set to another application's console. One technique for redirecting output / error / input is to create a two-way pipe and set the std handles to the pipe.

There's a crappy example of this on MSDN.

James (MS)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top