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
    Brian: Thank you so much Edward! :-)

  2. Re: DPM 2016 + SQL 2016 and "An unexpected error occurred during the installation" ID: 4387
    Tom: Thank you Edward! After beating my head against a wall for days, tried your suggestion out and lo and...

  3. Re: DPM 2016 + SQL 2016 and "An unexpected error occurred during the installation" ID: 4387
    Mike: DPM 2016 setup will fail if you have SQL Server Management Studio (SSMS) V17.x installed. Re-Install...

  4. Re: DPM 2016 + SQL 2016 and "An unexpected error occurred during the installation" ID: 4387
    Rob: Edward, thanks man! you were a lifesaver. My scenario was Win Server 2016 from scratch, SQL 2016 (N...

  5. Re: DPM 2016 + SQL 2016 and "An unexpected error occurred during the installation" ID: 4387
    Edward: It also crashes with the 4387 error if you have the SQL Management Studio 17 tools installed. Installing...

  6. Re: DPM 2016 + SQL 2016 and "An unexpected error occurred during the installation" ID: 4387
    Ram: Hi - I followed richsmif instruction and was able to successfully install DPM 2016 on SQL 2016. Completed...

  7. Re: DPM 2016 + SQL 2016 and "An unexpected error occurred during the installation" ID: 4387
    Neighborgeek: Thanks for the post, this is exactly the issue I am running into. I'm disappointed to see that you didn...

  8. Re: DPM 2016 + SQL 2016 and "An unexpected error occurred during the installation" ID: 4387
    richsmif: I have DPM 16 working with SQL 16. Install SQL 16 first, don't touch, install DPM 16 , upgrade to ...

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

  10. 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...

Archive

Tag Cloud