Windows console app no window

Is it possible to build a Console app that does not display a console Window when double-clicked?

I want to build a console app, that is normally run from the command line.

But, when it is double clicked from within Explorer (as opposed to being run from a cmd.exe prompt) then I’d like the program to NOT display a console window.

I want to avoid this:

EDIT I guess another way to ask it is, is it possible for a program to know how it was invoked — whether by double-click or by command line ?

I’m working in .NET, on Windows.

EDIT 2: From this Old New Thing blog post I learned some good stuff. Here’s what I know now.

In Windows, EXE files are marked as either GUI or non-GUI. With csc.exe, this is selected with /target:winexe or /target:exe . Before the first instruction in the process executes, the Windows kernel sets up the execution environment. At that moment, if the EXE is marked GUI, the kernel sets the stdin/stdout for the process to NULL, and if non-GUI (command-line) the kernel creates a console and sets the stdin/stdout for the process to that console.

When launching the process, if there is no stdin/stdout (== /target:winexe ), then the call immediately returns. So, launching a gui app from a cmd.exe, you will immediately get your cmd prompt back. If there is a stdin/stdout, and if run from cmd.exe, then the parent cmd.exe waits for process exit.

Читайте также:  Учебники по командной строке windows

The «immediate return» is important because if you code a GUI app to attach to its parent’s console, you will be able to do console.writeline, etc. But the cmd.exe prompt is active. The user can type new commands, start a new process, and so on. In other words, from a winexe, simply attaching to the parent console with AttachConsole(-1) will not «turn it into» a console app.

At this point I think the only way to allow an app to use the console if it is invoked from cmd.exe, and NOT use it if it is double-clicked, is to define the exe as a regular console exe ( /target:exe ), and hide the window on startup if appropriate. You still get a console window appearing briefly.

I still haven’t figured how to know whether it was launched from explorer or cmd.exe, but I’m getting closer..

ANSWERS

It is not possible to build a console app that does not display a console window.

It is possible to build a console app that hides its window very quickly, but not so quickly that it is as if the window never appears.

Now, to determine whether a console app was launched from explorer, some have suggested to look at the console it is running in
(from mgb’s answer, and KB article 99115) :

This tells you if the process was launched in its own console, but not whether it was explorer. A double click in explorer would do this, but also a Start.Process() from within an app would do the same thing.

If you want to treat those situations differently, use this to learn the name of the parent process:

Читайте также:  Fighters in the windows

To hide the window quickly after the process is launched, use this:

How do I show a console output/window in a forms application?

To get stuck in straight away, a very basic example:

If I compile this with default options (using csc at command line), as expected, it will compile to a console application. Also, because I imported System.Windows.Forms , it will also show a message box.

Now, if I use the option /target:winexe , which I think is the same as choosing Windows Application from within project options, as expected I will only see the Message Box and no console output.

(In fact, the moment it is launched from command line, I can issue the next command before the application has even completed).

So, my question is — I know that you can have «windows»/forms output from a console application, but is there anyway to show the console from a Windows application?

12 Answers 12

this one should work.

Perhaps this is over-simplistic.

Create a Windows Form project.

Then: Project Properties -> Application -> Output Type -> Console Application

Then can have Console and Forms running together, works for me

If you are not worrying about opening a console on-command, you can go into the properties for your project and change it to Console Application

This will still show your form as well as popping up a console window. You can’t close the console window, but it works as an excellent temporary logger for debugging.

Just remember to turn it back off before you deploy the program.

You can call AttachConsole using pinvoke to get a console window attached to a WinForms project: http://www.csharp411.com/console-output-from-winforms-application/

Читайте также:  Активатор windows 10 корпоративная 20h2

You may also want to consider Log4net ( http://logging.apache.org/log4net/index.html ) for configuring log output in different configurations.

This worked for me, to pipe the output to a file. Call the console with

cmd /c «C:\path\to\your\application.exe» > myfile.txt

Add this code to your application.

I found this code here: http://www.csharp411.com/console-output-from-winforms-application/ I thought is was worthy to post it here as well.

Create a Windows Forms Application, and change the output type to Console.

It will result in both a console and the form to open.

There are basically two things that can happen here.

Console output It is possible for a winforms program to attach itself to the console window that created it (or to a different console window, or indeed to a new console window if desired). Once attached to the console window Console.WriteLine() etc works as expected. One gotcha to this approach is that the program returns control to the console window immediately, and then carries on writing to it, so the user can also type away in the console window. You can use start with the /wait parameter to handle this I think.

Redirected console output This is when someone pipes the output from your program somewhere else, eg.

Оцените статью