2 * WTSSendMessage() command line tool
4 * Home page of code is: http://smartmontools.sourceforge.net
6 * Copyright (C) 2012 Christian Franke <smartmontools-support@lists.sourceforge.net>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2, or (at your option)
13 * You should have received a copy of the GNU General Public License
14 * (for example COPYING); If not, see <http://www.gnu.org/licenses/>.
19 #define _WIN32_WINNT WINVER
21 char svnid
[] = "$Id: wtssendmsg.c 3714 2012-11-24 16:34:47Z chrfranke $";
26 #define WIN32_LEAN_AND_MEAN
33 printf("wtssendmsg $Revision: 3714 $ - Display a message box on client desktops\n"
34 "Copyright (C) 2012 Christian Franke, smartmontools.org\n\n"
35 "Usage: wtssendmsg [-cas] [-v] [\"Caption\"] \"Message\"|-\n"
37 " -c Console session [default]\n"
38 " -a Active sessions\n"
39 " -s Connected sessions\n"
45 int main(int argc
, const char **argv
)
47 int mode
= 0, verbose
= 0, status
= 0, i
;
48 const char * message
= 0, * caption
= "";
50 WTS_SESSION_INFOA
* sessions
; DWORD count
;
52 for (i
= 1; i
< argc
&& argv
[i
][0] == '-' && argv
[i
][1]; i
++) {
54 for (j
= 1; argv
[i
][j
]; j
++)
56 case 'c': mode
= 0; break;
57 case 'a': mode
= 1; break;
58 case 's': mode
= 2; break;
59 case 'v': verbose
= 1; break;
60 default: return usage();
72 if (!strcmp(message
, "-")) {
73 // Read message from stdin
74 i
= fread(msgbuf
, 1, sizeof(msgbuf
)-1, stdin
);
89 if (!WTSEnumerateSessionsA(WTS_CURRENT_SERVER_HANDLE
, 0, 1, &sessions
, &count
)) {
90 fprintf(stderr
, "WTSEnumerateSessions() failed\n");
94 for (i
= 0; i
< (int)count
; i
++) {
97 printf("Session %d (\"%s\", State=%d)%s",
98 i
, sessions
[i
].pWinStationName
, sessions
[i
].State
,
99 (!message
? "\n" : ": "));
101 continue; // List sessions only
105 if ( !strcmpi(sessions
[i
].pWinStationName
, "Console")
106 || (mode
>= 1 && sessions
[i
].State
== WTSActive
)
107 || (mode
>= 2 && sessions
[i
].State
== WTSConnected
)) {
109 // Send Message, don't wait for OK button
111 if (WTSSendMessageA(WTS_CURRENT_SERVER_HANDLE
, sessions
[i
].SessionId
,
112 (char *)caption
, strlen(caption
),
113 (char *)message
, strlen(message
),
114 MB_OK
|MB_ICONEXCLAMATION
, 0 /*Timeout*/,
115 &result
, FALSE
/*!Wait*/)) {
117 printf("message sent\n");
122 printf("WTSSendMessage() failed with error=%d\n", (int)GetLastError());
124 fprintf(stderr
, "Session %d (\"%s\", State=%d): WTSSendMessage() failed with error=%d\n",
125 i
, sessions
[i
].pWinStationName
, sessions
[i
].State
, (int)GetLastError());
134 WTSFreeMemory(sessions
);