{"id":24103,"date":"2007-12-19T10:00:00","date_gmt":"2007-12-19T10:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2007\/12\/19\/how-do-i-mark-a-shortcut-file-as-requiring-elevation\/"},"modified":"2007-12-19T10:00:00","modified_gmt":"2007-12-19T10:00:00","slug":"how-do-i-mark-a-shortcut-file-as-requiring-elevation","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20071219-00\/?p=24103","title":{"rendered":"How do I mark a shortcut file as requiring elevation?"},"content":{"rendered":"<p>\nSpecifying whether elevation is required\nis typically something that is the responsibility of the program.\nThis is done by\n<a HREF=\"http:\/\/msdn.microsoft.com\/library\/en-us\/dnlong\/html\/AccProtVista.asp\">\nadding a <code>requestedExecutionLevel<\/code> element to your manifest<\/a>.\n(<a HREF=\"http:\/\/community.bartdesmet.net\/blogs\/bart\/archive\/2006\/10\/28\/Windows-Vista-_2D00_-Demand-UAC-elevation-for-an-application-by-adding-a-manifest-using-mt.exe.aspx\">Bart De Smet shows you how<\/a>.\n<a HREF=\"http:\/\/blogs.msdn.com\/calvin_hsia\/archive\/2007\/04\/13\/add-a-manifest-to-control-your-application-vista-uac-behavior.aspx\">\nCalvin Hsia does the same for your Visual FoxPro programs<\/a>.)\nBut if the program you&#8217;re running doesn&#8217;t have such a manifest&mdash;maybe\nit&#8217;s an old program that you don&#8217;t have\nany control over&mdash;you can create a shortcut to the program and\nmark the shortcut as requiring elevation.\n<\/p>\n<p>\nTo do this, you set the <code>SLDF_RUNAS_USER<\/code> flag in the\nshortcut attributes.\nHere&#8217;s a skeleton program that sets the flag on the shortcut\nwhose path is passed on the command line.\nFor expository purposes, I&#8217;ve skimped on the error reporting,\nand just to shake things up, I&#8217;ve used ATL smart pointers.\n<\/p>\n<pre>\n#include &lt;windows.h&gt;\n#include &lt;shlobj.h&gt;\n#include &lt;atlbase.h&gt;\nvoid MarkShortcutRunAs(LPCWSTR pszShortcut)\n{\n CComPtr&lt;IPersistFile&gt; sppf;\n if (FAILED(sppf.CoCreateInstance(CLSID_ShellLink))) return;\n if (FAILED(sppf-&gt;Load(pszShortcut, STGM_READWRITE))) return;\n CComQIPtr&lt;IShellLinkDataList&gt; spdl(sppf);\n if (!spdl) return;\n DWORD dwFlags;\n if (FAILED(spdl-&gt;GetFlags(&amp;dwFlags))) return;\n dwFlags |= SLDF_RUNAS_USER;\n if (FAILED(spdl-&gt;SetFlags(dwFlags))) return;\n if (FAILED(sppf-&gt;Save(NULL, TRUE))) return;\n wprintf(L\"Succeeded\\n\");\n}\nint __cdecl wmain(int argc, wchar_t *argv[])\n{\n if (argc == 2 &amp;&amp; SUCCEEDED(CoInitialize(NULL))) {\n  MarkShortcutRunAs(argv[1]);\n  CoUninitialize();\n }\n return 0;\n}\n<\/pre>\n<p>\nThere&#8217;s not really much to this program.\nIt creates a shell link object\n(<code>CLSID_ShellLink<\/code>) and\nasks it to load from the file whose path is given on the command line.\nIt then uses <code>IShellLinkDataList::GetFlags<\/code> and\n<code>IShellLinkDataList::SetFlags<\/code> to fetch the old flags\nand set new flags that include <code>SLDF_RUNAS_USER<\/code>.\nOnce that&#8217;s done, it saves the result back out.\n<\/p>\n<p>\nThe hard part was knowing that the <code>SLDF_RUNAS_USER<\/code>\nflag existed in the first place.\n<\/p>\n<p>\n(I fear that most people will read this article and say,\n&#8220;Awesome! My program requires elevation, and this is how I can\nmark my Start menu shortcut to prompt for elevation.\nThanks, Raymond!&#8221;\nThese people will have completely ignored the opening paragraph,\nwhich explains that that is the <i>wrong thing to do<\/i>.)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Specifying whether elevation is required is typically something that is the responsibility of the program. This is done by adding a requestedExecutionLevel element to your manifest. (Bart De Smet shows you how. Calvin Hsia does the same for your Visual FoxPro programs.) But if the program you&#8217;re running doesn&#8217;t have such a manifest&mdash;maybe it&#8217;s an [&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-24103","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Specifying whether elevation is required is typically something that is the responsibility of the program. This is done by adding a requestedExecutionLevel element to your manifest. (Bart De Smet shows you how. Calvin Hsia does the same for your Visual FoxPro programs.) But if the program you&#8217;re running doesn&#8217;t have such a manifest&mdash;maybe it&#8217;s an [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/24103","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=24103"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/24103\/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=24103"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=24103"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=24103"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}