Run console app in Windows Application

SlimyPizza

New member
Joined
Mar 18, 2009
Messages
3
Programming Experience
Beginner
I've got an existing console application (let's all it console.exe) that I want to run from a windows application. I want to do this to let the user more easily pick options and chose the file and path to submit to console.exe.

I'll want to program this as a VB Windows Application and upon choosing the right options and file path, run console.exe and have it appear in a console. The console needs to stay open so the user can examine it and read the results of having run the program.

How can I do this? Having the console window remain open after console.exe runs is my first problem. I use process.start("console.exe") and after console.exe runs, the console window quickly closes. How can I get it to remain open?

Thanks.
 
Try this:

VB.NET:
Public Class Form1

	Declare Function AllocConsole Lib "kernel32" () As Integer
	Declare Function FreeConsole Lib "kernel32" () As Integer

	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		If Convert.ToBoolean(AllocConsole()) Then
			Console.WriteLine("THIS IS FIRST LINE")
			Console.WriteLine("THIS IS SECOND LINE")
			Console.WriteLine("THIS IS THIRD LINE")
			Console.WriteLine("THIS IS FOURTH LINE")
			Console.WriteLine("THIS IS FIFTH LINE")
			Console.WriteLine("Press any key to Clear")
			Console.ReadKey()
			Console.Clear()
			Console.WriteLine("THE CONSOLE WAS CLEARED")
			Console.WriteLine("Press any key to close console")
			Console.ReadKey()
			FreeConsole()

			'System.Environment.Exit(1)		'ends entire application
		End If
	End Sub

End Class
 
Last edited:
Thanks. This does indeed create a console which "lingers" until a key press by the user. What I'm trying to do though is run a console application (call it console.exe) and have the console that application creates stay visible until a user presses a key.
If I add process.start("console.exe") to the code you provided, console.exe will run but not in a console window which will stay visible.
Any suggestions on how to run console.exe in a console window which I can cause to stay on screen?
Thanks...
 
Is there any particular reason you need to run the console app? I cant really think of anything you can do in a console app that you cant do in a windows app. Much easier to control another form within your application then trying to control a seperate application.

If your really just going for the look of a console app, you could create a windows form that looks like a console app window, drop a textbox on the form and set its dock property to fill the form, and set the background and font colors too.
 
I've got an existing console application that I'm trying to create a vb windows application to run it from. I didn't write the exisiting console application. I just want to make it easier to use.
 
You can redirect the input and output from the console from the process start params, hide the console window and use your own user interface instead - that would probably be neater.
 
ONE EXE for BOTH Console and WinForm Applications.

Is there any particular reason you need to run the console app? I cant really think of anything you can do in a console app that you cant do in a windows app. Much easier to control another form within your application then trying to control a seperate application.

If your really just going for the look of a console app, you could create a windows form that looks like a console app window, drop a textbox on the form and set its dock property to fill the form, and set the background and font colors too.

I know this thread is WAY OLD, but I did want to respond.

Tom, I have an application that I want to be able to run via a COMMAND LINE interface (batch file) - when certain command-line arguments are present; but ALSO to be a WinForms (GUI) Application - when other (or NO) command-line arguments are present.

Sure, I could duplicate the code in TWO different applications (distributing TWO .EXEs), or put the bulk of the code in a .DLL (that's shared between the two) - and distribute TWO .EXE AND a .DLL - but wouldn't it be "cleaner" to simply have ONE .EXE that's able to be run in a .BAT file (and return values to the console) - AND/OR to be able to run THE SAME APP as a GUI?

I OFTEN write applications that are BOTH GUI capable AND can be installed (and run) as Services (or can't you see a reason to do THAT either)?
 
I imagine that the console application is one that he hasn't written himself and doesn't have access to the source code for - he's just calling it.
 
Back
Top