6

I have a number of separate processes that are logically related (but all are started separately - there is no common 'parent' process).

Is it possible to make them appear as one group in the Windows Taskbar?

Working sample

Here's some working code inspired by Remy's answer

using System;
using System.Runtime.InteropServices;
using System.Security;

namespace ConsoleApplication1
{
    [SuppressUnmanagedCodeSecurity]
    internal static class SafeNativeMethods
    {
        [DllImport("shell32.dll")]
        public static extern int SetCurrentProcessExplicitAppUserModelID([MarshalAs(UnmanagedType.LPWStr)] string AppID);

        [DllImport("kernel32.dll")]
        public static extern bool AllocConsole();

        [DllImport("kernel32.dll")]
        public static extern bool FreeConsole();
    }

    internal class Program
    {
        public static int SetApplicationUserModelId(string appId)
        {
            // check for Windows 7
            Version version = Environment.OSVersion.Version;
            if ((version.Major > 6) || (version.Major == 6 && version.Minor >= 1))
                return SafeNativeMethods.SetCurrentProcessExplicitAppUserModelID(appId);
            return -1;
        }

        [STAThread]
        public static void Main(string[] args)
        {
            int result = SetApplicationUserModelId("Gardiner.Sample1");

            SafeNativeMethods.AllocConsole();

            // Now we have a console, we can write to it
            Console.Title = "Sample 1";

            Console.WriteLine("Sample 1 {0}", result);
            Console.ReadLine();
            SafeNativeMethods.FreeConsole();
        }
    }
}

To get this to work, the executable must be set to have 'Output type' set to 'Windows Application', and configure the 'Startup object' to be 'ConsoleApplication1.Program' (for the code sample above).

David Gardiner
  • 16,892
  • 20
  • 80
  • 117

1 Answers1

5

Yes, but only in Windows 7 and later. Multiple processes and windows are grouped together on the Taskbar if they have the same Application User Model ID assigned to them.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • I tried this by creating two C# Console apps and call SetCurrentProcessExplicitAppUserModelID at the start of the Main method (with the same App User Model ID). It doesn't seem to work. I get two separate icons in the Taskbar – David Gardiner Dec 18 '12 at 05:06
  • I use `SetCurrentProcessExplicitAppUserModelID()` in my app C++ apps and it works fine for me. Perhaps you are calling it too late? What `HRESULT` value is it returnng for you? – Remy Lebeau Dec 18 '12 at 05:34
  • It may be too late, but not sure how I could do it any earlier if it's the first line of the Main method in a .NET console app – David Gardiner Dec 18 '12 at 23:19
  • It may be the first line of your code, but it may not be the first of .NET's code. It has to be called before any other UI-related functions are called. If you can't call it early enough during program startup, then you might need to use the `IPropertyStore` interface directly via `SHGetPropertyStoreForWindow()` instead, if not simply add the ID to the shortcut you use to run your app. – Remy Lebeau Dec 18 '12 at 23:42
  • Looks like my problem was using Console applications. The UI always is created first. Switching to a Windows app (and then creating a console manually) works. – David Gardiner Dec 19 '12 at 23:26