{"id":8343,"date":"2012-02-09T07:00:00","date_gmt":"2012-02-09T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2012\/02\/09\/what-is-the-effect-of-memory-mapped-file-access-on-getlasterror\/"},"modified":"2012-02-09T07:00:00","modified_gmt":"2012-02-09T07:00:00","slug":"what-is-the-effect-of-memory-mapped-file-access-on-getlasterror","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20120209-00\/?p=8343","title":{"rendered":"What is the effect of memory-mapped file access on GetLastError()?"},"content":{"rendered":"<p>\nA customer was using memory-mapped files and was looking for\ninformation as to whether access to the memory-mapped data\nmodifies the value returned by\n<code>Get&shy;Last&shy;Error<\/code>.\nA member of the kernel team replied,\n&#8220;No, memory-mapped I\/O does not ever change the value returned by\n<code>Get&shy;Last&shy;Error<\/code>.&#8221;\n<\/p>\n<p>\nThat answer is simultaneously correct and wrong,\na case of\n<a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2011\/05\/12\/10163578.aspx\">\nlooking at the world through kernel-colored glasses<\/a>.\n<\/p>\n<p>\nWhile it&#8217;s true that the kernel does not ever change the value\nreturned by\n<code>Get&shy;Last&shy;Error<\/code>,\nit&#8217;s also the case that\n<i>you<\/i> might change it.\n<\/p>\n<p>\nIf you set up an exception handler, then your\nexception handler might perform operations that affect\nthe last-error code, and those changes will be visible\nafter the exception handler returns.\n(This applies to all exception handlers and filters,\nnot just ones related to memory-mapped files.)\n<\/p>\n<p>\nIf you intend to return\n<code>EXCEPTION_CONTINUE_EXECUTION<\/code>\nbecause you handled the exception,\nthen you probably should make sure to leave the last-error\ncode the way you found it.\nOtherwise, the code that you interrupted and then resumed will have had its\nlast-error code changed asynchronously.\nYou just\n<a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2011\/02\/10\/10127054.aspx\">\nsabotaged it from above<\/a>.\n<\/p>\n<pre>\n<i>\/\/ Code in italics is wrong\nLONG ExceptionFilter(LPEXCEPTION_POINTERS ExceptionPointers)\n{\n if (IsAnExceptionICanRepair(ExceptionPointers)) {\n   RepairException(ExceptionPointers);\n   \/\/ fixed up error; continuing\n   return EXCEPTION_CONTINUE_EXECUTION;\n }\n if (IsAnExceptionICanHandle(ExceptionPointers)) {\n  \/\/ We cannot repair it, but we can handle it.\n  return EXCEPTION_EXECUTE_HANDLER;\n }\n \/\/ Not our exception. Keep looking.\n return EXCEPTION_CONTINUE_SEARCH;\n}<\/i>\n<\/pre>\n<p>\nIf the <code>Is&shy;An&shy;Exception&shy;I&shy;Can&shy;Repair<\/code> function\nor\n<code>Repair&shy;Exception<\/code> function\ndoes anything that affects the last-error code,\nthen when the exception filter is executed for a repairable\nexception, the last-error code is magically changed without the\nmainline code&#8217;s knowledge.\nAll the mainline code did was execute stuff normally, and somehow\nduring a memory access or a floating point operation or some other\nseemingly-harmless action, the last-error code spontaneously changed!\n<\/p>\n<p>\nIf you are going to continue execution at the point the exception was\nraised, then you need to &#8220;put things back the way you found them&#8221;\n(except of course for the part where you repair the exception itself).\n<\/p>\n<pre>\nLONG ExceptionFilter(LPEXCEPTION_POINTERS ExceptionPointers)\n{\n <a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2011\/04\/29\/10159322.aspx\"><font COLOR=\"blue\">PreserveLastError<\/font><\/a> <font COLOR=\"blue\">preserveLastError;<\/font>\n if (IsAnExceptionICanRepair(ExceptionPointers)) {\n   RepairException(ExceptionPointers);\n   \/\/ fixed up error; continuing\n   return EXCEPTION_CONTINUE_EXECUTION;\n }\n if (IsAnExceptionICanHandle(ExceptionPointers)) {\n  \/\/ We cannot repair it, but we can handle it.\n  return EXCEPTION_EXECUTE_HANDLER;\n }\n \/\/ Not our exception. Keep looking.\n return EXCEPTION_CONTINUE_SEARCH;\n}\n<\/pre>\n<p>\n<b>Exercise<\/b>:\nWhy isn&#8217;t it important to restore the last error code if\nyou return <code>EXCEPTION_EXECUTE_HANDLER<\/code>?\n<\/p>\n<p>\n<b>Exercise<\/b>:\nIs it important to restore the last error code if you return\n<code>EXCEPTION_CONTINUE_SEARCH<\/code>?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A customer was using memory-mapped files and was looking for information as to whether access to the memory-mapped data modifies the value returned by Get&shy;Last&shy;Error. A member of the kernel team replied, &#8220;No, memory-mapped I\/O does not ever change the value returned by Get&shy;Last&shy;Error.&#8221; That answer is simultaneously correct and wrong, a case of looking [&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-8343","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>A customer was using memory-mapped files and was looking for information as to whether access to the memory-mapped data modifies the value returned by Get&shy;Last&shy;Error. A member of the kernel team replied, &#8220;No, memory-mapped I\/O does not ever change the value returned by Get&shy;Last&shy;Error.&#8221; That answer is simultaneously correct and wrong, a case of looking [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/8343","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=8343"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/8343\/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=8343"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=8343"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=8343"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}