3

I am programming an application using the command line application output type to display debug information in the console while MOGRE is handling the actual window creation. I would like to hide the console when compiling the application for release. Not showing the console can easily be done by going into the project properties, the application tab and change the output type to windows application. When doing that only the MOGRE window will be shown.

While I believe it would be cleaner to create a windows application and attach a console to it when one wants that behavior I am still curious weather it is possible to do this programmatically.

That is, is there a way to programmatically determine that when compiling in debug mode the application compiles as command line application and when in release mode it compiles as windows application? And if so, how could it be done?

Edit: I am not asking how to attach a console to a windows forms application. I put the important part in italics out of hope that will make clearer what I want.

Sascha Hennig
  • 2,556
  • 1
  • 19
  • 22
  • it is a .NET wrpper for OGRE which is some sort of opensource 3D engine - see http://www.ogre3d.org/ and http://www.ogre3d.org/tikiwiki/MOGRE – Yahia Oct 19 '11 at 20:42
  • [MOGRE](http://www.ogre3d.org/tikiwiki/MOGRE+Introduction) (Managed OGRE) is an advanced .NET 2.0 wrapper for OGRE and [OGRE](http://www.ogre3d.org/about) (Object-Oriented Graphics Rendering Engine) is a scene-oriented, flexible 3D engine written in C++ designed to make it easier and more intuitive for developers to produce applications utilising hardware-accelerated 3D graphics. – Sascha Hennig Oct 19 '11 at 20:43
  • 1
    @SaschaHennig - In the future, please remember that not everyone reading a question knows every acronym you know... The way the question is phrased assumes MOGRE is widely used (which it is, in managed 3D graphics, but not outside). Adding appropriate tags helps with this. – Oded Oct 19 '11 at 20:46
  • See [Show Console in Windows Application?](http://stackoverflow.com/questions/472282/show-console-in-windows-application) – Chris Shouts Oct 19 '11 at 20:49
  • @Chris Shouts: that is not what I was asking. I want to know how to change the application output type depending on build type either via the project settings or programmatically through say flags or the like. – Sascha Hennig Oct 19 '11 at 21:02
  • @Oded - I was quite aware that not every one knows (M)OGRE. I just had the impression that it was not really important to the question and as I tend to make stuff more complex than need be I try to leave unimportant stuff out. In this case I should have left out the acronym MOGRE at all I guess. – Sascha Hennig Oct 19 '11 at 21:33

2 Answers2

8

You can achieve this if you edit the .csproj manually:

  • Right click on the project node in Solution Explorer
  • Select "Unload Project"
  • Right click on the project node in Solution Explorer
  • Select "Edit MyApp.csproj"

Move the <OutputType ../> property group Xml element from the <PropertyGroup .../> Xml element without Condition to the property group with conditions corresponding to build configuration / platform.

Before:

<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    ...
    <OutputType>Exe</OutputType>
    ...
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    ...
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    ...
  </PropertyGroup>

After:

<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    ...
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    ...
    <OutputType>Exe</OutputType>
    ...
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    ...
    <OutputType>WinExe</OutputType>
    ...
  </PropertyGroup>

And finish:

  • Right click on the project node in Solution Explorer
  • Select "Reload Project"

Here is a proof example:

class Program
{
    public static void Main(string[] args)
    {
#if DEBUG
        Console.WriteLine("test");
#else
        Application.Run(new Form1());
#endif
    }
}

It works, but I don't think this is officially supported, so use at your own risk :-)

Simon Mourier
  • 132,049
  • 21
  • 248
  • 298
5

Not very sure, if there is pure .NET way to achieve what you want to do, but there is a way to achieve this by using Windows APIs:

[DllImport("kernel32.dll", SetLastError = true)]
static extern bool AllocConsole();

[DllImport("kernel32.dll", SetLastError = true)]
static extern bool FreeConsole();

[DllImport("kernel32", SetLastError = true)]
static extern bool AttachConsole(int dwProcessId);

Here is code sample that could be helpful to you: Attach Console to Windows Forms application

Tigran
  • 61,654
  • 8
  • 86
  • 123
  • That does not really answer the question. The point is not how to attach/show/hide a console window when compiling a windows forms application, but specifically how to set the output type of a project programmatically. Or possibly how to add/change csc-command line parameters. I.e. "csc /target:exe" would compile the application as console application. But I do not want to compile the application from the command line either. Is there a way to specify those options in the source/application properties? – Sascha Hennig Oct 19 '11 at 21:00
  • @SaschaHennig: You can handle it via arguments of your executable. If application is launched with some specified argument, it appears like a Console, if not the default "view" is Window. So what you need is to call your EXE with appropriate argument, or without it. No conditional compilation needed. – Tigran Oct 19 '11 at 21:08
  • 1
    Well, your answer certainly would be better practice than conditional compilation and it would rather do it your way in a production environment. I was just curious if and how it would be possible the way I mentioned. – Sascha Hennig Oct 19 '11 at 21:30