Question write in calling Console from windows form application

micky3248

New member
Joined
Feb 23, 2005
Messages
3
Programming Experience
10+
Hi,

I have a windows form application that can be called from the console using parameters so it doesn't open the forms. Now I want to write log messages to the console when something goes wrong. How do I do this? Further, if the application wasn't called from the console (so it works in graphical mode) I want to open a new console if an error happens and write the log messages into it. How do I detect if the app was called from the console or directly using the icon on the desktop?

Thx in advance.

Micky
 
Hello.

To attach a console to a WinForm Application you need an API-Call. Furthermore this console will not be blocked, means that the user can still execute commands in it while your program writes to it.

VB.NET:
        ''' <summary>API-Call: Attaches the console to the application</summary>
        ''' <param name="dwProcessId">Console-ProcessID (-1 for the console from which the program was called)</param>
        ''' <returns>Boolean Success</returns>
        ''' <remarks>API-Call: AttachConsole in kernel32.dll<br/>
        ''' This Function atttaches a console (by it's ProcessID) to the application,
        ''' which allows us to print (with Console.Print*) directly into that console.<br/>
        ''' The console will not be blocked...</remarks>
        Public Declare Function AttachConsole Lib "kernel32.dll" Alias "AttachConsole" (ByVal dwProcessId As Int32) As Boolean
        ''' <summary>API-Call: Releases the attached console (if any)</summary>
        ''' <returns>Boolean Success</returns>
        ''' <remarks>API-Call: FreeConsole in kernel32.dll<br/>
        ''' This function releases the attached console (if any).</remarks>
        Public Declare Function FreeConsole Lib "kernel32.dll" Alias "FreeConsole" () As Boolean

I don't know what happens if you call this if it was not called from a console.
But if you need to call a new one, I'd suggest that you use a new Process (have a look at the Process Object)

Bobby
 
Back
Top