FreeRDP
TestWtsApiQuerySessionInformation.c
1 
2 #include <winpr/crt.h>
3 #include <winpr/error.h>
4 #include <winpr/wtsapi.h>
5 #include <winpr/environment.h>
6 
7 int TestWtsApiQuerySessionInformation(int argc, char* argv[])
8 {
9  DWORD count = 0;
10  BOOL bSuccess = 0;
11  HANDLE hServer = NULL;
12  LPSTR pBuffer = NULL;
13  DWORD sessionId = 0;
14  DWORD bytesReturned = 0;
15  PWTS_SESSION_INFOA pSessionInfo = NULL;
16 
17  WINPR_UNUSED(argc);
18  WINPR_UNUSED(argv);
19 
20 #ifndef _WIN32
21  if (!GetEnvironmentVariableA("WTSAPI_LIBRARY", NULL, 0))
22  {
23  printf("%s: No RDS environment detected, skipping test\n", __func__);
24  return 0;
25  }
26 #endif
27 
28  hServer = WTS_CURRENT_SERVER_HANDLE;
29 
30  count = 0;
31  pSessionInfo = NULL;
32 
33  bSuccess = WTSEnumerateSessionsA(hServer, 0, 1, &pSessionInfo, &count);
34 
35  if (!bSuccess)
36  {
37  printf("WTSEnumerateSessions failed: %" PRIu32 "\n", GetLastError());
38  return 0;
39  }
40 
41  printf("WTSEnumerateSessions count: %" PRIu32 "\n", count);
42 
43  for (DWORD index = 0; index < count; index++)
44  {
45  char* Username = NULL;
46  char* Domain = NULL;
47  char* ClientName = NULL;
48  ULONG ClientBuildNumber = 0;
49  USHORT ClientProductId = 0;
50  ULONG ClientHardwareId = 0;
51  USHORT ClientProtocolType = 0;
52  PWTS_CLIENT_DISPLAY ClientDisplay = NULL;
53  PWTS_CLIENT_ADDRESS ClientAddress = NULL;
54  WTS_CONNECTSTATE_CLASS ConnectState = WTSInit;
55 
56  pBuffer = NULL;
57  bytesReturned = 0;
58 
59  sessionId = pSessionInfo[index].SessionId;
60 
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);
64 
65  /* WTSUserName */
66 
67  bSuccess =
68  WTSQuerySessionInformationA(hServer, sessionId, WTSUserName, &pBuffer, &bytesReturned);
69 
70  if (!bSuccess)
71  {
72  printf("WTSQuerySessionInformation WTSUserName failed: %" PRIu32 "\n", GetLastError());
73  return -1;
74  }
75 
76  Username = (char*)pBuffer;
77  printf("\tWTSUserName: '%s'\n", Username);
78 
79  /* WTSDomainName */
80 
81  bSuccess = WTSQuerySessionInformationA(hServer, sessionId, WTSDomainName, &pBuffer,
82  &bytesReturned);
83 
84  if (!bSuccess)
85  {
86  printf("WTSQuerySessionInformation WTSDomainName failed: %" PRIu32 "\n",
87  GetLastError());
88  return -1;
89  }
90 
91  Domain = (char*)pBuffer;
92  printf("\tWTSDomainName: '%s'\n", Domain);
93 
94  /* WTSConnectState */
95 
96  bSuccess = WTSQuerySessionInformationA(hServer, sessionId, WTSConnectState, &pBuffer,
97  &bytesReturned);
98 
99  if (!bSuccess)
100  {
101  printf("WTSQuerySessionInformation WTSConnectState failed: %" PRIu32 "\n",
102  GetLastError());
103  return -1;
104  }
105 
106  ConnectState = *((WTS_CONNECTSTATE_CLASS*)pBuffer);
107  printf("\tWTSConnectState: %u (%s)\n", ConnectState, WTSSessionStateToString(ConnectState));
108 
109  /* WTSClientBuildNumber */
110 
111  bSuccess = WTSQuerySessionInformationA(hServer, sessionId, WTSClientBuildNumber, &pBuffer,
112  &bytesReturned);
113 
114  if (!bSuccess)
115  {
116  printf("WTSQuerySessionInformation WTSClientBuildNumber failed: %" PRIu32 "\n",
117  GetLastError());
118  return -1;
119  }
120 
121  ClientBuildNumber = *((ULONG*)pBuffer);
122  printf("\tWTSClientBuildNumber: %" PRIu32 "\n", ClientBuildNumber);
123 
124  /* WTSClientName */
125 
126  bSuccess = WTSQuerySessionInformationA(hServer, sessionId, WTSClientName, &pBuffer,
127  &bytesReturned);
128 
129  if (!bSuccess)
130  {
131  printf("WTSQuerySessionInformation WTSClientName failed: %" PRIu32 "\n",
132  GetLastError());
133  return -1;
134  }
135 
136  ClientName = (char*)pBuffer;
137  printf("\tWTSClientName: '%s'\n", ClientName);
138 
139  /* WTSClientProductId */
140 
141  bSuccess = WTSQuerySessionInformationA(hServer, sessionId, WTSClientProductId, &pBuffer,
142  &bytesReturned);
143 
144  if (!bSuccess)
145  {
146  printf("WTSQuerySessionInformation WTSClientProductId failed: %" PRIu32 "\n",
147  GetLastError());
148  return -1;
149  }
150 
151  ClientProductId = *((USHORT*)pBuffer);
152  printf("\tWTSClientProductId: %" PRIu16 "\n", ClientProductId);
153 
154  /* WTSClientHardwareId */
155 
156  bSuccess = WTSQuerySessionInformationA(hServer, sessionId, WTSClientHardwareId, &pBuffer,
157  &bytesReturned);
158 
159  if (!bSuccess)
160  {
161  printf("WTSQuerySessionInformation WTSClientHardwareId failed: %" PRIu32 "\n",
162  GetLastError());
163  return -1;
164  }
165 
166  ClientHardwareId = *((ULONG*)pBuffer);
167  printf("\tWTSClientHardwareId: %" PRIu32 "\n", ClientHardwareId);
168 
169  /* WTSClientAddress */
170 
171  bSuccess = WTSQuerySessionInformationA(hServer, sessionId, WTSClientAddress, &pBuffer,
172  &bytesReturned);
173 
174  if (!bSuccess)
175  {
176  printf("WTSQuerySessionInformation WTSClientAddress failed: %" PRIu32 "\n",
177  GetLastError());
178  return -1;
179  }
180 
181  ClientAddress = (PWTS_CLIENT_ADDRESS)pBuffer;
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]);
186  printf("\n");
187 
188  /* WTSClientDisplay */
189 
190  bSuccess = WTSQuerySessionInformationA(hServer, sessionId, WTSClientDisplay, &pBuffer,
191  &bytesReturned);
192 
193  if (!bSuccess)
194  {
195  printf("WTSQuerySessionInformation WTSClientDisplay failed: %" PRIu32 "\n",
196  GetLastError());
197  return -1;
198  }
199 
200  ClientDisplay = (PWTS_CLIENT_DISPLAY)pBuffer;
201  printf("\tWTSClientDisplay: HorizontalResolution: %" PRIu32 " VerticalResolution: %" PRIu32
202  " ColorDepth: %" PRIu32 "\n",
203  ClientDisplay->HorizontalResolution, ClientDisplay->VerticalResolution,
204  ClientDisplay->ColorDepth);
205 
206  /* WTSClientProtocolType */
207 
208  bSuccess = WTSQuerySessionInformationA(hServer, sessionId, WTSClientProtocolType, &pBuffer,
209  &bytesReturned);
210 
211  if (!bSuccess)
212  {
213  printf("WTSQuerySessionInformation WTSClientProtocolType failed: %" PRIu32 "\n",
214  GetLastError());
215  return -1;
216  }
217 
218  ClientProtocolType = *((USHORT*)pBuffer);
219  printf("\tWTSClientProtocolType: %" PRIu16 "\n", ClientProtocolType);
220  }
221 
222  WTSFreeMemory(pSessionInfo);
223 
224  return 0;
225 }