{"id":10523,"date":"2011-06-01T07:00:00","date_gmt":"2011-06-01T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2011\/06\/01\/how-do-i-prevent-users-from-pinning-my-program-to-the-taskbar\/"},"modified":"2011-06-01T07:00:00","modified_gmt":"2011-06-01T07:00:00","slug":"how-do-i-prevent-users-from-pinning-my-program-to-the-taskbar","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20110601-00\/?p=10523","title":{"rendered":"How do I prevent users from pinning my program to the taskbar?"},"content":{"rendered":"<p>\nA customer wanted to prevent users from pinning their application\nto the taskbar.\n<\/p>\n<blockquote CLASS=\"q\"><p>\nI have an application that is launched as a helper by a main application.\nUsers shouldn&#8217;t be launching it directly, but rather should be launching\nthe main application.\nBut since the helper shows up in the taskbar,\nusers may be tempted to right-click on the taskbar icon and select\n&#8220;Pin to taskbar.&#8221;\nUnfortunately, this pins the helper program to the taskbar instead of\nthe main application,\nand launching the helper program directly doesn&#8217;t work.\nIs there a way I can prevent users from pinning the helper program?\n<\/p><\/blockquote>\n<p>\nIt so happens that there are\n<a HREF=\"http:\/\/msdn.microsoft.com\/en-us\/library\/dd378459.aspx#exclusion_lists\">\na number of ways of marking your\nhelper program as <i>Don&#8217;t pin me<\/i><\/a>.\nGiven the description above, the most direct way is probably to set the\n<a HREF=\"http:\/\/msdn.microsoft.com\/en-us\/library\/dd561983.aspx\">\n<code>System.App&shy;User&shy;Model.Prevent&shy;Pinning<\/code>\nproperty<\/a>\non the window created by the helper program.\n<\/p>\n<p>\nTake our\n<a HREF=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2003\/07\/23\/54576.aspx\">\nscratch program<\/a>\nand make the following changes:<\/p>\n<pre>\n<font COLOR=\"blue\">#include &lt;shellapi.h&gt;\n#include &lt;propsys.h&gt;\n#include &lt;propkey.h&gt;\nHRESULT MarkWindowAsUnpinnable(HWND hwnd)\n{\n IPropertyStore *pps;\n HRESULT hr = SHGetPropertyStoreForWindow(hwnd, IID_PPV_ARGS(&amp;pps));\n if (SUCCEEDED(hr)) {\n  PROPVARIANT var;\n  var.vt = VT_BOOL;\n  var.boolVal = <a HREF=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2004\/12\/22\/329884.aspx\">VARIANT_TRUE<\/a>;\n  hr = pps-&gt;SetValue(PKEY_AppUserModel_PreventPinning, var);\n  pps-&gt;Release();\n }\n return hr;\n}\n<\/font>\nBOOL\nOnCreate(HWND hwnd, LPCREATESTRUCT lpcs)\n{\n <font COLOR=\"blue\">MarkWindowAsUnpinnable(hwnd);<\/font>\n return TRUE;\n}\n<\/pre>\n<p>\nI set the <code>PROP&shy;VARIANT<\/code> manually instead of using\n<code>Init&shy;Prop&shy;Variant&shy;From&shy;Boolean<\/code>\njust to emphasize that\n<a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2004\/12\/22\/329884.aspx\">\nthe <code>boolVal<\/code> must be <code>VARIANT_TRUE<\/code> and not\n<code>TRUE<\/code><\/a>.\nIn real life, I probably would have used\n<code>Init&shy;Prop&shy;Variant&shy;From&shy;Boolean<\/code>.\n<\/p>\n<p>\nRun this program and observe that &#8220;Pin this program\nto taskbar&#8221; does not appear on the menu when you\nright-click on the taskbar button.\n<\/p>\n<p>\nEven better would be to permit pinning,\nbut set the\n<a HREF=\"http:\/\/msdn.microsoft.com\/en-us\/library\/dd391571.aspx\">\n<code>System.App&shy;User&shy;Model.Relaunch&shy;Command<\/code><\/a>,\n<a HREF=\"http:\/\/msdn.microsoft.com\/en-us\/library\/dd391572.aspx\">\n<code>.Relaunch&shy;Display&shy;Name&shy;Resource<\/code><\/a>\nand optionally\n<a HREF=\"http:\/\/msdn.microsoft.com\/en-us\/library\/dd391573.aspx\">\n<code>.Relaunch&shy;Icon&shy;Resource<\/code><\/a>\nproperties\nso that\n<a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2011\/04\/27\/10158395.aspx#10158646\">\nif the user tries to pin the helper,\nit actually pins the main application<\/a>.\n<\/p>\n<p>\nStart with a new scratch program and make these changes:\n<\/p>\n<pre>\n<font COLOR=\"blue\">#include &lt;shellapi.h&gt;\n#include &lt;propsys.h&gt;\n#include &lt;propkey.h&gt;\n#include &lt;propvarutil.h&gt;\nHRESULT IPropertyStore_SetValue(IPropertyStore *pps,\n    REFPROPERTYKEY pkey, PCWSTR pszValue)\n{\n PROPVARIANT var;\n HRESULT hr = InitPropVariantFromString(pszValue, &amp;var);\n if (SUCCEEDED(hr))\n {\n  hr = pps-&gt;SetValue(pkey, var);\n  PropVariantClear(&amp;var);\n }\n return hr;\n}<\/font>\nBOOL\nOnCreate(HWND hwnd, LPCREATESTRUCT lpcs)\n{\n <font COLOR=\"blue\">IPropertyStore *pps;\n HRESULT hr = SHGetPropertyStoreForWindow(hwnd, IID_PPV_ARGS(&amp;pps));\n if (SUCCEEDED(hr)) {\n  IPropertyStore_SetValue(pps,\n    PKEY_AppUserModel_ID, L\"Contoso.Scratch\");\n  IPropertyStore_SetValue(pps,\n    PKEY_AppUserModel_RelaunchCommand,\n    L\"notepad.exe %windir%\\\\system.ini\");\n  IPropertyStore_SetValue(pps,\n    PKEY_AppUserModel_RelaunchDisplayNameResource,\n    L\"C:\\\\full\\\\path\\\\to\\\\scratch.exe,-1\");\n  \/\/ optionally also set PKEY_AppUserModel_RelaunchIconResource\n  pps-&gt;Release();\n }<\/font>\n return TRUE;\n}\n\/\/ resource file\nSTRINGTABLE BEGIN\n 1 \"Open system.ini\"\nEND\n<\/pre>\n<p>\nI&#8217;m pulling a fast one here and pretending that Notepad is my\nmain application.\nObviously you&#8217;d use your actual main application.\n(I&#8217;m also hard-coding the path to my scratch program.)\n<\/p>\n<p>\nWhen you run this program, right-click on the taskbar button.\nObserve that the option to run a new copy of the program is called\n<i>Open system.ini<\/i> and if you pick it\n(or use the middle-mouse-button shortcut),\nNotepad runs.\nIf you pin the program, the pinned icon runs Notepad.\n<\/p>\n<p>\nEven if you don&#8217;t need to redirect the pinned item to another program,\nyou can use this second technique to\n<a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2011\/04\/27\/10158395.aspx#10158652\">\npass a custom command line for the pinned icon<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A customer wanted to prevent users from pinning their application to the taskbar. I have an application that is launched as a helper by a main application. Users shouldn&#8217;t be launching it directly, but rather should be launching the main application. But since the helper shows up in the taskbar, users may be tempted to [&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-10523","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 users from pinning their application to the taskbar. I have an application that is launched as a helper by a main application. Users shouldn&#8217;t be launching it directly, but rather should be launching the main application. But since the helper shows up in the taskbar, users may be tempted to [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/10523","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=10523"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/10523\/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=10523"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=10523"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=10523"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}