Monthly archives: May 2007

Timothy

In VB.Net, sending output to the console from a Windows "Forms" application

Ever launched a forms app from the command line where you happily outputted debug code to the console from within Visual Studio, only to discover nothing is output to the command line like you expect?  Me too.  Really, it makes sense, though.  Forms apps in .Net run on their own thread and therefore detach from the initiating command line right after you hit enter.  Well how do you see all that pretty console text then?  Disable application framework **shiver**?  No...

You can do it using your old friend P/Invoke to access unmanaged code in kernel32.dll.

Here's the P/Invoke declarations:

Declare Function AttachConsole Lib "kernel32.dll" (ByVal dwProcessId As Int32) As Boolean
Declare Function FreeConsole Lib "kernel32.dll" () As Boolean

These functions allow you to attach and detach from an existing console session.  So how do you find out which console launched your WinForms app?  You don't need to.  If you specify "-1" as the process ID, it will automatically attach to the console that started the application.  Here's how you do it.

AttachConsole(-1)
System.Console.Writeline("I am writing to the console!")
FreeConsole()

Really, you probably will only attach once and free once in your app, but that's just example code up there.

One caveat: Since your app is running in its own thread, it will write to the console asynchronously.  This means that the user will receive a command prompt for the next command before your app writes anything to the console.  The console will not block waiting for your app to exit.  Be sure not to write things at strange times that will confuse the user about why strange text is appearing on the screen!

Source of information: http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=983493&SiteID=1

Timothy

Screenshots of RD Tabs 2.0 Alpha

Check out the forums!  I posted screenshots of RD Tabs 2.0.  Let me know what you think in the forums and don't forget to vote!  :-)

http://forums.avianwaves.com/Default.aspx?g=posts&t=21

Blog

Search Posts

Recent Comments

  1. Re: DPM 2016 + SQL 2016 and "An unexpected error occurred during the installation" ID: 4387
    ptbNPA: That should have been *ID 810*, not 820

  2. Re: DPM 2016 + SQL 2016 and "An unexpected error occurred during the installation" ID: 4387
    ptbNPA: For anyone else coming across this in the future and have an ID 820 error: For some strange reason...

  3. Re: DPM 2016 + SQL 2016 and "An unexpected error occurred during the installation" ID: 4387
    Timothy: Sorry, I don't have any other insight. I'm sure you ran into all the same articles I did about the error...

  4. Re: DPM 2016 + SQL 2016 and "An unexpected error occurred during the installation" ID: 4387
    ptbNPA: FYI, after posting the issue on technet + windows-noob.com with no replies I decided to do a fresh Windows...

  5. Re: DPM 2016 + SQL 2016 and "An unexpected error occurred during the installation" ID: 4387
    ptbNPA: Hi and thanks for your post. I originally installed SQL 2016 with SP1 on a Windows server 2016 and got...

  6. Re: DPM 2016 + SQL 2016 and "An unexpected error occurred during the installation" ID: 4387
    Timothy: Update Rollup 2.

  7. Re: DPM 2016 + SQL 2016 and "An unexpected error occurred during the installation" ID: 4387
    Catwiesl: You write "Microsoft System Center Data Protection Manager (DPM) officially supports SQL 2016 with U...

  8. Re: PowerShell: Quickly Finding the Oldest and Newest Files in a Folder
    Neki: i would like to compare a local file with a remote file and download the remote file if its newer than...

  9. Re: RD Tabs: Tabbed Windows Remote Desktops
    lukas: Fantastic software though I am having issues saving passwords. As soon as I kill the program the setting...

  10. Re: Easy Way to Change Permissions on the Windows Server Scheduled Tasks Folder (C:\Windows\Tasks)
    George: From another server, using an admin account, I opened \\SERVER\c$\windows in Windows Explorer. Tasks...

Archive

Tag Cloud