If you're just trying to make sure something either happens completely or not at all, try the transaction functions (CreateFileTransacted et al) since its what they're for.
Note that your ShowError function returns the error value after it has possibly been modified by WriteConsole/WriteFile or whatever Windows API fprintf uses to produce console output. You have no guarantee these don't change the error value on their success paths, only that the error value makes sense if they fail.
Actually I did take steps to verify that fprintf() doesn't write to GetLastError() ... interrestingly it seems to respect the stderr handle. If I intentionally make a bad format string or send it a NULL pointer, it will write to GetLastError(), but not when it succeeds. So far that's been reliable and at that point in the code I would not dream of making a decision on the value... it's just reporting it.
An example of what I'm looking for is is in the screen shot... When a path isn't found because the path doesn't exist it returns 2 (ERROR_FILE_NOT_FOUND) over a range of different functions (PathFileExists(), PathIsDirectory(), GetFileAttributesEx(), etc.) which in my case means the trap worked as expected. For almost everything else (badly formatted paths, network authorization, etc.) it returns a range of different values... all of which are treated as a bail now signal.
For Example:
// target is current directory
if (dstpath[0] == 0)
dstpath[0] = L'.';
// fully resolve target path
{ WCHAR tmp[MAX_PATH] = {0};
GetFullPathName(dstpath,MAX_PATH,tmp,NULL);
lstrcpy(dstpath,tmp); }
// ensure target is reachable
if (!PathIsDirectory(dstpath))
ShowError(L"Target path is unreachable");
However, there are spots in the code where what comes back is very important to what happens next...
For Example:
// test for orphans
int IsOrphaned(PWCHAR SrcPath,PWCHAR File)
{ WCHAR stmp[MAX_PATH];
PathCombine(stmp,SrcPath,File);
if (PathFileExists(stmp))
return 0; // not orphaned
if (GetLastError() == ERROR_FILE_NOT_FOUND)
return 1; // orphaned
// can't trust result
ShowError(L"Error tracking orphaned files"); }
There are spots like this in the project where I seem to have little choice but to rely upon the value from GetLastError() to make sure it's a trustworthy result... that is, in this case, I end up treating ERROR_FILE_NOT_FOUND as a success signal.
I know this isn't the best programming practice but unless someone has a better way of telling me if a file exists or not, without risk of a false negative, I'm kinda stuck with it. I've tried FindFirstFile(), GetFileAttributes() and others and pretty much all of them have to be double checked in the same way.