{"id":102408,"date":"2019-04-10T07:00:00","date_gmt":"2019-04-10T14:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=102408"},"modified":"2019-06-06T17:42:11","modified_gmt":"2019-06-07T00:42:11","slug":"20190410-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20190410-00\/?p=102408","title":{"rendered":"Even if you open a file with GUID, you can still get its name, or at least one of its names"},"content":{"rendered":"<p>Some time ago, <a HREF=\"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/20110228-00\/?p=11363\">I showed how you could eschew file names entirely and use GUIDs to open your files<\/a>. But even if you choose to open the file with a GUID, you can still get its name. <\/p>\n<p>Take the second program from that earlier article and make these changes: <\/p>\n<pre>\n#define UNICODE\n#define _UNICODE\n#include &lt;windows.h&gt;\n#include &lt;stdio.h&gt;\n#include &lt;tchar.h&gt;\n#include &lt;ole2.h&gt;\n\nint __cdecl _tmain(int argc, PTSTR *argv)\n{\n HANDLE hRoot = CreateFile(_T(\"C:\\\\\"), 0,\n                 FILE_SHARE_READ | FILE_SHARE_WRITE |\n                 FILE_SHARE_DELETE, NULL,\n                 OPEN_EXISTING,\n                 FILE_FLAG_BACKUP_SEMANTICS, NULL);\n if (hRoot != INVALID_HANDLE_VALUE) {\n  FILE_ID_DESCRIPTOR desc;\n  desc.dwSize = sizeof(desc);\n  desc.Type = ObjectIdType;\n  if (SUCCEEDED(CLSIDFromString(argv[1], &amp;desc.ObjectId))) {\n   HANDLE h = OpenFileById(hRoot, &amp;desc, GENERIC_READ,\n                 FILE_SHARE_READ | FILE_SHARE_WRITE |\n                 FILE_SHARE_DELETE, NULL, 0);\n   if (h != INVALID_HANDLE_VALUE) {\n    <font COLOR=\"blue\">TCHAR buffer[MAX_PATH];\n    DWORD result = GetFinalPathNameByHandle(h,\n                                buffer, ARRAYSIZE(buffer));\n    if (result &gt; 0 &amp;&amp; result &lt; ARRAYSIZE(buffer)) {\n     _tprintf(_T(\"Final path is %s\\n\"), buffer);\n    }<\/font>\n    CloseHandle(h);\n   }\n  }\n  CloseHandle(hRoot);\n }\n return 0;\n}\n<\/pre>\n<p>There&#8217;s a catch: If the file has multiple names (say, due to hard links), then only one of the names is returned, and you don&#8217;t get to pick which one. The system will pick one arbitrarily. <\/p>\n<p>You can get the other names with the <code>FindFirstFileName<\/code> function, which I discussed <a HREF=\"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/20110720-00\/?p=10103\">some time ago<\/a>. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>GetFinalPathByHandle works even for files opened by ID.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-102408","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>GetFinalPathByHandle works even for files opened by ID.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/102408","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=102408"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/102408\/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=102408"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=102408"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=102408"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}