There is an application that (initially unknown) neither the form nor the console may be initially as both may already be present (both the console and the form).

How to determine the absence of the console and create it if it is not?

    2 answers 2

    You can create a console only if it is not there, so you can simply call AllocConsole without any conditions. Proof :

    Remarks

    AllocConsole function fails if there is a console.

    In general, you can use GetConsoleWindow, for example, to check the console

    • I will add only the FreeConsole function, which deletes the created console window. And yet, one very important point. If you expect input in the console (for example, using readln), then the main thread will be paused. Therefore, do not be surprised that while waiting for input, the form may not be updated. - KoVadim

    In either case, this is either a console or a GUI. It is determined by the compiler at the build stage. See the structure of PE files.

    Technically easier to hide the console, run a cycle of forms. In Delphi, this is quite simple to implement.

    • Not suitable .. Let's do this .. there is a command to install the handle of the default output ... the question then is how to create a window of the same class as the console window - Vladimir Klykov
    • CnPack has CnVCL among other things, there is a component CnConsole (something like that), it shows the console implementation if you do not want to compile it. - Man_Borscha