2014-03-01 09:32:23 +04:00
|
|
|
|
|
|
|
#include <winpr/crt.h>
|
|
|
|
#include <winpr/error.h>
|
|
|
|
#include <winpr/wtsapi.h>
|
2016-05-30 18:54:59 +03:00
|
|
|
#include <winpr/environment.h>
|
2014-03-01 09:32:23 +04:00
|
|
|
|
|
|
|
int TestWtsApiQuerySessionInformation(int argc, char* argv[])
|
|
|
|
{
|
2016-05-30 18:54:59 +03:00
|
|
|
DWORD index, i;
|
2014-03-18 00:39:35 +04:00
|
|
|
DWORD count;
|
2014-03-01 09:32:23 +04:00
|
|
|
BOOL bSuccess;
|
|
|
|
HANDLE hServer;
|
|
|
|
LPSTR pBuffer;
|
|
|
|
DWORD sessionId;
|
|
|
|
DWORD bytesReturned;
|
2016-05-30 18:54:59 +03:00
|
|
|
PWTS_SESSION_INFOA pSessionInfo;
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
if (!GetEnvironmentVariableA("WTSAPI_LIBRARY", NULL, 0))
|
|
|
|
{
|
|
|
|
printf("%s: No RDS environment detected, skipping test\n", __FUNCTION__);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
2014-03-01 09:32:23 +04:00
|
|
|
|
|
|
|
hServer = WTS_CURRENT_SERVER_HANDLE;
|
|
|
|
|
2014-03-18 00:39:35 +04:00
|
|
|
count = 0;
|
|
|
|
pSessionInfo = NULL;
|
2014-03-01 09:32:23 +04:00
|
|
|
|
2016-05-30 18:54:59 +03:00
|
|
|
bSuccess = WTSEnumerateSessionsA(hServer, 0, 1, &pSessionInfo, &count);
|
2014-03-01 09:32:23 +04:00
|
|
|
|
|
|
|
if (!bSuccess)
|
|
|
|
{
|
2016-12-14 00:47:08 +03:00
|
|
|
printf("WTSEnumerateSessions failed: %"PRIu32"\n", GetLastError());
|
2014-04-28 05:54:21 +04:00
|
|
|
return 0;
|
2014-03-01 09:32:23 +04:00
|
|
|
}
|
|
|
|
|
2016-12-14 00:47:08 +03:00
|
|
|
printf("WTSEnumerateSessions count: %"PRIu32"\n", count);
|
2014-03-18 00:39:35 +04:00
|
|
|
|
|
|
|
for (index = 0; index < count; index++)
|
|
|
|
{
|
|
|
|
char* Username;
|
|
|
|
char* Domain;
|
|
|
|
char* ClientName;
|
|
|
|
ULONG ClientBuildNumber;
|
|
|
|
USHORT ClientProductId;
|
|
|
|
ULONG ClientHardwareId;
|
|
|
|
USHORT ClientProtocolType;
|
|
|
|
PWTS_CLIENT_DISPLAY ClientDisplay;
|
|
|
|
PWTS_CLIENT_ADDRESS ClientAddress;
|
|
|
|
WTS_CONNECTSTATE_CLASS ConnectState;
|
|
|
|
|
2014-05-22 21:50:01 +04:00
|
|
|
pBuffer = NULL;
|
|
|
|
bytesReturned = 0;
|
|
|
|
|
2014-03-18 00:39:35 +04:00
|
|
|
sessionId = pSessionInfo[index].SessionId;
|
|
|
|
|
2016-12-14 00:47:08 +03:00
|
|
|
printf("[%"PRIu32"] SessionId: %"PRIu32" State: %s (%u) WinstationName: '%s'\n",
|
2016-05-30 18:54:59 +03:00
|
|
|
index,
|
|
|
|
pSessionInfo[index].SessionId,
|
|
|
|
WTSSessionStateToString(pSessionInfo[index].State),
|
|
|
|
pSessionInfo[index].State,
|
|
|
|
pSessionInfo[index].pWinStationName);
|
2014-03-18 00:39:35 +04:00
|
|
|
|
|
|
|
/* WTSUserName */
|
|
|
|
|
2014-05-22 21:50:01 +04:00
|
|
|
bSuccess = WTSQuerySessionInformationA(hServer, sessionId, WTSUserName, &pBuffer, &bytesReturned);
|
2014-03-18 00:39:35 +04:00
|
|
|
|
|
|
|
if (!bSuccess)
|
|
|
|
{
|
2016-12-14 00:47:08 +03:00
|
|
|
printf("WTSQuerySessionInformation WTSUserName failed: %"PRIu32"\n", GetLastError());
|
2014-03-18 00:39:35 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
Username = (char*) pBuffer;
|
2016-05-30 18:54:59 +03:00
|
|
|
printf("\tWTSUserName: '%s'\n", Username);
|
2014-03-18 00:39:35 +04:00
|
|
|
|
|
|
|
/* WTSDomainName */
|
|
|
|
|
2014-05-22 21:50:01 +04:00
|
|
|
bSuccess = WTSQuerySessionInformationA(hServer, sessionId, WTSDomainName, &pBuffer, &bytesReturned);
|
2014-03-18 00:39:35 +04:00
|
|
|
|
|
|
|
if (!bSuccess)
|
|
|
|
{
|
2016-12-14 00:47:08 +03:00
|
|
|
printf("WTSQuerySessionInformation WTSDomainName failed: %"PRIu32"\n", GetLastError());
|
2014-03-18 00:39:35 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
Domain = (char*) pBuffer;
|
2016-05-30 18:54:59 +03:00
|
|
|
printf("\tWTSDomainName: '%s'\n", Domain);
|
2014-03-18 00:39:35 +04:00
|
|
|
|
|
|
|
/* WTSConnectState */
|
|
|
|
|
2014-05-22 21:50:01 +04:00
|
|
|
bSuccess = WTSQuerySessionInformationA(hServer, sessionId, WTSConnectState, &pBuffer, &bytesReturned);
|
2014-03-18 00:39:35 +04:00
|
|
|
|
|
|
|
if (!bSuccess)
|
|
|
|
{
|
2016-12-14 00:47:08 +03:00
|
|
|
printf("WTSQuerySessionInformation WTSConnectState failed: %"PRIu32"\n", GetLastError());
|
2014-03-18 00:39:35 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ConnectState = *((WTS_CONNECTSTATE_CLASS*) pBuffer);
|
2016-05-30 18:54:59 +03:00
|
|
|
printf("\tWTSConnectState: %u (%s)\n", ConnectState, WTSSessionStateToString(ConnectState));
|
2014-03-18 00:39:35 +04:00
|
|
|
|
|
|
|
/* WTSClientBuildNumber */
|
|
|
|
|
2014-05-22 21:50:01 +04:00
|
|
|
bSuccess = WTSQuerySessionInformationA(hServer, sessionId, WTSClientBuildNumber, &pBuffer, &bytesReturned);
|
2014-03-18 00:39:35 +04:00
|
|
|
|
|
|
|
if (!bSuccess)
|
|
|
|
{
|
2016-12-14 00:47:08 +03:00
|
|
|
printf("WTSQuerySessionInformation WTSClientBuildNumber failed: %"PRIu32"\n", GetLastError());
|
2014-03-18 00:39:35 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ClientBuildNumber = *((ULONG*) pBuffer);
|
2016-12-14 00:47:08 +03:00
|
|
|
printf("\tWTSClientBuildNumber: %"PRIu32"\n", ClientBuildNumber);
|
2014-03-18 00:39:35 +04:00
|
|
|
|
|
|
|
/* WTSClientName */
|
|
|
|
|
2014-05-22 21:50:01 +04:00
|
|
|
bSuccess = WTSQuerySessionInformationA(hServer, sessionId, WTSClientName, &pBuffer, &bytesReturned);
|
2014-03-18 00:39:35 +04:00
|
|
|
|
|
|
|
if (!bSuccess)
|
|
|
|
{
|
2016-12-14 00:47:08 +03:00
|
|
|
printf("WTSQuerySessionInformation WTSClientName failed: %"PRIu32"\n", GetLastError());
|
2014-03-18 00:39:35 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ClientName = (char*) pBuffer;
|
2016-05-30 18:54:59 +03:00
|
|
|
printf("\tWTSClientName: '%s'\n", ClientName);
|
2014-03-18 00:39:35 +04:00
|
|
|
|
|
|
|
/* WTSClientProductId */
|
|
|
|
|
2014-05-22 21:50:01 +04:00
|
|
|
bSuccess = WTSQuerySessionInformationA(hServer, sessionId, WTSClientProductId, &pBuffer, &bytesReturned);
|
2014-03-18 00:39:35 +04:00
|
|
|
|
|
|
|
if (!bSuccess)
|
|
|
|
{
|
2016-12-14 00:47:08 +03:00
|
|
|
printf("WTSQuerySessionInformation WTSClientProductId failed: %"PRIu32"\n", GetLastError());
|
2014-03-18 00:39:35 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ClientProductId = *((USHORT*) pBuffer);
|
2016-12-14 00:47:08 +03:00
|
|
|
printf("\tWTSClientProductId: %"PRIu16"\n", ClientProductId);
|
2014-03-18 00:39:35 +04:00
|
|
|
|
|
|
|
/* WTSClientHardwareId */
|
|
|
|
|
2014-05-22 21:50:01 +04:00
|
|
|
bSuccess = WTSQuerySessionInformationA(hServer, sessionId, WTSClientHardwareId, &pBuffer, &bytesReturned);
|
2014-03-18 00:39:35 +04:00
|
|
|
|
|
|
|
if (!bSuccess)
|
|
|
|
{
|
2016-12-14 00:47:08 +03:00
|
|
|
printf("WTSQuerySessionInformation WTSClientHardwareId failed: %"PRIu32"\n", GetLastError());
|
2014-03-18 00:39:35 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ClientHardwareId = *((ULONG*) pBuffer);
|
2016-12-14 00:47:08 +03:00
|
|
|
printf("\tWTSClientHardwareId: %"PRIu32"\n", ClientHardwareId);
|
2014-03-18 00:39:35 +04:00
|
|
|
|
|
|
|
/* WTSClientAddress */
|
|
|
|
|
2014-05-22 21:50:01 +04:00
|
|
|
bSuccess = WTSQuerySessionInformationA(hServer, sessionId, WTSClientAddress, &pBuffer, &bytesReturned);
|
2014-03-18 00:39:35 +04:00
|
|
|
|
|
|
|
if (!bSuccess)
|
|
|
|
{
|
2016-12-14 00:47:08 +03:00
|
|
|
printf("WTSQuerySessionInformation WTSClientAddress failed: %"PRIu32"\n", GetLastError());
|
2014-03-18 00:39:35 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ClientAddress = (PWTS_CLIENT_ADDRESS) pBuffer;
|
2016-12-14 00:47:08 +03:00
|
|
|
printf("\tWTSClientAddress: AddressFamily: %"PRIu32" Address: ",
|
2016-05-30 18:54:59 +03:00
|
|
|
ClientAddress->AddressFamily);
|
|
|
|
for (i = 0; i < sizeof(ClientAddress->Address); i++)
|
2016-12-14 00:47:08 +03:00
|
|
|
printf("%02"PRIX8"", ClientAddress->Address[i]);
|
2016-05-30 18:54:59 +03:00
|
|
|
printf("\n");
|
|
|
|
|
2014-03-18 00:39:35 +04:00
|
|
|
|
|
|
|
/* WTSClientDisplay */
|
|
|
|
|
2014-05-22 21:50:01 +04:00
|
|
|
bSuccess = WTSQuerySessionInformationA(hServer, sessionId, WTSClientDisplay, &pBuffer, &bytesReturned);
|
2014-03-18 00:39:35 +04:00
|
|
|
|
|
|
|
if (!bSuccess)
|
|
|
|
{
|
2016-12-14 00:47:08 +03:00
|
|
|
printf("WTSQuerySessionInformation WTSClientDisplay failed: %"PRIu32"\n", GetLastError());
|
2014-03-18 00:39:35 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ClientDisplay = (PWTS_CLIENT_DISPLAY) pBuffer;
|
2016-12-14 00:47:08 +03:00
|
|
|
printf("\tWTSClientDisplay: HorizontalResolution: %"PRIu32" VerticalResolution: %"PRIu32" ColorDepth: %"PRIu32"\n",
|
2016-05-30 18:54:59 +03:00
|
|
|
ClientDisplay->HorizontalResolution, ClientDisplay->VerticalResolution,
|
|
|
|
ClientDisplay->ColorDepth);
|
2014-03-18 00:39:35 +04:00
|
|
|
|
|
|
|
/* WTSClientProtocolType */
|
|
|
|
|
2014-05-22 21:50:01 +04:00
|
|
|
bSuccess = WTSQuerySessionInformationA(hServer, sessionId, WTSClientProtocolType, &pBuffer, &bytesReturned);
|
2014-03-18 00:39:35 +04:00
|
|
|
|
|
|
|
if (!bSuccess)
|
|
|
|
{
|
2016-12-14 00:47:08 +03:00
|
|
|
printf("WTSQuerySessionInformation WTSClientProtocolType failed: %"PRIu32"\n", GetLastError());
|
2014-03-18 00:39:35 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ClientProtocolType = *((USHORT*) pBuffer);
|
2016-12-14 00:47:08 +03:00
|
|
|
printf("\tWTSClientProtocolType: %"PRIu16"\n", ClientProtocolType);
|
2014-03-18 00:39:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
WTSFreeMemory(pSessionInfo);
|
|
|
|
|
2014-03-01 09:32:23 +04:00
|
|
|
return 0;
|
|
|
|
}
|