1 /***********************************************************************\
2 *                               wtsapi32.d                              *
3 *                                                                       *
4 *                       Windows API header module                       *
5 *                                                                       *
6 *                     Translated from MinGW-w64 API                     *
7 *                                                                       *
8 *                       Placed into public domain                       *
9 \***********************************************************************/
10 module windows.wtsapi32;
11 pragma(lib, "wtsapi32");
12 private import windows.w32api;
13 import windows.windef;
14 nothrow:
15 enum {
16 	WTS_CURRENT_SERVER        = null,
17 	WTS_CURRENT_SERVER_HANDLE = null,
18 	WTS_CURRENT_SERVER_NAME   = null
19 }
20 
21 enum DWORD WTS_CURRENT_SESSION = cast(DWORD) -1;
22 
23 enum {
24 	IDTIMEOUT = 32000,
25 	IDASYNC   = 32001
26 }
27 
28 enum {
29 	WTS_WSD_LOGOFF     = 0x01,
30 	WTS_WSD_SHUTDOWN   = 0x02,
31 	WTS_WSD_REBOOT     = 0x04,
32 	WTS_WSD_POWEROFF   = 0x08,
33 	WTS_WSD_FASTREBOOT = 0x10
34 }
35 
36 enum WTS_CONNECTSTATE_CLASS {
37 	WTSActive,
38 	WTSConnected,
39 	WTSConnectQuery,
40 	WTSShadow,
41 	WTSDisconnected,
42 	WTSIdle,
43 	WTSListen,
44 	WTSReset,
45 	WTSDown,
46 	WTSInit
47 }
48 
49 struct WTS_SERVER_INFOW {
50 	LPWSTR pServerName;
51 }
52 alias WTS_SERVER_INFOW* PWTS_SERVER_INFOW;
53 
54 struct WTS_SERVER_INFOA {
55 	LPSTR pServerName;
56 }
57 alias WTS_SERVER_INFOA* PWTS_SERVER_INFOA;
58 
59 version(Unicode) {
60 	alias WTS_SERVER_INFOW  WTS_SERVER_INFO;
61 	alias PWTS_SERVER_INFOW PWTS_SERVER_INFO;
62 } else {
63 	alias WTS_SERVER_INFOA  WTS_SERVER_INFO;
64 	alias PWTS_SERVER_INFOA PWTS_SERVER_INFO;
65 }
66 
67 struct WTS_SESSION_INFOW {
68 	DWORD SessionId;
69 	LPWSTR pWinStationName;
70 	WTS_CONNECTSTATE_CLASS State;
71 }
72 alias WTS_SESSION_INFOW* PWTS_SESSION_INFOW;
73 
74 struct WTS_SESSION_INFOA {
75 	DWORD SessionId;
76 	LPSTR pWinStationName;
77 	WTS_CONNECTSTATE_CLASS State;
78 }
79 alias WTS_SESSION_INFOA* PWTS_SESSION_INFOA;
80 
81 version(Unicode) {
82 	alias WTS_SESSION_INFOW  WTS_SESSION_INFO;
83 	alias PWTS_SESSION_INFOW PWTS_SESSION_INFO;
84 } else {
85 	alias WTS_SESSION_INFOA  WTS_SESSION_INFO;
86 	alias PWTS_SESSION_INFOA PWTS_SESSION_INFO;
87 }
88 
89 struct WTS_PROCESS_INFOW {
90 	DWORD SessionId;
91 	DWORD ProcessId;
92 	LPWSTR pProcessName;
93 	PSID pUserSid;
94 }
95 alias WTS_PROCESS_INFOW* PWTS_PROCESS_INFOW;
96 
97 struct WTS_PROCESS_INFOA {
98 	DWORD SessionId;
99 	DWORD ProcessId;
100 	LPSTR pProcessName;
101 	PSID pUserSid;
102 }
103 alias WTS_PROCESS_INFOA* PWTS_PROCESS_INFOA;
104 
105 version(Unicode) {
106 	alias WTS_PROCESS_INFOW  WTS_PROCESS_INFO;
107 	alias PWTS_PROCESS_INFOW PWTS_PROCESS_INFO;
108 } else {
109 	alias WTS_PROCESS_INFOA  WTS_PROCESS_INFO;
110 	alias PWTS_PROCESS_INFOA PWTS_PROCESS_INFO;
111 }
112 
113 enum {
114 	WTS_PROTOCOL_TYPE_CONSOLE,
115 	WTS_PROTOCOL_TYPE_ICA,
116 	WTS_PROTOCOL_TYPE_RDP
117 }
118 
119 enum WTS_INFO_CLASS {
120 	WTSInitialProgram,
121 	WTSApplicationName,
122 	WTSWorkingDirectory,
123 	WTSOEMId,
124 	WTSSessionId,
125 	WTSUserName,
126 	WTSWinStationName,
127 	WTSDomainName,
128 	WTSConnectState,
129 	WTSClientBuildNumber,
130 	WTSClientName,
131 	WTSClientDirectory,
132 	WTSClientProductId,
133 	WTSClientHardwareId,
134 	WTSClientAddress,
135 	WTSClientDisplay,
136 	WTSClientProtocolType,
137 	WTSIdleTime,
138 	WTSLogonTime,
139 	WTSIncomingBytes,
140 	WTSOutgoingBytes,
141 	WTSIncomingFrames,
142 	WTSOutgoingFrames,
143 	WTSClientInfo,
144 	WTSSessionInfo, // = 24
145 }
146 
147 struct WTS_CLIENT_ADDRESS {
148 	DWORD    AddressFamily;
149 	BYTE[20] Address;
150 }
151 alias WTS_CLIENT_ADDRESS* PWTS_CLIENT_ADDRESS;
152 
153 struct WTS_CLIENT_DISPLAY {
154 	DWORD HorizontalResolution;
155 	DWORD VerticalResolution;
156 	DWORD ColorDepth;
157 }
158 alias WTS_CLIENT_DISPLAY* PWTS_CLIENT_DISPLAY;
159 
160 enum WTS_CONFIG_CLASS {
161 	WTSUserConfigInitialProgram,
162 	WTSUserConfigWorkingDirectory,
163 	WTSUserConfigfInheritInitialProgram,
164 	WTSUserConfigfAllowLogonTerminalServer,
165 	WTSUserConfigTimeoutSettingsConnections,
166 	WTSUserConfigTimeoutSettingsDisconnections,
167 	WTSUserConfigTimeoutSettingsIdle,
168 	WTSUserConfigfDeviceClientDrives,
169 	WTSUserConfigfDeviceClientPrinters,
170 	WTSUserConfigfDeviceClientDefaultPrinter,
171 	WTSUserConfigBrokenTimeoutSettings,
172 	WTSUserConfigReconnectSettings,
173 	WTSUserConfigModemCallbackSettings,
174 	WTSUserConfigModemCallbackPhoneNumber,
175 	WTSUserConfigShadowingSettings,
176 	WTSUserConfigTerminalServerProfilePath,
177 	WTSUserConfigTerminalServerHomeDir,
178 	WTSUserConfigTerminalServerHomeDirDrive,
179 	WTSUserConfigfTerminalServerRemoteHomeDir
180 }
181 
182 enum {
183 	WTS_EVENT_NONE        = 0x0,
184 	WTS_EVENT_CREATE      = 0x1,
185 	WTS_EVENT_DELETE      = 0x2,
186 	WTS_EVENT_RENAME      = 0x4,
187 	WTS_EVENT_CONNECT     = 0x8,
188 	WTS_EVENT_DISCONNECT  = 0x10,
189 	WTS_EVENT_LOGON       = 0x20,
190 	WTS_EVENT_LOGOFF      = 0x40,
191 	WTS_EVENT_STATECHANGE = 0x80,
192 	WTS_EVENT_LICENSE     = 0x100,
193 	WTS_EVENT_ALL         = 0x7fffffff,
194 	WTS_EVENT_FLUSH       = 0x80000000
195 }
196 
197 enum WTS_VIRTUAL_CLASS {
198 	WTSVirtualClientData,
199 	WTSVirtualFileHandle
200 }
201 
202 version(Unicode) {
203 	alias WTSEnumerateServersW WTSEnumerateServers;
204 	alias WTSOpenServerW WTSOpenServer;
205 	alias WTSEnumerateSessionsW WTSEnumerateSessions;
206 	alias WTSEnumerateProcessesW WTSEnumerateProcesses;
207 	alias WTSQuerySessionInformationW WTSQuerySessionInformation;
208 	alias WTSQueryUserConfigW WTSQueryUserConfig;
209 	alias WTSSetUserConfigW WTSSetUserConfig;
210 	alias WTSSendMessageW WTSSendMessage;
211 } else {
212 	alias WTSEnumerateServersA WTSEnumerateServers;
213 	alias WTSOpenServerA WTSOpenServer;
214 	alias WTSEnumerateSessionsA WTSEnumerateSessions;
215 	alias WTSEnumerateProcessesA WTSEnumerateProcesses;
216 	alias WTSQuerySessionInformationA WTSQuerySessionInformation;
217 	alias WTSQueryUserConfigA WTSQueryUserConfig;
218 	alias WTSSetUserConfigA WTSSetUserConfig;
219 	alias WTSSendMessageA WTSSendMessage;
220 }
221 
222 extern(Windows) {
223 	WINBOOL WTSEnumerateServersW(LPWSTR pDomainName, DWORD Reserved, DWORD Version, PWTS_SERVER_INFOW* ppServerInfo, DWORD* pCount);
224 	WINBOOL WTSEnumerateServersA(LPSTR pDomainName, DWORD Reserved, DWORD Version, PWTS_SERVER_INFOA* ppServerInfo, DWORD* pCount);
225 	HANDLE WTSOpenServerW(LPWSTR pServerName);
226 	HANDLE WTSOpenServerA(LPSTR pServerName);
227 	VOID WTSCloseServer(HANDLE hServer);
228 	WINBOOL WTSEnumerateSessionsW(HANDLE hServer, DWORD Reserved, DWORD Version, PWTS_SESSION_INFOW* ppSessionInfo, DWORD* pCount);
229 	WINBOOL WTSEnumerateSessionsA(HANDLE hServer, DWORD Reserved, DWORD Version, PWTS_SESSION_INFOA* ppSessionInfo, DWORD* pCount);
230 	WINBOOL WTSEnumerateProcessesW(HANDLE hServer, DWORD Reserved, DWORD Version, PWTS_PROCESS_INFOW* ppProcessInfo, DWORD* pCount);
231 	WINBOOL WTSEnumerateProcessesA(HANDLE hServer, DWORD Reserved, DWORD Version, PWTS_PROCESS_INFOA* ppProcessInfo, DWORD* pCount);
232 	WINBOOL WTSTerminateProcess(HANDLE hServer, DWORD ProcessId, DWORD ExitCode);
233 	WINBOOL WTSQuerySessionInformationW(HANDLE hServer, DWORD SessionId, WTS_INFO_CLASS WTSInfoClass, LPWSTR* ppBuffer, DWORD* pBytesReturned);
234 	WINBOOL WTSQuerySessionInformationA(HANDLE hServer, DWORD SessionId, WTS_INFO_CLASS WTSInfoClass, LPSTR* ppBuffer, DWORD* pBytesReturned);
235 	WINBOOL WTSQueryUserConfigW(LPWSTR pServerName, LPWSTR pUserName, WTS_CONFIG_CLASS WTSConfigClass, LPWSTR* ppBuffer, DWORD* pBytesReturned);
236 	WINBOOL WTSQueryUserConfigA(LPSTR pServerName, LPSTR pUserName, WTS_CONFIG_CLASS WTSConfigClass, LPSTR* ppBuffer, DWORD* pBytesReturned);
237 	WINBOOL WTSSetUserConfigW(LPWSTR pServerName, LPWSTR pUserName, WTS_CONFIG_CLASS WTSConfigClass, LPWSTR pBuffer, DWORD DataLength);
238 	WINBOOL WTSSetUserConfigA(LPSTR pServerName, LPSTR pUserName, WTS_CONFIG_CLASS WTSConfigClass, LPSTR pBuffer, DWORD DataLength);
239 	WINBOOL WTSSendMessageW(HANDLE hServer, DWORD SessionId, LPWSTR pTitle, DWORD TitleLength, LPWSTR pMessage, DWORD MessageLength, DWORD Style, DWORD Timeout, DWORD* pResponse, WINBOOL bWait);
240 	WINBOOL WTSSendMessageA(HANDLE hServer, DWORD SessionId, LPSTR pTitle, DWORD TitleLength, LPSTR pMessage, DWORD MessageLength, DWORD Style, DWORD Timeout, DWORD* pResponse, WINBOOL bWait);
241 	WINBOOL WTSDisconnectSession(HANDLE hServer, DWORD SessionId, WINBOOL bWait);
242 	WINBOOL WTSLogoffSession(HANDLE hServer, DWORD SessionId, WINBOOL bWait);
243 	WINBOOL WTSShutdownSystem(HANDLE hServer, DWORD ShutdownFlag);
244 	WINBOOL WTSWaitSystemEvent(HANDLE hServer, DWORD EventMask, DWORD* pEventFlags);
245 	HANDLE WTSVirtualChannelOpen(HANDLE hServer, DWORD SessionId, LPSTR pVirtualName);
246 	WINBOOL WTSVirtualChannelClose(HANDLE hChannelHandle);
247 	WINBOOL WTSVirtualChannelRead(HANDLE hChannelHandle, ULONG TimeOut, PCHAR Buffer, ULONG BufferSize, PULONG pBytesRead);
248 	WINBOOL WTSVirtualChannelWrite(HANDLE hChannelHandle, PCHAR Buffer, ULONG Length, PULONG pBytesWritten);
249 	WINBOOL WTSVirtualChannelPurgeInput(HANDLE hChannelHandle);
250 	WINBOOL WTSVirtualChannelPurgeOutput(HANDLE hChannelHandle);
251 	WINBOOL WTSVirtualChannelQuery(HANDLE hChannelHandle, WTS_VIRTUAL_CLASS, PVOID* ppBuffer, DWORD* pBytesReturned);
252 	VOID WTSFreeMemory(PVOID pMemory);
253 
254 	WINBOOL WTSRegisterSessionNotification(HWND hWnd, DWORD dwFlags);
255 	WINBOOL WTSUnRegisterSessionNotification(HWND hWnd);
256 	WINBOOL WTSQueryUserToken(ULONG SessionId, PHANDLE phToken);
257 }
258 
259 enum {
260 	NOTIFY_FOR_ALL_SESSIONS = 1,
261 	NOTIFY_FOR_THIS_SESSION = 0
262 }
263 
264 enum {
265 	USERNAME_LENGTH       = 20,
266 	CLIENTNAME_LENGTH     = 20,
267 	CLIENTADDRESS_LENGTH  = 30,
268 	WINSTATIONNAME_LENGTH = 32,
269 	DOMAIN_LENGTH         = 17
270 }
271 
272 static if (_WIN32_WINNT >= 0x600) {
273 	struct WTSCLIENTW {
274 		WCHAR[CLIENTNAME_LENGTH + 1]   ClientName;
275 		WCHAR[DOMAIN_LENGTH + 1]   Domain;
276 		WCHAR[USERNAME_LENGTH + 1]   UserName;
277 		WCHAR[MAX_PATH + 1]   WorkDirectory;
278 		WCHAR[MAX_PATH + 1]   InitialProgram;
279 		BYTE    EncryptionLevel;
280 		ULONG   ClientAddressFamily;
281 		USHORT[CLIENTADDRESS_LENGTH + 1]  ClientAddress;
282 		USHORT  HRes;
283 		USHORT  VRes;
284 		USHORT  ColorDepth;
285 		WCHAR[MAX_PATH + 1]   ClientDirectory;
286 		ULONG   ClientBuildNumber;
287 		ULONG   ClientHardwareId;
288 		USHORT  ClientProductId;
289 		USHORT  OutBufCountHost;
290 		USHORT  OutBufCountClient;
291 		USHORT  OutBufLength;
292 		WCHAR[MAX_PATH + 1]   DeviceId;
293 	}
294 	alias WTSCLIENTW* PWTSCLIENTW;
295 
296 	struct WTSCLIENTA {
297 		CHAR[CLIENTNAME_LENGTH + 1]    ClientName;
298 		CHAR[DOMAIN_LENGTH + 1 ]    Domain;
299 		CHAR[USERNAME_LENGTH + 1]    UserName;
300 		CHAR[MAX_PATH + 1]    WorkDirectory;
301 		CHAR[MAX_PATH + 1]    InitialProgram;
302 		BYTE    EncryptionLevel;
303 		ULONG   ClientAddressFamily;
304 		USHORT[CLIENTADDRESS_LENGTH + 1]  ClientAddress;
305 		USHORT  HRes;
306 		USHORT  VRes;
307 		USHORT  ColorDepth;
308 		CHAR[MAX_PATH + 1]    ClientDirectory;
309 		ULONG   ClientBuildNumber;
310 		ULONG   ClientHardwareId;
311 		USHORT  ClientProductId;
312 		USHORT  OutBufCountHost;
313 		USHORT  OutBufCountClient;
314 		USHORT  OutBufLength;
315 		CHAR[MAX_PATH + 1]    DeviceId;
316 	}
317 	alias WTSCLIENTA* PWTSCLIENTA;
318 
319 	version(Unicode) {
320 		alias WTSCLIENTW  WTSCLIENT;
321 		alias PWTSCLIENTW PWTSCLIENT;
322 	} else {
323 		alias WTSCLIENTA  WTSCLIENT;
324 		alias PWTSCLIENTA PWTSCLIENT;
325 	}
326 
327 	struct WTSINFOW {
328 		WTS_CONNECTSTATE_CLASS State;
329 		DWORD                  SessionId;
330 		DWORD                  IncomingBytes;
331 		DWORD                  OutgoingBytes;
332 		DWORD                  IncomingCompressedBytes;
333 		DWORD                  OutgoingCompressedBytes;
334 		WCHAR[WINSTATIONNAME_LENGTH]                  WinStationName;
335 		WCHAR[DOMAIN_LENGTH]                  Domain;
336 		WCHAR[USERNAME_LENGTH+1]                  UserName;
337 		LARGE_INTEGER          ConnectTime;
338 		LARGE_INTEGER          DisconnectTime;
339 		LARGE_INTEGER          LastInputTime;
340 		LARGE_INTEGER          LogonTime;
341 		LARGE_INTEGER          CurrentTime;
342 	}
343 	alias WTSINFOW* PWTSINFOW;
344 
345 	struct WTSINFOA {
346 		WTS_CONNECTSTATE_CLASS State;
347 		DWORD                  SessionId;
348 		DWORD                  IncomingBytes;
349 		DWORD                  OutgoingBytes;
350 		DWORD                  IncomingCompressedBytes;
351 		DWORD                  OutgoingCompressedBytes;
352 		CHAR[WINSTATIONNAME_LENGTH]                   WinStationName;
353 		CHAR[DOMAIN_LENGTH]                   Domain;
354 		CHAR[USERNAME_LENGTH+1]                   UserName;
355 		LARGE_INTEGER          ConnectTime;
356 		LARGE_INTEGER          DisconnectTime;
357 		LARGE_INTEGER          LastInputTime;
358 		LARGE_INTEGER          LogonTime;
359 		LARGE_INTEGER          CurrentTime;
360 	}
361 	alias WTSINFOA* PWTSINFOA;
362 
363 	version(Unicode) {
364 		alias WTSINFOW  WTSINFO;
365 		alias PWTSINFOW PWTSINFO;
366 	} else {
367 		alias WTSINFOA  WTSINFO;
368 		alias PWTSINFOA PWTSINFO;
369 	}
370 
371 	extern(Windows) {
372 		WINBOOL WTSConnectSessionA(
373 			ULONG LogonId,
374 			ULONG TargetLogonId,
375 			PSTR   pPassword,
376 			WINBOOL bWait
377 		);
378 
379 		WINBOOL WTSConnectSessionW(
380 			ULONG LogonId,
381 			ULONG TargetLogonId,
382 			PWSTR  pPassword,
383 			WINBOOL bWait
384 		);
385 
386 		WINBOOL WTSRegisterSessionNotificationEx(
387 			HANDLE hServer,
388 			HWND hWnd,
389 			DWORD dwFlags
390 		);
391 
392 		WINBOOL WTSStartRemoteControlSessionA(
393 			LPSTR pTargetServerName,
394 			ULONG TargetLogonId,
395 			BYTE HotkeyVk,
396 			USHORT HotkeyModifiers
397 		);
398 
399 		WINBOOL WTSStartRemoteControlSessionW(
400 			LPWSTR pTargetServerName,
401 			ULONG TargetLogonId,
402 			BYTE HotkeyVk,
403 			USHORT HotkeyModifiers
404 		);
405 
406 		version(Unicode) {
407 			alias WTSStartRemoteControlSessionW WTSStartRemoteControlSession;
408 			alias WTSConnectSessionW WTSConnectSession;
409 		} else {
410 			alias WTSStartRemoteControlSessionA WTSStartRemoteControlSession;
411 			alias WTSConnectSessionA WTSConnectSession;
412 		}
413 
414 		WINBOOL WTSStopRemoteControlSession(
415 			ULONG LogonId
416 		);
417 
418 		WINBOOL WTSUnRegisterSessionNotificationEx(
419 			HANDLE hServer,
420 			HWND hWnd
421 		);
422 
423 		HANDLE WTSVirtualChannelOpenEx(
424 			DWORD SessionId,
425 			LPSTR pVirtualName,
426 			DWORD flags
427 		);
428 	} /* extern(Windows) */
429 } /* static if (_WIN32_WINNT >= 0x600) */