Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / February 2007

Tip: Looking for answers? Try searching our database.

WinForm app write to command line

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
kh - 26 Jan 2007 09:48 GMT
If my winform app is started with command line arguments i need to be able to
execute some code and then write back to the command line from where the app
was started. is this possible?

thanks

kh
Massimo - 26 Jan 2007 18:00 GMT
> If my winform app is started with command line arguments i need to be able
> to
> execute some code and then write back to the command line from where the
> app
> was started. is this possible?

I don't see any reason why you shouldn't be able to use System.Console in
your Main() method...

Massimo
Jeffrey Tan[MSFT] - 29 Jan 2007 10:25 GMT
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.
PhilW - 01 Feb 2007 17:52 GMT
I'm using AttachConsole to send a GUI applications output to the console, if
started from the console with a command line argument.

  if(AttachConsole((DWORD)-1)){

     freopen("CON", "w", stdout);
     freopen("CON", "w", stderr);
     freopen("CONIN$", "r", stdin);

     printf("usage: myapp blah\n");

     FreeConsole();

     // simulate return pressing
     INPUT input;
     memset(&input,0,sizeof(input));
     input.type=INPUT_KEYBOARD;
     input.ki.wVk=VK_RETURN;
     SendInput(1,&input,sizeof(input));
     input.ki.dwFlags=KEYEVENTF_KEYUP;
     SendInput(1,&input,sizeof(input));
     }

This all works great EXCEPT that if I try to re-direct this output in the
console window:  myapp > output.txt, the file is created, but the output is
still sent to the conosle leaving the file empty.

Any ideas?

> Hi KH,
>
[quoted text clipped - 68 lines]
> ==================================================
> This posting is provided "AS IS" with no warranties, and confers no rights.
Jeffrey Tan[MSFT] - 02 Feb 2007 08:23 GMT
Hi PhilW,

Are you the same person as KH?

Yes, I can reproduce this behavior. This is because the AttachConsole API
will always attach the standard output of the Winform application to the
parent console output instead of the redirected file handle. This is
totally system programming now, I do not think .Net has not build-in
support or simple workaround for it.

Thanks.

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.
PhilW - 08 Feb 2007 21:48 GMT
Hi Jeffery,

No, I'm not KH.
But it's exactly the answer I was looking for.  (Not the answer I wanted,
but the response is what counts.)

Thanks a lot!

Phil

> Hi PhilW,
>
[quoted text clipped - 29 lines]
> ==================================================
> This posting is provided "AS IS" with no warranties, and confers no rights.
Jeffrey Tan[MSFT] - 31 Jan 2007 02:46 GMT
Hi KH,

Have you reviewed my reply to you? Does it make sense to you? If you still
need any help or have any concern, please feel free to tell me, thanks.

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.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.