-2

I have a Winform application that can be started normally by double-clicking on it and using as a foreground windows application, also it can be started from the command line with arguments

What I need is to allow the application Console.WriteLine function logs to be printed to the command line.

Currently, when I start the application from the command line, it just executes and the cursor goes to a new line as if it is ready to take new command. Any suggestions?

EDIT

I tried checking the answers under the following answer (which neither of them worked for me)

Mohammad Ali
  • 551
  • 7
  • 17
  • @Sinatr as they suggested when using `AllocConsole` and other ones it is only opening a new console window, but no logs are showing – Mohammad Ali Aug 08 '19 at 13:23
  • Sounds like you want to use `AttachConsole`. [This solution](https://stackoverflow.com/a/472432/1997232) looks interesting too. – Sinatr Aug 08 '19 at 13:58
  • @Sinatr so you didn't see my comment on his answer right? hehehe – Mohammad Ali Aug 08 '19 at 14:09
  • @Sinatr I found the answer: using the answer link you provided plus `kernel32.dll` `FreeConsole` function, I had to read the documentation of `kernel32.dll` in order to understand its functions and to know how to really use them – Mohammad Ali Aug 08 '19 at 14:45

1 Answers1

0

After searching and reading the documentation for kernel32.dll and making some tests

I reached to the following solution

I used this answer plus the function FreeConsole from kernel32.dll in order to make it work

First I made my WinForm application to run as a console application project properties > Application > Output type = Console Application

This will allow the project to run as a console application so when starting the application normally (double click exe or run from visual studio), it would start the windows application and would start a cmd window

To fix this I had to use the function FreeConsole from kernel32.dll in order to release the cmd window

The resulted working script I ended with is

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

    [STAThread]
    static void Main(string[] args) {
        if (args.Length == 0)
        {
            FreeConsole();
            MessageBox.Show("Application running by double-clicking on exe");

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new FrmMain());
        }else
        {
            Console.WriteLine("Application is running from cmd with arguments);
        }
    }
}

EDIT The above snippet will remove the console windows from the application, which will release the handler of the Console, meaning when calling Console.WriteLine or Console.Write functions it will throw an exception The handle is invalid

To avoid this problem, we don't need to release the console window completely, it is enough to hide it, as below using ShowWindow from kernel32.dll

In the program class

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

[DllImport("kernel32.dll")]
static extern IntPtr GetConsoleWindow();

[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

And in the Main function

//FreeConsole();
var handle = GetConsoleWindow();
ShowWindow(handle, 0);
Mohammad Ali
  • 551
  • 7
  • 17