1

I am developing an application that writes some exception messages to the Console if they are of no use to the user. As a result if someone runs the executable by simply double clicking it, they will never see these messages.

In Visual Studio these messages show up in the output window, but in my case I am testing my application on a machine that does not a Visual Studio installation, though I still want to see if any of these messages appear.

In the past I have simply ran the executable from the command prompt and it acted as the output window in Visual Studio. Though for some reason my application, when ran from the command prompt, simple "returns" and does not show any messages.

For example I might start it like so, and it instantly returns

D:\>MyApp.exe

D:\>

I am not sure if there is a specific switch I should use (I have tried /K to no avail) when I run it, or if there is something about my application that causes it to return.

Any ideas on how I might run it via the command line so I can see the messages?

For reference here is my applications Program.cs

static class Program
{
    static Mutex mutex = new Mutex(true, "{12345678-1234-1234-1234-123456789012}");

    [STAThread]
    static void Main()
    {
        if (mutex.WaitOne(TimeSpan.Zero, true))
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            SplashScreen.ShowSplashScreen();
            Application.Run(MainForm.Instance);

            mutex.ReleaseMutex();
        }
        else
            NativeMethods.PostMessage((IntPtr)NativeMethods.HWND_BROADCAST, NativeMethods.WM_JTTMAINWINDOW, IntPtr.Zero, IntPtr.Zero);
    }
}
KDecker
  • 6,928
  • 8
  • 40
  • 81
  • Could you look if the following helps http://stackoverflow.com/questions/14616837/how-to-redirect-output-from-exe-after-it-has-terminated this will allow you to write it to a file. – Reinard Oct 07 '15 at 18:27
  • 1
    Have you looked at using a logging library that allows console listeners to be defined, like log4net? You can also attach the console window to your application using p/invoke. Are you using `Console.WriteXXX` or `Debug.WriteXXX` calls? There is also a small issue with the way you are using the mutex to create a single instance app, you need to catch unhandled exceptions and release there. – Ron Beyer Oct 07 '15 at 18:29
  • This program doesn't write anything to the console. I'd add a `Console.WriteLine("something");` before the `if`-statement for testing purpose. – venerik Oct 07 '15 at 18:29
  • @venerik I actually just did that. Made it the very first statement, and it was not shown. // @RonBeyer I am using `Console.Write...` // @SirTroll this does write the output to a file. That is a good fix for the meantime, though it would be nice to be able to see what actions cause what messages at a given time. – KDecker Oct 07 '15 at 18:32
  • 2
    Your program is being built as a Windows application (the `/SUBSYSTEM:Windows` linker option). If you want it to output to the console, you have to build it as a console application (the `/SUBSYSTEM:Console` option). However, this means that if you double-click the application a console will be automatically opened, which may be undesirable. There isn't any way for the same executable to do both at once; the usual solution is to have two executables, a GUI version and a console version. – Harry Johnston Oct 07 '15 at 20:12
  • 1
    (Actually, it is *possible* for a Windows application to attach to the parent console and write to it, but because the console won't wait for the application this is usually not a good idea - the output will be intermixed with the output of `cmd.exe` and/or whatever other program is running in the same console.) – Harry Johnston Oct 07 '15 at 20:13
  • @HarryJohnston Hmm, opening a console with the application doesn't seem too harmful for its use, I think I will look into that. // Could you give me some more information about your second comment? Specifically more about what will be intermixed in the output? I don't understand where this other output would come from. // Also, you could do that in an answer and I would accept it. – KDecker Oct 08 '15 at 12:16
  • If you attach to the parent console, any output from `cmd.exe` (including in particular the command-line prompt) will be intermixed with your output. So will the output from any command or program the user might run: for example, if the user asks for a directory listing, the output from your program, or fragments of it, might appear in the middle of that listing at random. – Harry Johnston Oct 08 '15 at 19:31

1 Answers1

1

To allow your program to output to the console, build it as a console application.

In Visual Studio, the relevant linker option is /SUBSYSTEM:Console ; or, when creating a new project, choose the "console application" template which will set the linker option automatically.

This will also mean that if you run the program from the command prompt, the command prompt will wait for the program to exit. Also, if you double-click the program rather than running it from the command prompt, a console window will automatically be created.

(There is no way to make the command prompt wait for a program but prevent the program from creating a console window if double-clicked. See this question and its answers for more information.)

Community
  • 1
  • 1
Harry Johnston
  • 35,639
  • 6
  • 68
  • 158