Re: Console Windows Forms hybrid

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Ian Griffiths [C# MVP] (ian-interact-sw_at_nospam.nospam)
Date: 09/24/04


Date: Fri, 24 Sep 2004 15:29:36 +0100

The standard solution to this is to have two executables. This is exactly
what Visual Studio .NET does.

The technique relies on two facts:

 (1) the command line always prefers .COM executables to .EXEs
 (2) a .COM executable is allowed to be a proper PE executable (it doesn't
have to be an old-style DOS .COM app)

So you make the .COM the console application, and the .EXE the Windows
application. In the console application, if no parameters are passed on the
command line, just launch the EXE and then exit.

This is why when you just type "devenv" at the command prompt VS.NET
launches in the usual way (assuming VS.NET is on your path), but if you pass
in parameters, it runs as a console app.

Of course you probably don't want to ship two copies of your application for
this purpose...

In practice what you usually want to do is have one real copy of the
application. This is workable - I have a little shim launcher program whose
sole purpose is to allow what you're trying to do here - to allow a windows
app to run normally when run without parameters, but to run as a console app
when parameters are passed. You can use the same shim application for
everything - all you need to do is compile it and then rename. The source
code is here:

 http://www.interact-sw.co.uk/utilities/winappconsole/source/

Compile this into a file called "MyApplication.com". (Or whatever you want
to call it - so long as it ends in .com.) Then copy the real EXE into the
same directory. (You must build the EXE as a Windows Application, not a
Console application.)

The nice thing about this launcher program is that you don't really need to
do anything special in the main exe. You will be able to access the command
line switches in the normal way from either Environment.CommandLine, or via
the 'args' parameters passed to main. You don't really need to anything
special at all. If you run it as

  MyApplication

from the command line, it'll just run as a normal windows app, without
attaching to the console. If you run it as:

  MyApplication /some /parameters

then it will run as a console application.

And as for running the program from explorer, just double click on the EXE.

Hope that helps.

-- 
Ian Griffiths - http://www.interact-sw.co.uk/iangblog/
DevelopMentor - http://www.develop.com/
"Mark Allison" wrote:
> OK, I found out where - I did this to get it to work. In Visual Studio, 
> click Project, Properties; in the Common Properties section, General, 
> Output Type is Console Application.
>
> I now have another issue. If I launch the application from Windows 
> Explorer, a blank console window gets launched with it. How do I prevent 
> that?
>
> It works fine if launched from a console. Thanks for your help.
>
> Mark Allison wrote:
>> Thanks Morten, here's my Main method:
>>
>> [STAThread]
>> public static void Main(string[] args)
>> {
>>     if (args.Length == 0)
>>     {
>>         Application.Run(new frmFindDBWin());
>>     }
>>     else
>>     {
>>         Console.WriteLine ("Nothing happens here");
>>     }
>> }
>>
>> How do I compile it as a Console Application?
>> Morten Wennevik wrote:
>>
>>> Hi Mark,
>>>
>>> Could you show us your Main method?
>>> Are you compiling as Windows or Console application?  I believe you need 
>>> to build it as a Console Application to make it work 


Relevant Pages

  • Re: C# Console app with hidden window and user input
    ... hide its console window when its launched out on the field. ... console window at launch time. ... create a Windows Service or a simple Windows Forms app that happens not ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Windows XP Crash
    ... then I tried to go in from windows, but without the proper msgina no way. ... How can I find the CD on console? ... the command you were having a problem with earlier --is REN ... (Do you have an installation CD? ...
    (microsoft.public.windowsxp.general)
  • Re: Help! Cant delete a file in Windows XP!
    ... I go into DOS (or rather the command prompt feature of WXP). ... When logged into Windows, ... New -> Task menu to run cmd.exe to load a console shell. ... Into its Recovery Console mode? ...
    (microsoft.public.windowsxp.basics)
  • Re: Image viewing - a different approach
    ... it's a method for platform-independent "launch" ... as every Windows user has his desktop filled with. ... Such way we don't have to care about file type, launch command and other ...
    (comp.lang.tcl)
  • Re: run application
    ... >>>open any script file or pass any command line parameter. ... I list the source code messagebox.c and dialogboxindrect.c ... There may be some strange interaction between Windows, the console ...
    (microsoft.public.vc.language)