Hi KH,
Sorry for the late response. I am in training all day and get back to
office till now.
I think Massimo's reply is based on the assumption that your application is
a control type application which launched a Winform from the console, so
the console window and the Winform are in the same application/process. In
this situation, just as Massimo pointed out, you may directly use
System.Console.WriteLine() in the Winform class code, the final output will
go to the original console window without any problem.
Another understanding of your problem is that your application is a pure
Winform application type. You first launched a console window from cmd.exe
and then type the path of your Winform application Exe from the cmd.exe, so
you want to write some output from your winform application to the cmd.exe
console window. In this scenario, the task is much more complex. This is
because the cmd.exe console window resides in cmd.exe process, which is a
different process from our new launched winform application. In Windows
design, process boundary is a very strong wall for security reason; it is
hard to break the processes boundary in Windows. Furthermore, since the
cmd.exe is not written by us, it is hard to tell cmd.exe to cooperate with
our Winform application for displaying output.
Forturnately, Microsoft has seen this common requirement and introduced a
new Win32 console API "AttachConsole" in WinXP. AttachConsole allows us to
attach the calling process to the console of the specified process. Also,
it introduced a special parameter ATTACH_PARENT_PROCESS which identifies
the parent process console. This really fits our need. However, .Net still
did not encapsulate this new Win32 API in .Net BCL, so we have to use
p/invoke to use AttachConsole in our Winform application. Please refer to
the second question in the C++ Q&A below for more context information
regarding this API:
http://msdn.microsoft.com/msdnmag/issues/04/02/CQA/
I still can not find a .Net article regarding this API, below is the
declaration of AttachConsole API in C#:
[DllImport("kernel32.dll")]
static extern bool AttachConsole( int dwProcessId);
const int ATTACH_PARENT_PROCESS=-1;
[STAThread]
static void Main( string[] args)
{
AttachConsole( ATTACH_PARENT_PROCESS);
}
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/subscriptions/managednewsgroups/default.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/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.