views:

35

answers:

4

I have a project in Visual Studio 2008. I want to have two different ways of running it - either as a regular Windows application (so it gets a window and I can do graphics stuff) or as a console application (so that it doesn't have a window and doesn't do graphics). Is there a way to set it up so that I can use a command line option or something to switch between these two options when I run it? Is there a way to set up two different configurations in the same Visual Studio proejct so I can build either one? Or do I need to create two completely separate projects, one for the window application and one for the console application.

A: 

Yes, you can use Configuration Manager to create new configurations. The project settings can be (is) different per configuration. So you can create two configurations, and in project settings set Output Type to be "Windows Application" or "Console Application".

This is how you do it: In the Build Type drop down (The one with Debug and Release options by default), select "Configuration Manager". Add a new configuration and call it "Debug (Console)" - with this new configuration selected, go to project properties and set Output Type to Console Application. Repeat to add a Windows Forms configuration.

driis
A: 

just like you have the default Debug and Release configurations, you can create your own. Right-click the project, select 'Configuration Manager', click the 'Active Solution Configuration' dropdown box and select 'New..'. Enter names like 'CmdLine Debug' and 'Windowed Release' etc, delete the other configurations. Now right-click the project, select Properties, and adjust the needed settings for the appropriate configuration.

Tp to make your life easier: put properties that are common to all configuration in property sheets, and use those sheets everywhere. This way, if you want to change an opion that applies to all configurations, you'll only have to do it once.

stijn
No, unfortunately it doesn't work that way. The output type, i.e. whether you create a Console or Window application is a project setting not affected by the active configuration.
0xA3
huh? I thought properties->linker->system->subsystem specifies this?
stijn
Sorry, as there was no programming language given I was assuming .NET projects. It might be different for C++ but I doubt it is, basically because a Windows app will have to have a WinMain and a message loop whereas a Console app doesn't.
0xA3
just checked, if I change a console project to use /SUBSYSTEM:WINDOWS, the linker complains it can't find _WinMain ..
stijn
... which you could of course handle using some amount of conditional preprocessor directives ;-)
0xA3
+1  A: 

You might want to consider splitting your project into three.

  1. An assembly/dll (depending on your language) that does the work.
  2. A console application.
  3. A Windows application.

The latter two are just two different interfaces on the assembly/dll that does all the work.

ChrisF
+1  A: 

The recommended (non-hacky) way to do this would be to place all your logic in a class library and then create two separate projects for a Console and a Window application. Then add the class library as a reference to these projects as suggested by Chris.

You cannot have a single application that both acts as a Console and a Window application. The reason basically is that Console and Window application are different on the binary level with different flags in the PE header.

There are some hacks though that allow you to fake such a behaviour using Windows API functions (AttachConsole and CreateConsole). For details see

C#: Is it possible to have a single application behave as Console or Windows application depending on switches?

But be aware that there might be certain differences in behaviour to regular Console or Window applications.

0xA3