{"id":93001,"date":"2016-02-08T07:00:00","date_gmt":"2016-02-08T22:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/?p=93001"},"modified":"2019-03-13T10:30:06","modified_gmt":"2019-03-13T17:30:06","slug":"20160208-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20160208-00\/?p=93001","title":{"rendered":"How can I get the canonical name for a known folder?"},"content":{"rendered":"<p>A customer had a question about the <a HREF=\"https:\/\/msdn.microsoft.com\/en-us\/library\/windows\/desktop\/bb761740(v=vs.85).aspx\"><code>IKnown&shy;Folder::Get&shy;Folder&shy;By&shy;Name<\/code><\/a> method: &#8220;What is the canonical name for the Documents folder? We tried <tt>\"Documents\"<\/tt> but that didn&#8217;t work.&#8221; <\/p>\n<p>One question that comes to mind is why you are using this method to begin with. If you already know that you want the Documents folder, then use <a HREF=\"https:\/\/msdn.microsoft.com\/en-us\/library\/windows\/desktop\/bb761738(v=vs.85).aspx\"><code>IKnown&shy;Folder::Get&shy;Folder<\/code><\/a> and pass <code>FOLDERID_Documents<\/code>. Just go straight for the thing you want; don&#8217;t play around with canonical names. <\/p>\n<p>But okay, let&#8217;s answer the question anyway. The way to get the canonical name for a folder is to ask it! <\/p>\n<p>Today&#8217;s smart pointer library is (rolls dice) ATL! <\/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 main()\n{\n <a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2004\/05\/20\/135841.aspx\">CCoInitialize<\/a> init;\n\n CComPtr&lt;IKnownFolderManager&gt; mgr;\n CoCreateInstance(CLSID_KnownFolderManager, 0,\n                  CLSCTX_ALL, IID_PPV_ARGS(&amp;mgr));\n\n CComPtr&lt;IKnownFolder&gt; kf;\n mgr-&gt;GetFolder(FOLDERID_Documents, &amp;kf);\n KNOWNFOLDER_DEFINITION def;\n kf-&gt;GetFolderDefinition(&amp;def);\n printf(\"%ls\\n\", def.pszName);\n FreeKnownFolderDefinitionFields(&amp;def);\n return 0;\n}\n<\/pre>\n<p>Run this program and it tells you that the canonical name is <tt>Personal<\/tt>. <\/p>\n<p>Let&#8217;s go one step further: Let&#8217;s print the canonical names for <i>all<\/i> of the known folders. <\/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 main()\n{\n <a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2004\/05\/20\/135841.aspx\">CCoInitialize<\/a> init;\n\n CComPtr&lt;IKnownFolderManager&gt; mgr;\n CoCreateInstance(CLSID_KnownFolderManager, 0,\n                  CLSCTX_ALL, IID_PPV_ARGS(&amp;mgr));\n\n <font COLOR=\"blue\">UINT count;\n CComHeapPtr&lt;KNOWNFOLDERID&gt; kfids;\n mgr-&gt;GetFolderIds(&amp;kfids, &amp;count);\n for (UINT index = 0; index &lt; count; index++) {<\/font>\n  CComPtr&lt;IKnownFolder&gt; kf;\n  mgr-&gt;GetFolder(<font COLOR=\"blue\">kfids[index]<\/font>, &amp;kf);\n  KNOWNFOLDER_DEFINITION def;\n  kf-&gt;GetFolderDefinition(&amp;def);\n  printf(\"%ls\\n\", def.pszName);\n  FreeKnownFolderDefinitionFields(&amp;def);\n <font COLOR=\"blue\">}<\/font>\n return 0;\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>You ask it.<\/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-93001","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>You ask it.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/93001","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=93001"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/93001\/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=93001"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=93001"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=93001"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}