{"id":93093,"date":"2016-02-29T07:00:00","date_gmt":"2016-02-29T22:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/?p=93093"},"modified":"2019-03-13T10:30:55","modified_gmt":"2019-03-13T17:30:55","slug":"20160229-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20160229-00\/?p=93093","title":{"rendered":"How can I get the original shortcut target path with environment variables unexpanded?"},"content":{"rendered":"<p>A customer wanted to be able to get the target of a shortcut with environment variables unexpanded. The <code>IShellLink::Get&shy;Path<\/code> method will expand environment variables. <\/p>\n<p>The way to get the unexpanded target path is to go for the <code>EXP_SZ_LINK<\/code> data in the shell link data list. We <a HREF=\"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/20071219-00\/?p=24103\">briefly encountered the shell link data list a while back<\/a>. Now we&#8217;ll dig in a little more. <\/p>\n<pre>\n#include &lt;windows.h&gt;\n#include &lt;shlobj.h&gt;\n#include &lt;stdio.h&gt; \/\/ Horrors! Mixing stdio and C++!\n#include &lt;atlbase.h&gt;\n#include &lt;atlalloc.h&gt;\n\nint __cdecl wmain(int argc, wchar_t**argv)\n{\n <a HREF=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/\">CCoInitialize<\/a> init;\n\n CComPtr&lt;IShellLink&gt; lnk;\n CoCreateInstance(CLSID_ShellLink, 0,\n                  CLSCTX_ALL, IID_PPV_ARGS(&amp;lnk));\n CComQIPtr&lt;IPersistFile&gt; pf(lnk);\n pf-&gt;Load(argv[1], STGM_READ);\n\n CComQIPtr&lt;IShellLinkDataList&gt; list(lnk);\n DWORD flags;\n list-&gt;GetFlags(&amp;flags);\n if (flags &amp; SLDF_HAS_EXP_SZ) {\n  CHeapPtr&lt;void, CLocalAllocator&gt; rawData;\n  list-&gt;CopyDataBlock(EXP_SZ_LINK_SIG, &amp;rawData);\n  auto linkData = reinterpret_cast&lt;EXP_SZ_LINK *&gt;(static_cast&lt;void *&gt;(rawData));\n  printf(\"Unexpanded target = %ls\\n\", linkData-&gt;swzTarget);\n }\n return 0;\n}\n<\/pre>\n<p>After loading the shortcut file, we ask the <code>IShell&shy;Link&shy;Data&shy;List<\/code> to inspect the shortcut flags. If the <code>SLDF_HAS_EXP_SZ<\/code> flag is set, then the path to the target contains an environment variable reference. To get the original unexpanded path, ask for the <code>EXP_SZ_LINK_SIG<\/code> data block. That returns a data block in the form of a <code>EXP_SZ_LINK<\/code> structure, from which you can extract the unexpanded paths. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>You&#8217;ll have to dig into the data list.<\/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-93093","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>You&#8217;ll have to dig into the data list.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/93093","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=93093"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/93093\/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=93093"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=93093"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=93093"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}