Re: Console Windows Forms hybrid

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

From: Mark Allison (marka_at_no.tinned.meat.mvps.org)
Date: 09/24/04


Date: Fri, 24 Sep 2004 23:31:06 +0100

Thanks for your help Ian, that works great, but it still seems messy to
me. I don't want to have to ship an extra file just to get around this.
It seems like a workaround to me, not an elegant solution.

Is this the only way?

--
Mark Allison, SQL Server MVP
http://www.markallison.co.uk
Looking for a SQL Server replication book?
http://www.nwsu.com/0974973602m.html
Ian Griffiths [C# MVP] wrote:
> 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.
> 
> 


Relevant Pages