Pages

Friday 5 July 2024

How to Run a Silent Console Application in Delphi

 Author: PETER ASCHBACHER (PA-SOFT)

Sometimes, it's necessary to hide the console window of a CONSOLE APPLICATION entirely to avoid distracting the user with an unnecessary command prompt. Fortunately, Delphi provides a straightforward way to achieve this. Below, we'll walk through the steps to create a console application that runs silently without displaying the console window.

program OpenTaskbarSettings;


// The $APPTYPE GUI directive ensures that the application is treated as a GUI application,

// which prevents the console window from being shown.

{$APPTYPE GUI}


uses

  System.SysUtils,

  Winapi.Windows,

  Winapi.ShellAPI;


// Procedure to hide the console window if it exists

procedure HideConsole;

var

  hConsole: HWND;

begin

  // Get the handle of the console window

  hConsole := Winapi.Windows.GetConsoleWindow;

  // If the console window handle is valid, hide the console window

  if hConsole <> 0 then

    ShowWindow(hConsole, SW_HIDE);

end;


// Procedure to open the Taskbar Settings window

procedure DoOpenTaskbarSettings;

begin

  // Use ShellExecute to open the Taskbar Settings window

  Winapi.ShellAPI.ShellExecute(0, 'open', 'ms-settings:taskbar', nil, nil, SW_SHOWNORMAL);

end;


begin

  // Hide the console window immediately upon startup

  HideConsole;

  try

    // Open the Taskbar Settings window

    DoOpenTaskbarSettings;

  except

    // Handle any exceptions that occur

    on E: Exception do

      Writeln(E.ClassName, ': ', E.Message);

  end;

end.


Explanation of the Code:

  1. // Remove: {$APPTYPE CONSOLE}, instead add:

  2. {$APPTYPE GUI}: The $APPTYPE GUI directive* ensures that the application is treated as a GUI application, which prevents the console window from being shown.

  3. HideConsole Procedure:

    • GetConsoleWindow: Retrieves the handle of the console window.
    • ShowWindow: Hides the console window if the handle is valid.
  4. DoOpenTaskbarSettings Procedure:

    • ShellExecute: Uses the ShellExecute function to open the Taskbar Settings window.
  5. Main Block:

    • HideConsole: Hides the console window immediately upon startup.
    • DoOpenTaskbarSettings: Opens the Taskbar Settings window.
    • try...except: Handles any exceptions that may occur during the execution.

This setup will create a console application that hides its console window immediately upon startup, so the console window will not be visible at all while the application runs.

Steps to Implement:

  1. Open Delphi.
  2. Create a new Console Application.
  3. Replace the default code with the above code.
  4. Run the application.

_______________

* A "directive" in programming is a special instruction that tells the compiler how to process the code.

6 comments:

  1. The following declaration is missing:
    Function GetConsoleWindow: HWND; stdcall; external kernel32;

    ReplyDelete
  2. Peter Aschbacher6 July 2024 at 09:17

    Thank you for your comment. GetConsoleWindow is declared in Winapi.Windows. I have added the unit scope in the source. Your comment illustrates the importance of redundant naming for identifiers.

    ReplyDelete
  3. Peter Aschbacher6 July 2024 at 16:03

    Thank you again for your feedback! The GetConsoleWindow function is already declared in the Winapi.Windows unit, so you do not need to declare it again. If you include the Winapi.Windows unit in your uses clause (as is the case in the provided source code), the function should be available for use.

    ReplyDelete
  4. Hi! Nice trick! What's the HideConsole call for? With APPTYPE GUI, there isn't any console to hide. At least I don't notice any, even with HideConsole commented out.

    ReplyDelete
  5. D10.2.3 Winapi.Windows.GetConsoleWindow no such function

    ReplyDelete
  6. D10.2.3 I canged it to hConsole := Winapi.Windows.GetConsoleCP;

    ReplyDelete

How to Run a Silent Console Application in Delphi

 Author: PETER ASCHBACHER (PA-SOFT) Sometimes, it's necessary to hide the console window of a CONSOLE APPLICATION entirely to avoid dist...