{"id":3233,"date":"2013-09-16T07:00:00","date_gmt":"2013-09-16T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2013\/09\/16\/forcing-a-file-handle-closed-when-it-has-been-opened-remotely\/"},"modified":"2013-09-16T07:00:00","modified_gmt":"2013-09-16T07:00:00","slug":"forcing-a-file-handle-closed-when-it-has-been-opened-remotely","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20130916-00\/?p=3233","title":{"rendered":"Forcing a file handle closed when it has been opened remotely"},"content":{"rendered":"<p>\nToday&#8217;s Little Program closes a file handle that was opened remotely.\nIt builds on\n<a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2013\/03\/11\/10399694.aspx\">\nprevious discussion on how to use the <code>Net&shy;Xxx<\/code> functions<\/a>.\n<\/p>\n<pre>\nint __cdecl wmain(int argc, wchar_t **argv)\n{\n FILE_INFO_3 *pinfo3;\n NET_API_STATUS status;\n DWORD_PTR resumeHandle = 0;\n do {\n  DWORD actual, estimatedTotal;\n  status = NetFileEnum(NULL, NULL, NULL, 3,\n                       (LPBYTE*)&amp;pinfo3,\n                       MAX_PREFERRED_LENGTH,\n                       &amp;actual,\n                       &amp;estimatedTotal,\n                       &amp;resumeHandle);\n  if (status == NERR_Success ||\n      status == ERROR_MORE_DATA) {\n   for (DWORD i = 0; i &lt; actual; i++) {\n    <font COLOR=\"blue\">if (lstrcmpiW(argv[1], pinfo3[i].fi3_pathname) == 0) {\n     wprintf(L\"Closing %ls result %d\\n\", pinfo3[i].fi3_pathname,\n             NetFileClose(NULL, pinfo3[i].fi3_id));\n     status = ERROR_NO_MORE_FILES;\n     break;\n    }<\/font>\n   }\n   NetApiBufferFree(pinfo3);\n  }\n } while (status == ERROR_MORE_DATA);\n return 0;\n}\n<\/pre>\n<p>\nForcing a network file handle closed does <i>not<\/i>\nactually close the handle.\nThis makes it very different from\n<a HREF=\"http:\/\/technet.microsoft.com\/en-us\/magazine\/2009.04.windowsconfidential.aspx\">\nthe various &#8220;force handle closed&#8221;\nutilities out there<\/a>.\nRather, forcing a network file handle closed is accomplished by\nsimulating a network failure,\nso that when the remote machine tries to use the handle again,\nit&#8217;s told, &#8220;Wha? I&#8217;m sorry, we must have a bad connection,\nbecause I&#8217;m not sure what you&#8217;re talking about.&#8221;\nSince programs which access network resources must deal with\nthe possibility of network connectivity loss,\nthis deception does not violate the interface contract.\n<\/p>\n<p>\n(Doing this to handles to local resources is a much riskier\nundertaking, because applications expect access to local files\nto remain valid for the lifetime of the handle.\nThere is no equivalent of transient network connectivity failure\nfor local files on non-removable drives.\nThere is also no API for simulating it.)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today&#8217;s Little Program closes a file handle that was opened remotely. It builds on previous discussion on how to use the Net&shy;Xxx functions. int __cdecl wmain(int argc, wchar_t **argv) { FILE_INFO_3 *pinfo3; NET_API_STATUS status; DWORD_PTR resumeHandle = 0; do { DWORD actual, estimatedTotal; status = NetFileEnum(NULL, NULL, NULL, 3, (LPBYTE*)&amp;pinfo3, MAX_PREFERRED_LENGTH, &amp;actual, &amp;estimatedTotal, &amp;resumeHandle); if [&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-3233","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Today&#8217;s Little Program closes a file handle that was opened remotely. It builds on previous discussion on how to use the Net&shy;Xxx functions. int __cdecl wmain(int argc, wchar_t **argv) { FILE_INFO_3 *pinfo3; NET_API_STATUS status; DWORD_PTR resumeHandle = 0; do { DWORD actual, estimatedTotal; status = NetFileEnum(NULL, NULL, NULL, 3, (LPBYTE*)&amp;pinfo3, MAX_PREFERRED_LENGTH, &amp;actual, &amp;estimatedTotal, &amp;resumeHandle); if [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/3233","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=3233"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/3233\/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=3233"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=3233"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=3233"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}