1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
|
/*
* WTSSendMessage() command line tool
*
* Home page of code is: https://www.smartmontools.org
*
* Copyright (C) 2012-19 Christian Franke
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#define WINVER 0x0501
#define _WIN32_WINNT WINVER
char svnid[] = "$Id: wtssendmsg.c 4941 2019-08-08 18:56:36Z chrfranke $";
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <wtsapi32.h>
static int usage()
{
printf("wtssendmsg $Revision: 4941 $ - Display a message box on client desktops\n"
"Copyright (C) 2012-19 Christian Franke, www.smartmontools.org\n\n"
"Usage: wtssendmsg [-cas] [-t TIMEOUT] [-w 0..5] [-v] [\"Caption\"] \"Message\"|-\n"
" wtssendmsg -v\n\n"
" -c Console session [default]\n"
" -a Active sessions\n"
" -s Connected sessions\n"
" -t Remove message box after TIMEOUT seconds\n"
" -w Select buttons and wait for response or timeout\n"
" -v List sessions\n"
);
return 1;
}
static int getnum(const char * s)
{
char * endp;
int n = strtol(s, &endp, 10);
if (*endp)
return -1;
return n;
}
int main(int argc, const char **argv)
{
int mode = 0, timeout = 0, buttons = -1, verbose = 0, i;
for (i = 1; i < argc && argv[i][0] == '-' && argv[i][1]; i++) {
int j;
for (j = 1; argv[i][j]; j++) {
switch (argv[i][j]) {
case 'c': mode = 0; continue;
case 'a': mode = 1; continue;
case 's': mode = 2; continue;
case 't':
if (argv[i][j+1] || ++i >= argc)
return usage();
timeout = getnum(argv[i]);
if (timeout < 0)
return usage();
break;
case 'w':
if (argv[i][j+1] || ++i >= argc)
return usage();
buttons = getnum(argv[i]);
if (!(MB_OK <= buttons && buttons <= MB_RETRYCANCEL)) // 0..5
return usage();
break;
case 'v': verbose = 1; continue;
default: return usage();
}
break;
}
}
const char * message = 0, * caption = "";
char msgbuf[1024];
if (i < argc) {
if (i+1 < argc)
caption = argv[i++];
message = argv[i++];
if (i < argc)
return usage();
if (!strcmp(message, "-")) {
// Read message from stdin
// The message is also written to a Windows event log entry, so
// don't convert '\r\n' to '\n' (the MessageBox works with both)
i = 0;
DWORD size = 0;
do
if (!ReadFile(GetStdHandle(STD_INPUT_HANDLE),
msgbuf+i, sizeof(msgbuf)-1-i, &size, (OVERLAPPED*)0))
break; // May fail with ERROR_BROKEN_PIPE instead of EOF
while (size > 0 && (i += size) < (int)sizeof(msgbuf)-1);
msgbuf[i] = 0;
message = msgbuf;
}
}
else {
if (!verbose)
return usage();
}
// Get session list
WTS_SESSION_INFOA * sessions; DWORD count;
if (!WTSEnumerateSessionsA(WTS_CURRENT_SERVER_HANDLE, 0, 1, &sessions, &count)) {
fprintf(stderr, "WTSEnumerateSessions() failed\n");
return 1;
}
int status = 0;
for (i = 0; i < (int)count; i++) {
if (verbose) {
printf("Session %d (\"%s\", State=%d)%s",
i, sessions[i].pWinStationName, sessions[i].State,
(!message ? "\n" : ": "));
if (!message)
continue; // List sessions only
fflush(stdout);
}
// Check session state
if (!( !strcmpi(sessions[i].pWinStationName, "Console")
|| (mode >= 1 && sessions[i].State == WTSActive)
|| (mode >= 2 && sessions[i].State == WTSConnected))) {
if (verbose)
printf("ignored\n");
continue;
}
// Send Message
DWORD response = ~0;
if (!WTSSendMessageA(WTS_CURRENT_SERVER_HANDLE, sessions[i].SessionId,
(char *)caption, strlen(caption),
(char *)message, strlen(message),
(buttons <= MB_OK ? MB_OK|MB_ICONEXCLAMATION
: buttons|MB_DEFBUTTON2|MB_ICONQUESTION ),
timeout, &response, (buttons >= MB_OK) /*Wait?*/ )) {
status |= 0x01;
if (verbose)
printf("WTSSendMessage() failed with error=%d\n", (int)GetLastError());
else
fprintf(stderr, "Session %d (\"%s\", State=%d): WTSSendMessage() failed with error=%d\n",
i, sessions[i].pWinStationName, sessions[i].State, (int)GetLastError());
continue;
}
if (buttons >= MB_OK) {
switch (response) {
case IDOK:
case IDYES: case IDABORT: status |= 0x02; break;
case IDNO: case IDRETRY: status |= 0x04; break;
case IDCANCEL: case IDIGNORE: status |= 0x08; break;
case IDTIMEOUT: status |= 0x10; break;
default: status |= 0x01; break;
}
if (verbose)
printf("response = %d, status = 0x%02x\n", (int)response, status);
}
else {
// response == IDASYNC
if (verbose)
printf("message sent\n");
}
}
WTSFreeMemory(sessions);
return status;
}
|