{"id":6833,"date":"2012-08-17T07:00:00","date_gmt":"2012-08-17T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2012\/08\/17\/what-if-my-application-is-really-two-applications-bundled-into-a-single-file-and-i-want-them-collected-into-two-groups-on-the-taskbar-in-windows-7\/"},"modified":"2012-08-17T07:00:00","modified_gmt":"2012-08-17T07:00:00","slug":"what-if-my-application-is-really-two-applications-bundled-into-a-single-file-and-i-want-them-collected-into-two-groups-on-the-taskbar-in-windows-7","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20120817-00\/?p=6833","title":{"rendered":"What if my application is really two applications bundled into a single file, and I want them collected into two groups on the taskbar in Windows 7?"},"content":{"rendered":"<p>\nA customer wanted to prevent multiple copies of their program\nfrom being grouped on the taskbar.\nThey didn&#8217;t give an explanation why,\nbut let&#8217;s assume that they are doing this for honorable purposes\nrather than as a way to annoy the user.\nFor example, maybe their program is really multiple applications\nbundled inside a single EXE file for convenience.\n<\/p>\n<p>\nThe information you need to do this is in MSDN under\n<a HREF=\"http:\/\/msdn.microsoft.com\/en-us\/library\/dd378459(v=VS.85).aspx\">\nApplication User Model IDs<\/a>,\nspecifically in the\n<a HREF=\"http:\/\/msdn.microsoft.com\/en-us\/library\/dd378459(v=VS.85).aspx#where\">\n<i>Where to assign an AppUserModelID<\/i><\/a> section.\nI&#8217;ll assume you&#8217;ve read the guidance there, and I&#8217;m just going to dive\ninto the implementation.\n<\/p>\n<p>\nSuppose our scratch program can serve\n<a HREF=\"http:\/\/snltranscripts.jt.org\/75\/75ishimmer.phtml\">\nboth as a floor wax and as a dessert topping<\/a>.\nIt decides on the mode based on a command line switch.\n<\/p>\n<pre>\n<font COLOR=\"blue\">#include &lt;shlobj.h&gt;<\/font>\nint WINAPI WinMain(HINSTANCE hinst, HINSTANCE hinstPrev,\n                   LPSTR lpCmdLine, int nShowCmd)\n{\n    MSG msg;\n    HWND hwnd;\n    g_hinst = hinst;\n    if (!InitApp()) return 0;\n    <font COLOR=\"blue\">BOOL fDessert = strcmp(lpCmdLine, \"-dessert\") == 0;\n    SetCurrentProcessExplicitAppUserModelID(fDessert ?\n            L\"Contoso.LitWare.DessertTopping\" :\n            L\"Contoso.LitWare.FloorWax\");<\/font>\n    if (SUCCEEDED(CoInitialize(NULL))) {\/* In case we use COM *\/\n        hwnd = CreateWindow(\n            TEXT(\"Scratch\"),                \/* Class Name *\/\n            <font COLOR=\"blue\">fDessert ? TEXT(\"Dessert topping\") : TEXT(\"Floor wax\"),<\/font>\n            WS_OVERLAPPEDWINDOW,            \/* Style *\/\n            CW_USEDEFAULT, CW_USEDEFAULT,   \/* Position *\/\n            CW_USEDEFAULT, CW_USEDEFAULT,   \/* Size *\/\n            NULL,                           \/* Parent *\/\n            NULL,                           \/* No menu *\/\n            hinst,                          \/* Instance *\/\n            0);                             \/* No special parameters *\/\n    ...\n}\n<\/pre>\n<p>\nRun this program a few times, some with the\n<code>-dessert<\/code> switch and some without.\nObserve that the dessert versions and non-dessert versions\ngroup separately.\n<\/p>\n<p>\nThe next level of fancy-pants behavior is to give different\nAppIDs to different windows within a single process.\nYou might do this if your combination floor wax\/dessert topping\nprogram actually runs both modes inside the same process.\nSomething like this:\n<\/p>\n<pre>\n<font COLOR=\"blue\">#include &lt;shellapi.h&gt;\n#include &lt;propkey.h&gt;\n#include &lt;propvarutil.h&gt;\n#include &lt;shlobj.h&gt;<\/font>\n<font COLOR=\"blue\">int g_cWindows = 0;<\/font>\nBOOL\nOnCreate(HWND hwnd, LPCREATESTRUCT lpcs)\n{\n  <font COLOR=\"blue\">++g_cWindows;<\/font>\n  return TRUE;\n}\nvoid\nOnDestroy(HWND hwnd)\n{\n  <font COLOR=\"blue\">if (--g_cWindows == 0)<\/font> PostQuitMessage(0);\n}\n<font COLOR=\"blue\">HWND\nCreateTaskWindow(BOOL fDessert, int nShowCmd)\n{\n  HWND hwnd = CreateWindow(\n      TEXT(\"Scratch\"),                \/* Class Name *\/\n      <font COLOR=\"blue\">fDessert ? TEXT(\"Dessert topping\") : TEXT(\"Floor wax\"),<\/font>\n      WS_OVERLAPPEDWINDOW,            \/* Style *\/\n      CW_USEDEFAULT, CW_USEDEFAULT,   \/* Position *\/\n      CW_USEDEFAULT, CW_USEDEFAULT,   \/* Size *\/\n      NULL,                           \/* Parent *\/\n      NULL,                           \/* No menu *\/\n      g_hinst,                        \/* Instance *\/\n      0);                             \/* No special parameters *\/\n  if (hwnd) {\n    IPropertyStore *pps;\n    HRESULT hr = SHGetPropertyStoreForWindow(hwnd, IID_PPV_ARGS(&amp;pps));\n    if (SUCCEEDED(hr)) {\n      <a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2011\/06\/01\/10170113.aspx\">IPropertyStore_SetValue<\/a>(pps, PKEY_AppUserModel_ID,\n            fDessert ?\n            L\"Contoso.LitWare.DessertTopping\" :\n            L\"Contoso.LitWare.FloorWax\");\n      pps-&gt;Release();\n    }\n    ShowWindow(hwnd, nShowCmd);\n  }\n  return hwnd;\n}\nvoid\nOnChar(HWND hwnd, TCHAR ch, int cRepeat)\n{\n    switch (ch) {\n    case 'd': CreateTaskWindow(TRUE, SW_SHOWNORMAL); break;\n    case 'f': CreateTaskWindow(FALSE, SW_SHOWNORMAL); break;\n    }\n}\n    HANDLE_MSG(hwnd, WM_CHAR, OnChar);<\/font>\nint WINAPI WinMain(HINSTANCE hinst, HINSTANCE hinstPrev,\n                   LPSTR lpCmdLine, int nShowCmd)\n{\n    MSG msg;\n    HWND hwnd;\n    g_hinst = hinst;\n    if (!InitApp()) return 0;\n    <font COLOR=\"blue\">BOOL fDessert = strcmp(lpCmdLine, \"-dessert\") == 0;<\/font>\n    <font COLOR=\"red\"><strike>\/\/ SetCurrentProcessExplicitAppUserModelID(...);<\/strike><\/font>\n    if (SUCCEEDED(CoInitialize(NULL))) {\/* In case we use COM *\/\n        hwnd = <font COLOR=\"blue\">CreateTaskWindow(fDessert, nShowCmd);<\/font>\n        <font COLOR=\"red\"><strike>\/\/ ShowWindow(hwnd, nShowCmd);<\/strike><\/font>\n    ...\n}\n<\/pre>\n<p>\nThis time, instead of setting the application ID globally,\nwe set it on a per-window basis.\nWhen you run this program, you can press &#8220;f&#8221; to open a new floor wax window\nor &#8220;d&#8221; to open a new dessert topping window.\nAs before, observe that the two types of windows group separately.\n<\/p>\n<p>\nThe last detail is setting the <code>System.AppUserModel.ID<\/code>\nproperty on the shortcuts used to launch these programs.\nYou can do this from MSI by adding an entry to your\n<code>Msi&shy;Shortcut&shy;Property<\/code> table,\nor if you create your shortcuts programmatically, you do this by\nsetting the property yourself:\n<\/p>\n<pre>\n CComPtr&lt;IShellLink&gt; spsl;\n spsl.CoCreateInstance(CLSID_ShellLink);\n spsl-&gt;SetPath(TEXT(\"C:\\\\Path\\\\to\\\\scratch.exe\"));\n <font COLOR=\"blue\">CComQIPtr&lt;IPropertyStore&gt; spps(spsl);\n IPropertyStore_SetValue(spps, PKEY_AppUserModel_ID,\n                         L\"Contoso.LitWare.FloorWax\");\n spps-&gt;Commit();<\/font>\n CComQIPtr&lt;IPersistFile&gt;(spsl)-&gt;Save(L\"LitWare Floor Wax.lnk\", TRUE);\n<\/pre>\n<p>\nNext time, we&#8217;ll look at another reason you might want\nto customize how your application group on the taskbar\nin Windows&nbsp;7.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A customer wanted to prevent multiple copies of their program from being grouped on the taskbar. They didn&#8217;t give an explanation why, but let&#8217;s assume that they are doing this for honorable purposes rather than as a way to annoy the user. For example, maybe their program is really multiple applications bundled inside a single [&hellip;]<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-6833","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>A customer wanted to prevent multiple copies of their program from being grouped on the taskbar. They didn&#8217;t give an explanation why, but let&#8217;s assume that they are doing this for honorable purposes rather than as a way to annoy the user. For example, maybe their program is really multiple applications bundled inside a single [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/6833","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=6833"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/6833\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=6833"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=6833"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=6833"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}