Reputation: 1324
Do anyone know if its possible to detect if the last boot up was preceded by a power-failure in Windows ?
My reason for wanting to detect this is to know when to re-scan files I frequently modify on disk with my service.
If there is a normal startup, I can be quite sure that the shutdown went smooth and the data that was in in-memory file buffers was flushed to disk.
Upvotes: 7
Views: 361
Reputation: 70206
As a simple implementation, would CreateFile with FILE_FLAG_DELETE_ON_CLOSE (immdediately followed by FlushFileBuffers just to be sure) not work?
If the system shuts down cleanly, your application will exit and the file handle will be closed, so the OS will delete the file.
If power fails or the world ends, the OS doesn't get a chance to close any handles or delete any files. Thus, the file will still be present after the system comes up again.
Upvotes: 1
Reputation: 30013
Why not to check if your process terminated gracefully instead? Place some special marker in registry or in file system, that will mean, that your process is still working and delete it on gracefull shutdown. Then check it at every startup.
Upvotes: 2
Reputation: 91320
Detecting powerfailure will only cover one possible reason for abnormal termination. The safe way of doing this is to create a marker file when you start, then remove it when you cleanly shutdown. If the marker file exists on next startup you know that your service was not cleanly terminated.
If you want to look for powerloss only, reading the event log will tell you whether the last shutdown was unexpected.
Upvotes: 5