tags:

views:

1007

answers:

4

How do I create a windows application that does the following:

  • it's a regular GUI app when invoked with no command line arguments
  • specifying the optional "--help" command line argument causes the app to write usage text to stdout then terminate
  • it must be a single executable. No cheating by making a console app exec a 2nd executable.
  • assume the main application code is written in C/C++
  • bonus points if no GUI window is created when "--help" is specified. (i.e., no flicker from a short-lived window)

In my experience the standard visual studio template for console app has no GUI capability, and the normal win32 template does not send its stdout to the parent cmd shell.

A: 

Its pretty simple. Within the Main function of the Program class, the first thing you do is check your args. If you see help, then dump the help text to the console and Close() you application. Otherwise, let the main window of your app get spun up.

edit wait, I lied. You can't write to the command window from a forms app. You can do it, but the console window would remain open while the application was running. All you have to do is go to the Properties editor for you windows forms app, select the Application tab, and change the Output type to Console. Then the following code works.

    static void Main()
 {
  if (Environment.GetCommandLineArgs().Contains("--help", StringComparer.CurrentCultureIgnoreCase))
  {
   Console.WriteLine("Help text goes here lol");
   return;
  }
  Application.EnableVisualStyles();
  Application.SetCompatibleTextRenderingDefault(false);
  Application.Run(new Form1());
 }
Will
+1  A: 

@Will: But then, the console would remain attached to the application (it would sit there, waiting for the application to complete). EDIT: Oh and OP is asking about C++, not C#.

@JeffJ: This question is nicely answered here (at least, how it's implemented in devenv and ildasm).

Also, you might want to try to play with the AttachConsole and FreeConsole functions.

Paulius Maruška
This answer was accepted because of information in the external link. However, I'm tempted to unaccept this answer and accept Hugh Allen's answer instead.
JeffJ
+4  A: 

Microsoft designed console and GUI apps to be mutually exclusive. This bit of short-sightedness means that there is no perfect solution. The most popular approach is to have two executables (eg. cscript / wscript, java / javaw, devenv.com / devenv.exe etc) however you've indicated that you consider this "cheating".

You've got two options - to make a "console executable" or a "gui executable", and then use code to try to provide the other behaviour.

  • GUI executable:

cmd.exe will assume that your program does no console I/O so won't wait for it to terminate before continuing, which in interactive mode (ie not a batch) means displaying the next ("C:\>") prompt and reading from the keyboard. So even if you use AttachConsole your output will be mixed with cmd's output, and the situation gets worse if you try to do input. This is basically a non-starter.

  • Console executable:

Contrary to belief, there is nothing to stop a console executable from displaying a GUI, but there are two problems.

The first is that if you run it from the command line with no arguments (so you want the GUI), cmd will still wait for it to terminate before continuing, so that particular console will be unusable for the duration. This can be overcome by launching a second process of the same executable (do you consider this cheating?), passing the DETACHED_PROCESS flag to CreateProcess() and immediately exiting. The new process can then detect that it has no console and display the GUI.

Here's C code to illustrate this approach:

#include <stdio.h>
#include <windows.h>

int main(int argc, char *argv[])
{
 if (GetStdHandle(STD_OUTPUT_HANDLE) == 0) // no console, we must be the child process
 {
  MessageBox(0, "Hello GUI world!", "", 0);
 }
 else if (argc > 1) // we have command line args
 {
  printf("Hello console world!\n");
 }
 else // no command line args but a console - launch child process
 {
  DWORD dwCreationFlags = CREATE_DEFAULT_ERROR_MODE | DETACHED_PROCESS;
  STARTUPINFO startinfo;
  PROCESS_INFORMATION procinfo;
  ZeroMemory(&startinfo, sizeof(startinfo));
  startinfo.cb = sizeof(startinfo);
  if (!CreateProcess(NULL, argv[0], NULL, NULL, FALSE, dwCreationFlags, NULL, NULL, &startinfo, &procinfo))
   MessageBox(0, "CreateProcess() failed :(", "", 0);
 }
 exit(0);
}

I compiled it with cygwin's gcc - YMMV with MSVC.

The second problem is that when run from Explorer, your program will for a split second display a console window. There's no programmatic way around this because the console is created by Windows when the app is launched, before it starts executing. The only thing you can do is, in your installer, make the shortcut to your program with a "show command" of SW_HIDE (ie. 0). This will only affect the console unless you deliberately honour the wShowWindow field of STARTUPINFO in your program, so don't do that.

I've tested this by hacking cygwin's "mkshortcut.exe". How you accomplish it in your install program of choice is up to you.

The user can still of course run your program by finding the executable in Explorer and double-clicking it, bypassing the console-hiding shortcut and seeing the brief black flash of a console window. There's nothing you can do about it.

Hugh Allen
This is a great answer. Thanks.Forking a 2nd process is not cheating.
JeffJ
A: 

I know my answer is coming in late, but I think the preferred technique for the situation here is the ".com" and ".exe" method.

This may be considered "cheating" by your definition of two executables, but it requires very little change on the programmers part and can be done one and forgot about. Also this solution does not have the disadvantages of Hugh's solution where you have a console windows displayed for a split second.

In windows from the command line, if you run a program and don't specify an extension, the order of precedence in locating the executable will prefer a .com over a .exe.

Then you can use tricks to have that ".com" be a proxy for the stdin/stdout/stderr and launch the same-named .exe file. This give the behavior of allowing the program to preform in a command line mode when called form a console (potentially only when certain command line args are detected) while still being able to launch as a GUI application free of a console.

There are various articles describing this like "How to make an application as both GUI and Console application?" (see references in link below).

I hosted a project called dualsubsystem on google code that updates an old codeguru solution of this technique and provides the source code and working example binaries.

I hope that is helpful!

gabeiscoding