How to start a process from a Windows Forms (C#) without creating a taskbar icon

0

I have a Windows Forms application with a button that's supposed to open an exe file.

The default Windows behavior is that the exe file, once opened, will have its own icon appear in the taskbar, however the requirement is for it not to have the icon, and for the window to appear "nested" inside the icon of the Windows Form application from which it originates, so to hide the location of the exe file from users.

I have looked around for a solution, and my understanding is that in order to achieve this, I would have to use the user32.dll library.

I have found some code online that attempts something similar (no window, no taskbar icon), and I am trying to tweak it so that it fits my needs, but so far I am stuck.

This is what I have:

    private int    GWL_STYLE        = -16;
    private int    GWL_EXSTYLE      = -20;
    private int    WS_EX_APPWINDOW  = 262144;
    private UInt32 WS_POPUP         = 0x80000000;
    private UInt32 WS_CHILD         = 0x40000000;


    [DllImport("user32.dll")]
    private static extern int GetWindowLong(IntPtr hWnd, int nIndex);

    [DllImport("user32.dll")]
    private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);

    [DllImport("user32.dll", SetLastError = true)]
    private static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

    public Process HideProcess(string executablesPath, System.Windows.Forms.Form parentForm) {
        Process valueToReturn = null;
        if (executablesPath != null && executablesPath.Equals(String.Empty) == false && File.Exists(executablesPath) == true) {
            ProcessStartInfo startInfo = new ProcessStartInfo(executablesPath);
            startInfo.WindowStyle = ProcessWindowStyle.Hidden;
            valueToReturn = Process.Start(startInfo);

            int style = GetWindowLong(valueToReturn.MainWindowHandle, GWL_EXSTYLE) & ~WS_EX_APPWINDOW;
            SetWindowLong(valueToReturn.MainWindowHandle, GWL_EXSTYLE, style);
            SetParent(valueToReturn.MainWindowHandle, parentForm.Handle);
        }
        return valueToReturn;
    }

I have tried several different variations of this method, but I always get close but not exactly where I'd like to be. The documentation I have found seems a little bit confusing, so I am a little bit stuck and would appreciate some help.

c#
windows
winforms
hide
taskbar
asked on Stack Overflow Oct 30, 2020 by wasp1311

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0