{"id":11543,"date":"2011-02-09T07:00:00","date_gmt":"2011-02-09T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2011\/02\/09\/why-does-shgetknownfolderpath-return-e_fail-for-a-known-folder\/"},"modified":"2011-02-09T07:00:00","modified_gmt":"2011-02-09T07:00:00","slug":"why-does-shgetknownfolderpath-return-e_fail-for-a-known-folder","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20110209-00\/?p=11543","title":{"rendered":"Why does SHGetKnownFolderPath return E_FAIL for a known folder?"},"content":{"rendered":"<p>\nA customer reported having problems with the\n<code>SH&shy;Get&shy;Known&shy;Folder&shy;Path<\/code> function.\nI&#8217;ve left in the red herrings.\n<\/p>\n<blockquote CLASS=\"q\">\n<p>\nOn Windows&nbsp;7, I&#8217;m trying to retrieve the Internet folder\nwith the following code:\n<\/p>\n<pre>\nif (OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &amp;hToken))\n{\n HRESULT hr = SHGetKnownFolderPath(FOLDERID_InternetFolder,\n                              KF_FLAG_DONT_VERIFY, hToken, &amp;pszPath);\n ...\n}\n<\/pre>\n<p>\nThe call always fails with <code>E_FAIL<\/code>.\nWhat am I doing wrong?\nI tried passing <code>NULL<\/code> as the token, but that didn&#8217;t help.\n<\/p>\n<\/blockquote>\n<p>\nThe reason the call fails has nothing to do with Windows&nbsp;7\nor the token.\nThe call fails because <code>FOLDERID_Internet&shy;Folder<\/code> is\na virtual folder&mdash;there is no path in the first place!\n<\/p>\n<blockquote CLASS=\"m\"><p>\nThe reason is that the folder you are requesting is a virtual\nfolder (KF_CATEGORY_VIRTUAL).\nVirtual folders don&#8217;t exist in the file system, so they don&#8217;t have a path.\n<code>SH&shy;Get&shy;Known&shy;Folder&shy;Item<\/code> should work.\n<\/p><\/blockquote>\n<p>\nThe customer appears to have misinterpreted this response\nin a way I wasn&#8217;t expecting\n(but which sadly\n<a HREF=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2004\/02\/25\/79842.aspx\">\nI&#8217;ve seen before<\/a>):\n<\/p>\n<blockquote CLASS=\"q\">\n<p>\nI added the <code>KF_CATEGORY_VIRTUAL<\/code> flag, but I still\nget the same error back.\n<\/p>\n<pre>\nif (OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &amp;hToken))\n{\n HRESULT hr = SHGetKnownFolderPath(FOLDERID_InternetFolder,\n                              KF_FLAG_DONT_VERIFY | KF_CATEGORY_VIRTUAL,\n                              hToken, &amp;pszPath);\n ...\n}\n<\/pre>\n<\/blockquote>\n<p>\nUm, no, that makes no sense at all.\n<code>KF_CATEGORY_VIRTUAL<\/code>\nis a <code>KF_CATEGORY<\/code>\nvalue, but the second parameter to\n<code>SH&shy;GetKnown&shy;Folder&shy;Path<\/code>\nis a <code>KNOWN_FOLDER_FLAG<\/code>.\nYou can&#8217;t just combine unrelated values like that.\nIt&#8217;s like\n<a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2004\/06\/03\/147584.aspx\">\nadding 3&nbsp;grams to 12&nbsp;meters<\/a>.\n<\/p>\n<p>\nAnd second, the <code>KF_CATEGORY_VIRTUAL<\/code> enumeration isn&#8217;t\nsomething that you pass in to &#8220;override&#8221; anything.\nThe point is that <code>FOLDERID_Internet&shy;Folder<\/code> is a virtual\nfolder: It has no path, so if you try to ask for its path, you&#8217;ll\njust get an error back because the thing you&#8217;re looking for\nsimply doesn&#8217;t exist.\n<\/p>\n<p>\nI never did figure out what this customer was trying to do.\nMaybe they figured, since they can&#8217;t\n<a HREF=\"http:\/\/notalwaysright.com\/byte-off-more-than-you-can-chew\/2372\">\ndownload the Internet<\/a>,\nthey could at least try to do a\n<code>Find&shy;First&shy;File<\/code> on it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A customer reported having problems with the SH&shy;Get&shy;Known&shy;Folder&shy;Path function. I&#8217;ve left in the red herrings. On Windows&nbsp;7, I&#8217;m trying to retrieve the Internet folder with the following code: if (OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &amp;hToken)) { HRESULT hr = SHGetKnownFolderPath(FOLDERID_InternetFolder, KF_FLAG_DONT_VERIFY, hToken, &amp;pszPath); &#8230; } The call always fails with E_FAIL. What am I doing wrong? I tried [&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-11543","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>A customer reported having problems with the SH&shy;Get&shy;Known&shy;Folder&shy;Path function. I&#8217;ve left in the red herrings. On Windows&nbsp;7, I&#8217;m trying to retrieve the Internet folder with the following code: if (OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &amp;hToken)) { HRESULT hr = SHGetKnownFolderPath(FOLDERID_InternetFolder, KF_FLAG_DONT_VERIFY, hToken, &amp;pszPath); &#8230; } The call always fails with E_FAIL. What am I doing wrong? I tried [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/11543","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=11543"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/11543\/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=11543"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=11543"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=11543"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}