WM_TIMECHANGE broadcast strangeness...

I’m currently writing and testing a simple class that watches for system time changes. This is to allow an app to adjust some timers that it sets for absolute times in the future if the system time changes.

The code’s fairly simple. The system automatically broadcasts a WM_TIMECHANGE message to all top level windows whenever something adjusts the system time or the time zone so all you need to do is write code that creates a hidden window and deals with the message.

Testing this was proving somewhat tricky. On my Vista development box I found that trying to broadcast my own WM_TIMECHANGE message didn’t work. I tried various methods along the lines of: SendMessage(HWND_BROADCAST, WM_TIMECHANGE, 0, 0); and none of them resulted in my window getting the message. I did some googling and tried SendNotifyMessage(), PostMessage(), and using HWND_TOPMOST rather than HWND_BROADCAST as various web sources suggested and none of them worked on this platform. Actually changing the time, by calling SetSystemTime() did cause a WM_TIMECHANGE to be broadcast but my test process needed to run with the SE_SYSTEMTIME_NAME privilege enabled which was a pain and even then it just seems wrong to actually have to call SetSystemTime() (even if it was being called with the result of a call to GetSystemTime()).

Well, it seems that you CAN broadcast a WM_TIMECHANGE message if you do the broadcasting yourself using EnumWindows() where your enumProc simply calls SendMessage(hwnd, WM_TIMECHANGE, 0, 0); for each enumerated top-level window… This is actually a better solution as I don’t really want to broadcast the message to all processes I just want to send it to all top-level windows in my process and I can do that by using GetWindowThreadProcessId() to filter the hWnds that I actually send to.

I get the feeling that this may stop working in the future though as I can’t see any reason why broadcasting the ‘proper’ way is blocked and manually broadcasting would be allowed. Therefore I assume that, perhaps, my manual broadcast method has been missed and broadcasting of this message should actually be forbidden entirely… Unfortunately I can’t find any docs which explain why my original attempts to broadcast the message don’t work…