Instead of counting, use GetTickCount64 function (sysinfoapi.h) | Microsoft Docs
] - it returns the milliseconds since Windows was started. It's a lot more accurate, since timers in Windows only fire "after a time period", not "on a time period". If you use timers to count seconds for example, they will drift quite significantly when the system is busy.
Remember that while WM_QUERYENDSESSION will tell you windows is about to close, it won't have any effect if the power cord is pulled out!