Search code examples
delphimessageboxdelphi-10-seattle

How display MessageBox without show application icon in Windows taskbar?


I have a dll that is injected in a process and this dll contains a Form where i want use MessageBox() (or some other type of dialog) to alert the user about some operations.

Eg:

Application.MessageBox('successful operation!','Information',mb_Ok+mb_IconInformation);

Happens that everytime that this is showed, also is showed the icon of target application in Windows taskbar, and i not want this.

Then i want display these messages without show application icon in taskbar. How make this?


Solution

  • In Delphi 7, Application.MessageBox() calls the Win32 API MessageBox() function specifying the Application.Handle as the owner window 1.

    Inside a DLL, Application.Handle is 0 by default, so your MessageBox dialog is being displayed without an owner window assigned to it. That explains why it is able to appear on the Taskbar, as only a top-level unowned window (with the APP_EX_APPWINDOW extended style) can appear there.

    So, the simplest solution is to call the Win32 API MessageBox() function yourself, specifying an owner HWND that belongs to the app you have injected your DLL into. Or, if you want to keep using Application.MessageBox(), assign such an HWND to the Application.Handle property after the DLL has been injected.

    But either way, make sure the thread that is calling MessageBox() is attached to the message queue of the chosen owner HWND. If you are calling MessageBox() in a separate thread, use AttachThreadInput() to make that attachment before calling MessageBox(), and again to remove the attachment after MessageBox() exits.

    1: In later Delphi versions, Application.MessageBox uses the Application.OnGetActiveFormHandle event, the Win32 GetActiveWindow() function, and the Win32 GetLastActivePopup() function (in that order) to find a suitable owner window before resorting to using the Application.Handle.