3 #include <winpr/error.h>
4 #include <winpr/wtsapi.h>
5 #include <winpr/environment.h>
7 int TestWtsApiQuerySessionInformation(
int argc,
char* argv[])
11 HANDLE hServer = NULL;
14 DWORD bytesReturned = 0;
21 if (!GetEnvironmentVariableA(
"WTSAPI_LIBRARY", NULL, 0))
23 printf(
"%s: No RDS environment detected, skipping test\n", __func__);
28 hServer = WTS_CURRENT_SERVER_HANDLE;
33 bSuccess = WTSEnumerateSessionsA(hServer, 0, 1, &pSessionInfo, &count);
37 printf(
"WTSEnumerateSessions failed: %" PRIu32
"\n", GetLastError());
41 printf(
"WTSEnumerateSessions count: %" PRIu32
"\n", count);
43 for (DWORD index = 0; index < count; index++)
45 char* Username = NULL;
47 char* ClientName = NULL;
48 ULONG ClientBuildNumber = 0;
49 USHORT ClientProductId = 0;
50 ULONG ClientHardwareId = 0;
51 USHORT ClientProtocolType = 0;
54 WTS_CONNECTSTATE_CLASS ConnectState = WTSInit;
59 sessionId = pSessionInfo[index].SessionId;
61 printf(
"[%" PRIu32
"] SessionId: %" PRIu32
" State: %s (%u) WinstationName: '%s'\n", index,
62 pSessionInfo[index].SessionId, WTSSessionStateToString(pSessionInfo[index].State),
63 pSessionInfo[index].State, pSessionInfo[index].pWinStationName);
68 WTSQuerySessionInformationA(hServer, sessionId, WTSUserName, &pBuffer, &bytesReturned);
72 printf(
"WTSQuerySessionInformation WTSUserName failed: %" PRIu32
"\n", GetLastError());
76 Username = (
char*)pBuffer;
77 printf(
"\tWTSUserName: '%s'\n", Username);
81 bSuccess = WTSQuerySessionInformationA(hServer, sessionId, WTSDomainName, &pBuffer,
86 printf(
"WTSQuerySessionInformation WTSDomainName failed: %" PRIu32
"\n",
91 Domain = (
char*)pBuffer;
92 printf(
"\tWTSDomainName: '%s'\n", Domain);
96 bSuccess = WTSQuerySessionInformationA(hServer, sessionId, WTSConnectState, &pBuffer,
101 printf(
"WTSQuerySessionInformation WTSConnectState failed: %" PRIu32
"\n",
106 ConnectState = *((WTS_CONNECTSTATE_CLASS*)pBuffer);
107 printf(
"\tWTSConnectState: %u (%s)\n", ConnectState, WTSSessionStateToString(ConnectState));
111 bSuccess = WTSQuerySessionInformationA(hServer, sessionId, WTSClientBuildNumber, &pBuffer,
116 printf(
"WTSQuerySessionInformation WTSClientBuildNumber failed: %" PRIu32
"\n",
121 ClientBuildNumber = *((ULONG*)pBuffer);
122 printf(
"\tWTSClientBuildNumber: %" PRIu32
"\n", ClientBuildNumber);
126 bSuccess = WTSQuerySessionInformationA(hServer, sessionId, WTSClientName, &pBuffer,
131 printf(
"WTSQuerySessionInformation WTSClientName failed: %" PRIu32
"\n",
136 ClientName = (
char*)pBuffer;
137 printf(
"\tWTSClientName: '%s'\n", ClientName);
141 bSuccess = WTSQuerySessionInformationA(hServer, sessionId, WTSClientProductId, &pBuffer,
146 printf(
"WTSQuerySessionInformation WTSClientProductId failed: %" PRIu32
"\n",
151 ClientProductId = *((USHORT*)pBuffer);
152 printf(
"\tWTSClientProductId: %" PRIu16
"\n", ClientProductId);
156 bSuccess = WTSQuerySessionInformationA(hServer, sessionId, WTSClientHardwareId, &pBuffer,
161 printf(
"WTSQuerySessionInformation WTSClientHardwareId failed: %" PRIu32
"\n",
166 ClientHardwareId = *((ULONG*)pBuffer);
167 printf(
"\tWTSClientHardwareId: %" PRIu32
"\n", ClientHardwareId);
171 bSuccess = WTSQuerySessionInformationA(hServer, sessionId, WTSClientAddress, &pBuffer,
176 printf(
"WTSQuerySessionInformation WTSClientAddress failed: %" PRIu32
"\n",
182 printf(
"\tWTSClientAddress: AddressFamily: %" PRIu32
" Address: ",
183 ClientAddress->AddressFamily);
184 for (DWORD i = 0; i <
sizeof(ClientAddress->Address); i++)
185 printf(
"%02" PRIX8
"", ClientAddress->Address[i]);
190 bSuccess = WTSQuerySessionInformationA(hServer, sessionId, WTSClientDisplay, &pBuffer,
195 printf(
"WTSQuerySessionInformation WTSClientDisplay failed: %" PRIu32
"\n",
201 printf(
"\tWTSClientDisplay: HorizontalResolution: %" PRIu32
" VerticalResolution: %" PRIu32
202 " ColorDepth: %" PRIu32
"\n",
203 ClientDisplay->HorizontalResolution, ClientDisplay->VerticalResolution,
204 ClientDisplay->ColorDepth);
208 bSuccess = WTSQuerySessionInformationA(hServer, sessionId, WTSClientProtocolType, &pBuffer,
213 printf(
"WTSQuerySessionInformation WTSClientProtocolType failed: %" PRIu32
"\n",
218 ClientProtocolType = *((USHORT*)pBuffer);
219 printf(
"\tWTSClientProtocolType: %" PRIu16
"\n", ClientProtocolType);
222 WTSFreeMemory(pSessionInfo);