1

I have a Windows Form application but it sometimes behaves as a formless (console application) when parameters are passed by command line. If no parameters are passed, then it behaves as a normal Windows Forms.

When it behaves as a console application, I attach to console to output messages using Console.WriteLine().

[DllImport("kernel32.dll")]
static extern bool AttachConsole(int input);

static void Main(string[] args)
{            
    if (args.Length == 0)
    {   
        Application.Run(new MyForm());
    }
    else 
    {
        // case args.Length > 0
        AttachConsole(-1);
        Console.WriteLine("Start my formless app...");
        new FormLessApp().Start();
    }
}

But I am not sure If I need to deatach from console when formless aplication finishes. Is it necessary? If so which DLL method do I have to use?

Willy
  • 9,848
  • 22
  • 141
  • 284
  • 2
    Not a direct answer to your question, but you will probably better off with creating a separate console app and a winforms app that both reference the library which holds your business logic (i.e. all code that is not UI related). – Flater May 17 '17 at 08:34
  • 1
    Are you sure you need the `AttachConsole` call at all? If you are sure you need it, why aren't you reading its [documentation](https://msdn.microsoft.com/en-us/library/windows/desktop/ms681952(v=vs.85).aspx)? – Damien_The_Unbeliever May 17 '17 at 08:36
  • To reinforce Flater's point, this is how e.g. Visual Studio deals with this sort of situation. Rather than loading all of the bulky IDE exe when it's being called for console purposes, it instead runs as a separate app (the trick there being that `devenv.com` is the default to run when you just type `devenv` at the console and, if it's not passed any parameters, it just launches `devenv.exe` and then exits) – Damien_The_Unbeliever May 17 '17 at 08:41
  • 1
    It is not necessary, it automatically detaches when your program terminates. You ought to be a lot more concerned about a much bigger problem, this does not work well when your program is started with cmd.exe, as it normally will. Cmd.exe does not know that it needs to *wait* until your program ends. It immediately displays the prompt again and asks for input. This interferes badly with your use of the console. The user has to run your program with `start /wait` to prevent is problem, but of course they never do. – Hans Passant May 17 '17 at 08:49

1 Answers1

0

FreeConsole from Kernel32.dll. You can see the documentation here.

Also according to the documentation:

A process can use the FreeConsole function to detach itself from its console. If other processes share the console, the console is not destroyed, but the process that called FreeConsole cannot refer to it. A console is closed when the last process attached to it terminates or calls FreeConsole. After a process calls FreeConsole, it can call the AllocConsole function to create a new console or AttachConsole to attach to another console.

Therefore my guess is, if you are planning to detach from the console when terminating the FormLessApp() process, you don't need to do it manually, it will be destroyed automatically.

Rojan Gh.
  • 1,062
  • 1
  • 9
  • 32