Hi Peter,
Yes, I can reproduce out this behavior.
Normally, "console" applications (ones that begin with main/wmain) are
executed by cmd ¡°synchronously¡±, while ¡°windows¡± applications (ones
that begin with WinMain/wWinMain) are executed ¡°asynchronously¡±.
This is independent of whether said application writes to any consoles or
not.
So, your test program is a ¡°windows¡± app, so cmd.exe starts it up and
then immediately returns control to the user (printing a prompt for the
next command). The user can even use that prompt before test does
anything. (Put in a sleep statement in test.exe to observe this.) That¡¯s
because most ¡°windows¡± program launch a new GUI window and interact with
the user that way.
When your app now prints to the console, it just prints wherever the cursor
is (which means it starts at the end of the command prompt, and also those
messages would be intermixed with any commands the user types and any
results from those other commands), and when it gets done printing, cmd.exe
doesn¡¯t know that the appearance of its prompt has been trashed and needs
to be reprinted. [And if you were halfway through typing that command,
that command fragment is still there to be continued, even though you¡¯ve
spewed a whole bunch of stuff to the console.]
Now, the standard way to deal with this is:
1. Use a console app. Note that the cmd.exe prompt won¡¯t be available
again until your program exits.
2. Use a separate console instead of the one that launched your program.
3. Use windows GUI to show error messages.
4. Use a hybrid approach if this makes sense ¨C a small console app that
parses the cmd line and if it decides it wants to act like a console app,
it prints those messages, but otherwise spawns a GUI app (and exits) to
return the cmd prompt to the user. The GUI app then communicates to the
user via GUI windows, etc.
Regarding #4, if you do this, name the console app MyApplication.com and
the GUI MyApplication.exe. Cmd.exe will launch the .com file first, the
shell will launch the .exe first and everybody is happy:
MyApplication.exe ¨C This is a Windows Application, and performs no output
to the console.
MyApplication.com ¨C This is a Console Application, it calls
MyApplication.exe and returns output from the Windows Application to the
console via some kind of IPC.
See devenv.com/devenv.exe in Visual Studio as an example:
C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\devenv /?
C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\devenv.com /?
C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\devenv.exe /?
Hope this helps.
Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.