I'm writing a simple system service to switch on and off the remote desktop access of some computers depending on a schedule. This includes notifying, then logging off remote users that would be connected when the system switches to local-only.
To notify the user, I'm using the WTSSendMessage
function, which can display a dialog box inside a given session. It also allows specifying a timeout—which is very important in this case so that if nobody acknowledges the message within a given timeout, the service can simply force the logoff.
The problem I'm facing is that WTSSendMessage
(at least on the latest Windows 10, version 2004, and on Windows Server 2019 version 1809, the two systems on which I tested) doesn't return IDTIMEOUT
properly when the dialog box only has the OK button:
WTSSendMessage(WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION, SESSION_STOP_TITLE,
strlen(SESSION_STOP_TITLE), SESSION_STOP_MSG, strlen(SESSION_STOP_MSG),
MB_OK | MB_ICONEXCLAMATION, SESSION_WARNING_TIMEOUT, &resp, TRUE);
In this case, the resp
variable always contains IDOK
, independently on whether the user clicks OK or the timeout expires. The documentation doesn't say anything about a special case with a single OK button.
If instead of MB_OK
I use anything else (e.g., MB_YESNO
), the call properly works and resp
will contain either IDYES
, IDNO
, or IDTIMEOUT
depending on what happened. I also tried explicitly using the unicode (WTSSendMessageW
) version of the function just in case, but it doesn't change anything.
Is this a bug within the terminal services API? I didn't find any references to this behaviour anywhere. Would there be a workaround (besides rephrasing the message and using a Yes/No or OK/Cancel boutton couple)?