Question

I have an application that every second writes a small data file and renames the previous file version. Sometimes (rarely) the system is not closed correctly (my customer knows that he should not do it, but nevertheless he does...) and the file system gets corrupted.

Indeed, when the application tries to open the data file, a message balloon appears saying Windows-corrupt file. Note that I haven't managed to reproduce the event in my test environment yet, so all I know about it is what the customer reported to me.

Now I would at least prevent my application to run and intercept the file corruption as soon as possible in order to warn the user and (if possible) automate a reboot-chkdsk sequence. Is there a way to intercept the filesystem corruption error in a C program (e.g. by trying to open the file and check the returned error code)? Note that I an not sure to be able to reproduce the situation (that's why I am asking the question!).

The systems I am using at the moment are Windows XP SP3 (32 bits) and Windows 7 64 bits.

Was it helpful?

Solution

Try calling SetErrorMode() with the SEM_FAILCRITICALERRORS flag and then check for the ERROR_FILE_CORRUPT error code (and possible also ERROR_DISK_CORRUPT) whenever you open/read/write the file.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top