standalone application to work dually as Windows and Console applications

U

Uriel Mondrowicz

Hi all,

I'm trying to build in C# a simple tool that should be used in dual mode:
1. Windows Application when called without any arguments (or simply
double-clicked)
2. Console Application when called from a command line with some arguments

I could create a parent console application that runs from a command line
(and sends its output to the stdout console) when it is called with some
arguments.
If it detects that no arguments were entered (args.Length == 0) it opens a
windows form using the frmGUI.ShowDialog() method.

My main problem is that when clicking on the tool executable to run it as a
win GUI, it will open a black console window for a second, dissapear, and
then show the GUI form.
This is very annoying, and I'm sure there is a way to resolve this black
screen issue.
I tried using FreeConsole() method in my entry point just before checking
the args but it still shows the black console for a second.

I have seen a C++ solution for that kind of implementation here:
http://www.codeguru.com/Cpp/W-D/console/redirection/article.php/c3955 but it
requires using an edditional .com file which is not good for me as it should
be a single file application.

Do you have an idea how can I make it work?

Thanks in advance,
Uriel Mondrowicz.
 
A

Adam Hearn

Slight adjustment -> Try setting your project output as a Windows
application and then add support in for the command line. For example:
before the Windows Form is created using (Application.Run( new instance of
Form) you do your command line arguments checks. Don't know what your
application supports as fas as arguments go, but you may be able to provide
sufficient logic!

Best of luck!
 
U

Uriel Mondrowicz

Hi Adam,

Thanks for your reply.
In the console mode option, I need to use the standard input and output
(console window) to interact with the user.
If I compile it as a Windows application, there is no way to access the
parent console window for my printouts, correct?
Did you make this kind of dual application before?
Please advise.

Thanks again,
Uriel.
 
A

Adam Hearn

If you need input & output then that's no good! However, you can hide the
console window for the GUI invocation (i.e. no arguments) by calling the
FreeConsole API before you construct the window. Here's an example:

[System.Runtime.InteropServices.DllImport("kernel32.dll")]
private static extern bool FreeConsole();

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
if( args.Length == 0 )
{
// This seems to get rid of the console window before it's show
FreeConsole();

// Themed controls
Application.EnableVisualStyles();
Application.DoEvents();

// Run the GUI version of the application
Application.Run(new CGUIVersion());
}
else
{
// Run the console version of the application
CConsoleVersion.DoOp( args );
}

Hope that helps!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top