{"id":26063,"date":"2007-07-11T10:00:00","date_gmt":"2007-07-11T10:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2007\/07\/11\/how-to-check-for-errors-from-setfilepointer\/"},"modified":"2007-07-11T10:00:00","modified_gmt":"2007-07-11T10:00:00","slug":"how-to-check-for-errors-from-setfilepointer","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20070711-00\/?p=26063\/","title":{"rendered":"How to check for errors from SetFilePointer"},"content":{"rendered":"<p>The <a href=\"http:\/\/msdn.microsoft.com\/library\/en-us\/fileio\/fs\/setfilepointer.asp\"> <code>SetFilePointer<\/code><\/a> function reports an error in two different ways, depending on whether you passed <code>NULL<\/code> as the <code>lpDistanceToMoveHigh<\/code> parameter. The documentation in MSDN is correct, but I&#8217;ve discovered that people prefer when I <a href=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2006\/03\/02\/542115.aspx\"> restate the same facts in a different way<\/a>, so here comes the tabular version of the documentation.<\/p>\n<table border=\"1\" cellpadding=\"3\" cellspacing=\"0\" style=\"border: 0pt;border-collapse: collapse\">\n<tr>\n<th><\/th>\n<th>If <code>lpDistanceToMoveHigh == NULL<\/code><\/th>\n<th>If <code>lpDistanceToMoveHigh != NULL<\/code><\/th>\n<\/tr>\n<tr>\n<th>If success<\/th>\n<td><code>retVal != INVALID_SET_FILE_POINTER<code><\/code><\/code><\/td>\n<td><code>retVal != INVALID_SET_FILE_POINTER ||<br \/>         GetLastError() == ERROR_SUCCESS<\/code><\/td>\n<\/tr>\n<tr>\n<th>If failed<\/th>\n<td><code>retVal == INVALID_SET_FILE_POINTER<code><\/code><\/code><\/td>\n<td><code>retVal == INVALID_SET_FILE_POINTER &amp;&amp;<br \/>         GetLastError() != ERROR_SUCCESS<\/code><\/td>\n<\/tr>\n<\/table>\n<p> I&#8217;d show some sample code, but the documentation in MSDN already contains sample code both for the <code>lpDistancetoMoveHigh == NULL<\/code> case as well as the <code>lpDistancetoMoveHigh != NULL<\/code> case.\n A common mistake is calling <code>GetLastError<\/code> even if the return value is not <code>INVALID_SET_FILE_POINTER<\/code>. In other words, people ignore the whole <code>retVal == INVALID_SET_FILE_POINTER<\/code> part of the &#8220;did the function succeed or fail?&#8221; test. Just because <code>GetLastError()<\/code> returned an error code doesn&#8217;t mean that the <code>SetFilePointer<\/code> function failed. The return value must also have been <code>INVALID_SET_FILE_POINTER<\/code>. I will admit that the documentation in MSDN could be clearer on this point, but the sample code hopefully resolves any lingering ambiguity.\n But why does <code>SetFilePointer<\/code> use such a wacky way of reporting errors when <code>lpDistanceToMoveHigh<\/code> is non-<code>NULL<\/code>? The MSDN documentation also explains this detail: If the file size is greater than 4GB, then <code>INVALID_SET_FILE_POINTER<\/code> is a valid value for the low-order 32 bits of the file position. For example, if you moved the pointer to position 0x00000001`FFFFFFFF, then <code>*lpDistanceToMoveHigh<\/code> will be set to the high-order 32 bits of the result (1), and the return value is the low-order 32 bits of the result (0xFFFFFFFF, which happens to be the numerical value of <code>INVALID_SET_FILE_POINTER<\/code>). In that case (and only in that case) does the system need to use <code>SetLastError(ERROR_SUCCESS)<\/code> to tell you, &#8220;No, that value is perfectly fine. It&#8217;s just a coincidence that it happens to be equal to <code>INVALID_SET_FILE_POINTER<\/code>&#8220;.\n Why not call <code>SetLastError(ERROR_SUCCESS)<\/code> on all success paths, and not just the ones where the low-order 32 bits of the result happen to be 0xFFFFFFFF? That&#8217;s just a general convention of Win32: If a function succeeds, it is not required to call <code>SetLastError(ERROR_SUCESS)<\/code>. The success return value tells you that the function succeeded. The exception to this convention is if the return value is ambiguous, as we have here when the low-order 32 bits of the result happen to be 0xFFFFFFFF.\n You might argue that this was a stupid convention, But what&#8217;s done is done and until time travel has been perfected, you just have to live with the past. (Mind you, UNIX uses the same convention with the <code>errno<\/code> variable. Only if the previous function call failed is the value of <code>errno<\/code> defined.)\n Looking back on it, the designers of <code>SetFilePointer<\/code> were being a bit too clever. They tried to merge 32-bit and 64-bit file management into a single function. &#8220;It&#8217;s generic!&#8221; The problem with this is that you have to check for errors in two different ways depending on whether you were using the 32-bit variation or the 64-bit variation. Fortunately, the kernel folks realized that their cleverness backfired and they came up with a new function, <code>SetFilePointerEx<\/code>. That function produces a 64-bit value directly, and the return value is a simple <code>BOOL<\/code>, which makes checking for success or failure a snap.<\/p>\n<p> Exercise: What&#8217;s the deal with the <code>GetFileSize<\/code> function? <\/p>\n","protected":false},"excerpt":{"rendered":"<p>The SetFilePointer function reports an error in two different ways, depending on whether you passed NULL as the lpDistanceToMoveHigh parameter. The documentation in MSDN is correct, but I&#8217;ve discovered that people prefer when I restate the same facts in a different way, so here comes the tabular version of the documentation. If lpDistanceToMoveHigh == NULL [&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-26063","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>The SetFilePointer function reports an error in two different ways, depending on whether you passed NULL as the lpDistanceToMoveHigh parameter. The documentation in MSDN is correct, but I&#8217;ve discovered that people prefer when I restate the same facts in a different way, so here comes the tabular version of the documentation. If lpDistanceToMoveHigh == NULL [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/26063","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=26063"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/26063\/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=26063"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=26063"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=26063"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}