{"id":108863,"date":"2023-10-06T07:00:00","date_gmt":"2023-10-06T14:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=108863"},"modified":"2023-10-06T08:44:15","modified_gmt":"2023-10-06T15:44:15","slug":"20231006-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20231006-00\/?p=108863\/","title":{"rendered":"A very belated improvement to the filtering of the Browse for Folder dialog so it shows only drive letters"},"content":{"rendered":"<p>Quite some time ago, I showed how to <a title=\"Filtering the Browse for Folder dialog so it shows only drive letters\" href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20170424-00\/?p=96025\"> filter the Browse for Folder dialog so it shows only drive letters<\/a>. There&#8217;s a small quirk in the code, which it has taken me several years to getting around to addressing.<\/p>\n<p>But hey, better late than never.<\/p>\n<p>To recap, we detected drive letters by verifying that the length is at most 4, and that it is reported as a root directory by <code>Path\u00adIsRoot<\/code>. While this successfully allows drives <code>A:\\<\/code> through <code>Z:\\<\/code>, the <code>Path\u00adIs\u00adRoot<\/code> function also reports <code>\\\\a<\/code> through <code>\\\\z<\/code> as roots if you had a server with a one-character name. It also reports <code>\\<\/code> as a root, which is I guess sort of true?<\/p>\n<p>Now, these aren&#8217;t problems in practice because you cannot add <code>\\\\a<\/code> or <code>\\<\/code> as children of My Computer, or at least, you can&#8217;t do it today. But just to make sure, let&#8217;s reject those cases.<\/p>\n<p>Fortunately, the fix is simple: Instead of using <code>PathIsRoot<\/code>, we switch to <code>PathGetDriveNumber<\/code>, which returns \u22121 if the path does not begin with a letter and a colon.<\/p>\n<pre>    if (SUCCEEDED(StrRetToBuf(&amp;str, pidlItem, buf, ARRAYSIZE(buf))) &amp;&amp;\r\n        <span style=\"border: dashed 1px currentcolor;\">\/* <span style=\"text-decoration: line-through;\">PathIsRoot(buf)<\/span> *\/<\/span>\r\n        <span style=\"border: solid 1px currentcolor;\">PathGetDriveNumber(buf) &gt;= 0<\/span>) return S_OK;\r\n<\/pre>\n<p>There is still a tiny hole here: If someday it becomes possible to add raw drive letters to My Computer, then this will accept <code>A:<\/code> as well as <code>A:\\<\/code>. I&#8217;m not sure what that would even mean, but you could wear a belt and suspenders by using <code>Path\u00adIs\u00adRoot<\/code> to verify that you got a root, and then using <code>Path\u00adGet\u00adDrive\u00adNumber<\/code> to verify that you got a drive.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The case of the very short UNC.<\/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-108863","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>The case of the very short UNC.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/108863","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=108863"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/108863\/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=108863"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=108863"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=108863"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}