FreeRDP
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Modules Pages
TestWtsApiEnumerateSessions.c
1
2#include <winpr/crt.h>
3#include <winpr/error.h>
4#include <winpr/wtsapi.h>
5#include <winpr/environment.h>
6
7int TestWtsApiEnumerateSessions(int argc, char* argv[])
8{
9 DWORD count = 0;
10 BOOL bSuccess = 0;
11 HANDLE hServer = NULL;
12 PWTS_SESSION_INFOA pSessionInfo = NULL;
13
14 WINPR_UNUSED(argc);
15 WINPR_UNUSED(argv);
16
17#ifndef _WIN32
18 if (!GetEnvironmentVariableA("WTSAPI_LIBRARY", NULL, 0))
19 {
20 printf("%s: No RDS environment detected, skipping test\n", __func__);
21 return 0;
22 }
23#endif
24
25 hServer = WTS_CURRENT_SERVER_HANDLE;
26
27 count = 0;
28 pSessionInfo = NULL;
29
30 bSuccess = WTSEnumerateSessionsA(hServer, 0, 1, &pSessionInfo, &count);
31
32 if (!bSuccess)
33 {
34 printf("WTSEnumerateSessions failed: %" PRIu32 "\n", GetLastError());
35 return 0;
36 }
37
38 printf("WTSEnumerateSessions count: %" PRIu32 "\n", count);
39
40 for (DWORD index = 0; index < count; index++)
41 {
42 printf("[%" PRIu32 "] SessionId: %" PRIu32 " WinstationName: '%s' State: %s (%u)\n", index,
43 pSessionInfo[index].SessionId, pSessionInfo[index].pWinStationName,
44 WTSSessionStateToString(pSessionInfo[index].State), pSessionInfo[index].State);
45 }
46
47 WTSFreeMemory(pSessionInfo);
48
49 return 0;
50}