65.9K
CodeProject is changing. Read more.
Home

Attaching a Console to a WinForms application

starIconstarIconstarIconstarIconstarIcon

5.00/5 (1 vote)

Jan 25, 2012

CPOL
viewsIcon

8630

VB version:Friend Declare Function AllocConsole Lib "kernel32.dll" () As BooleanFriend Declare Function FreeConsole Lib "kernel32.dll" () As BooleanSub main() Dim MainForm As New frmMain If Command = "" Then MsgBox1 = AddressOf MyMsgBox1 ' messagebox delegate sub call...

VB version:
Friend Declare Function AllocConsole Lib "kernel32.dll" () As Boolean
Friend Declare Function FreeConsole Lib "kernel32.dll" () As Boolean

Sub main()
    Dim MainForm As New frmMain
    If Command = "" Then
        MsgBox1 = AddressOf MyMsgBox1 ' messagebox delegate sub call a messagebox
        Application.Run(MainForm)
    Else
        AllocConsole()
        HadMessage = False
        Try
            MsgBox1 = AddressOf MyLog1 ' messagebox delegate sub log a message
            MainForm.Show()
            Application.DoEvents()
            MainForm.InjectParameters(Command)
            MainForm.llGo_LinkClicked(Nothing, Nothing)
            IO.File.AppendAllText("Out.txt", MainForm.tbResults.Text)
            MainForm.Close()
        Catch ex As Exception
            MyLog1(ex.ToString)
        End Try
        If HadMessage Then
            Console.Beep()
            Console.WriteLine("SLEEPING 20 SEC WHILE YOU READ THE MESSAGE")
            System.Threading.Thread.Sleep(20000)
        End If
        FreeConsole()
    End If
End Sub