static BOOL audin_server_open(audin_server_context* context) { audin_server* audin = (audin_server*) context; if (!audin->thread) { PULONG pSessionId = NULL; DWORD BytesReturned = 0; audin->SessionId = WTS_CURRENT_SESSION; if (WTSQuerySessionInformationA(context->vcm, WTS_CURRENT_SESSION, WTSSessionId, (LPSTR*) &pSessionId, &BytesReturned)) { audin->SessionId = (DWORD) *pSessionId; WTSFreeMemory(pSessionId); } audin->audin_channel = WTSVirtualChannelOpenEx(audin->SessionId, "AUDIO_INPUT", WTS_CHANNEL_OPTION_DYNAMIC); if (!audin->audin_channel) return FALSE; audin->stopEvent = CreateEvent(NULL, TRUE, FALSE, NULL); audin->thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) audin_server_thread_func, (void*) audin, 0, NULL); return TRUE; } return FALSE; }
/** * Function description * * @return 0 on success, otherwise a Win32 error code */ static UINT echo_server_open_channel(echo_server* echo) { DWORD Error; HANDLE hEvent; DWORD StartTick; DWORD BytesReturned = 0; PULONG pSessionId = NULL; if (WTSQuerySessionInformationA(echo->context.vcm, WTS_CURRENT_SESSION, WTSSessionId, (LPSTR*) &pSessionId, &BytesReturned) == FALSE) { WLog_ERR(TAG, "WTSQuerySessionInformationA failed!"); return ERROR_INTERNAL_ERROR; } echo->SessionId = (DWORD) * pSessionId; WTSFreeMemory(pSessionId); hEvent = WTSVirtualChannelManagerGetEventHandle(echo->context.vcm); StartTick = GetTickCount(); while (echo->echo_channel == NULL) { if (WaitForSingleObject(hEvent, 1000) == WAIT_FAILED) { Error = GetLastError(); WLog_ERR(TAG, "WaitForSingleObject failed with error %lu!", Error); return Error; } echo->echo_channel = WTSVirtualChannelOpenEx(echo->SessionId, "ECHO", WTS_CHANNEL_OPTION_DYNAMIC); if (echo->echo_channel) break; Error = GetLastError(); if (Error == ERROR_NOT_FOUND) break; if (GetTickCount() - StartTick > 5000) break; } return echo->echo_channel ? CHANNEL_RC_OK : ERROR_INTERNAL_ERROR; }
static BOOL audin_server_open(audin_server_context* context) { audin_server* audin = (audin_server*) context; if (!audin->thread) { PULONG pSessionId = NULL; DWORD BytesReturned = 0; audin->SessionId = WTS_CURRENT_SESSION; if (WTSQuerySessionInformationA(context->vcm, WTS_CURRENT_SESSION, WTSSessionId, (LPSTR*) &pSessionId, &BytesReturned)) { audin->SessionId = (DWORD) * pSessionId; WTSFreeMemory(pSessionId); } audin->audin_channel = WTSVirtualChannelOpenEx(audin->SessionId, "AUDIO_INPUT", WTS_CHANNEL_OPTION_DYNAMIC); if (!audin->audin_channel) { WLog_ERR(TAG, "WTSVirtualChannelOpenEx failed!"); return FALSE; } if (!(audin->stopEvent = CreateEvent(NULL, TRUE, FALSE, NULL))) { WLog_ERR(TAG, "CreateEvent failed!"); return FALSE; } if (!(audin->thread = CreateThread(NULL, 0, audin_server_thread_func, (void*) audin, 0, NULL))) { WLog_ERR(TAG, "CreateThread failed!"); CloseHandle(audin->stopEvent); audin->stopEvent = NULL; return FALSE; } return TRUE; } WLog_ERR(TAG, "thread already running!"); return FALSE; }
static BOOL rdpgfx_server_open(RdpgfxServerContext* context) { RdpgfxServerPrivate* priv = (RdpgfxServerPrivate*) context->priv; void* buffer = NULL; if (!priv->isOpened) { PULONG pSessionId = NULL; DWORD BytesReturned = 0; priv->SessionId = WTS_CURRENT_SESSION; if (WTSQuerySessionInformationA(context->vcm, WTS_CURRENT_SESSION, WTSSessionId, (LPSTR*) &pSessionId, &BytesReturned) == FALSE) { WLog_ERR(TAG, "WTSQuerySessionInformationA failed!"); return FALSE; } priv->SessionId = (DWORD) * pSessionId; WTSFreeMemory(pSessionId); priv->rdpgfx_channel = WTSVirtualChannelOpenEx(priv->SessionId, RDPGFX_DVC_CHANNEL_NAME, WTS_CHANNEL_OPTION_DYNAMIC); if (!priv->rdpgfx_channel) { WLog_ERR(TAG, "WTSVirtualChannelOpenEx failed!"); return FALSE; } /* Query for channel event handle */ if (!WTSVirtualChannelQuery(priv->rdpgfx_channel, WTSVirtualEventHandle, &buffer, &BytesReturned) || (BytesReturned != sizeof(HANDLE))) { WLog_ERR(TAG, "WTSVirtualChannelQuery failed " "or invalid returned size(%d)", BytesReturned); if (buffer) WTSFreeMemory(buffer); goto out_close; } CopyMemory(&priv->channelEvent, buffer, sizeof(HANDLE)); WTSFreeMemory(buffer); if (!(priv->zgfx = zgfx_context_new(TRUE))) { WLog_ERR(TAG, "Create zgfx context failed!"); goto out_close; } if (priv->ownThread) { if (!(priv->stopEvent = CreateEvent(NULL, TRUE, FALSE, NULL))) { WLog_ERR(TAG, "CreateEvent failed!"); goto out_zgfx; } if (!(priv->thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) rdpgfx_server_thread_func, (void*) context, 0, NULL))) { WLog_ERR(TAG, "CreateThread failed!"); goto out_stopEvent; } } priv->isOpened = TRUE; priv->isReady = FALSE; return TRUE; } WLog_ERR(TAG, "RDPGFX channel is already opened!"); return FALSE; out_stopEvent: CloseHandle(priv->stopEvent); priv->stopEvent = NULL; out_zgfx: zgfx_context_free(priv->zgfx); priv->zgfx = NULL; out_close: WTSVirtualChannelClose(priv->rdpgfx_channel); priv->rdpgfx_channel = NULL; priv->channelEvent = NULL; return FALSE; }
int TestWtsApiQuerySessionInformation(int argc, char* argv[]) { DWORD index, i; DWORD count; BOOL bSuccess; HANDLE hServer; LPSTR pBuffer; DWORD sessionId; DWORD bytesReturned; 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 hServer = WTS_CURRENT_SERVER_HANDLE; count = 0; pSessionInfo = NULL; bSuccess = WTSEnumerateSessionsA(hServer, 0, 1, &pSessionInfo, &count); if (!bSuccess) { printf("WTSEnumerateSessions failed: %d\n", (int) GetLastError()); return 0; } printf("WTSEnumerateSessions count: %d\n", (int) count); 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; pBuffer = NULL; bytesReturned = 0; sessionId = pSessionInfo[index].SessionId; printf("[%u] SessionId: %u State: %s (%u) WinstationName: '%s'\n", index, pSessionInfo[index].SessionId, WTSSessionStateToString(pSessionInfo[index].State), pSessionInfo[index].State, pSessionInfo[index].pWinStationName); /* WTSUserName */ bSuccess = WTSQuerySessionInformationA(hServer, sessionId, WTSUserName, &pBuffer, &bytesReturned); if (!bSuccess) { printf("WTSQuerySessionInformation WTSUserName failed: %d\n", (int) GetLastError()); return -1; } Username = (char*) pBuffer; printf("\tWTSUserName: '******'\n", Username); /* WTSDomainName */ bSuccess = WTSQuerySessionInformationA(hServer, sessionId, WTSDomainName, &pBuffer, &bytesReturned); if (!bSuccess) { printf("WTSQuerySessionInformation WTSDomainName failed: %d\n", (int) GetLastError()); return -1; } Domain = (char*) pBuffer; printf("\tWTSDomainName: '%s'\n", Domain); /* WTSConnectState */ bSuccess = WTSQuerySessionInformationA(hServer, sessionId, WTSConnectState, &pBuffer, &bytesReturned); if (!bSuccess) { printf("WTSQuerySessionInformation WTSConnectState failed: %d\n", (int) GetLastError()); return -1; } ConnectState = *((WTS_CONNECTSTATE_CLASS*) pBuffer); printf("\tWTSConnectState: %u (%s)\n", ConnectState, WTSSessionStateToString(ConnectState)); /* WTSClientBuildNumber */ bSuccess = WTSQuerySessionInformationA(hServer, sessionId, WTSClientBuildNumber, &pBuffer, &bytesReturned); if (!bSuccess) { printf("WTSQuerySessionInformation WTSClientBuildNumber failed: %d\n", (int) GetLastError()); return -1; } ClientBuildNumber = *((ULONG*) pBuffer); printf("\tWTSClientBuildNumber: %u\n", ClientBuildNumber); /* WTSClientName */ bSuccess = WTSQuerySessionInformationA(hServer, sessionId, WTSClientName, &pBuffer, &bytesReturned); if (!bSuccess) { printf("WTSQuerySessionInformation WTSClientName failed: %d\n", (int) GetLastError()); return -1; } ClientName = (char*) pBuffer; printf("\tWTSClientName: '%s'\n", ClientName); /* WTSClientProductId */ bSuccess = WTSQuerySessionInformationA(hServer, sessionId, WTSClientProductId, &pBuffer, &bytesReturned); if (!bSuccess) { printf("WTSQuerySessionInformation WTSClientProductId failed: %d\n", (int) GetLastError()); return -1; } ClientProductId = *((USHORT*) pBuffer); printf("\tWTSClientProductId: %u\n", ClientProductId); /* WTSClientHardwareId */ bSuccess = WTSQuerySessionInformationA(hServer, sessionId, WTSClientHardwareId, &pBuffer, &bytesReturned); if (!bSuccess) { printf("WTSQuerySessionInformation WTSClientHardwareId failed: %d\n", (int) GetLastError()); return -1; } ClientHardwareId = *((ULONG*) pBuffer); printf("\tWTSClientHardwareId: %u\n", ClientHardwareId); /* WTSClientAddress */ bSuccess = WTSQuerySessionInformationA(hServer, sessionId, WTSClientAddress, &pBuffer, &bytesReturned); if (!bSuccess) { printf("WTSQuerySessionInformation WTSClientAddress failed: %d\n", (int) GetLastError()); return -1; } ClientAddress = (PWTS_CLIENT_ADDRESS) pBuffer; printf("\tWTSClientAddress: AddressFamily: %u Address: ", ClientAddress->AddressFamily); for (i = 0; i < sizeof(ClientAddress->Address); i++) printf("%02X", ClientAddress->Address[i]); printf("\n"); /* WTSClientDisplay */ bSuccess = WTSQuerySessionInformationA(hServer, sessionId, WTSClientDisplay, &pBuffer, &bytesReturned); if (!bSuccess) { printf("WTSQuerySessionInformation WTSClientDisplay failed: %d\n", (int) GetLastError()); return -1; } ClientDisplay = (PWTS_CLIENT_DISPLAY) pBuffer; printf("\tWTSClientDisplay: HorizontalResolution: %u VerticalResolution: %u ColorDepth: %u\n", ClientDisplay->HorizontalResolution, ClientDisplay->VerticalResolution, ClientDisplay->ColorDepth); /* WTSClientProtocolType */ bSuccess = WTSQuerySessionInformationA(hServer, sessionId, WTSClientProtocolType, &pBuffer, &bytesReturned); if (!bSuccess) { printf("WTSQuerySessionInformation WTSClientProtocolType failed: %d\n", (int) GetLastError()); return -1; } ClientProtocolType = *((USHORT*) pBuffer); printf("\tWTSClientProtocolType: %u\n", ClientProtocolType); } WTSFreeMemory(pSessionInfo); return 0; }