typedef std::basic_string tstring;
// Get current sessions
bool EnumSessionIds(std::vector& list)
{
list.clear();
WTS_SESSION_INFO *pSI = NULL;
DWORD dwSICount;
BOOL bRes = WTSEnumerateSessions(WTS_CURRENT_SERVER_HANDLE, 0, 1, &pSI, &dwSICount);
if (bRes == 0)
return false;
for (unsigned int i = 0; i < pbuffer =" NULL;" bres =" WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE," bres ="=" username =" pBuffer;" pbuffer =" NULL;" bres =" WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE," bres ="=" domain =" pBuffer;"> sessionIds;
bool bRes = EnumSessionIds(sessionIds);
if (!bRes)
{
// error
return 0;
}
// enum sessions
std::vector::iterator iter;
for (iter = sessionIds.begin(); iter != sessionIds.end(); iter++)
{
// print session domain
tstring domain;
GetSessionDomain(*iter, domain);
_tprintf(_T("Session Domain = %s\n"), domain.c_str());
// print session username
tstring username;
GetSessionUserName(*iter, username);
_tprintf(_T("Session UserName = %s\n"), username.c_str());
}
return 0;
}
Search this Blog:
Get UserName from SessionId
#include windows.h
#include vector
#include string
#include wtsapi32.h
#pragma comment(lib, "WtsApi32.lib")
Windows-based application over Terminal Services using WtsAPI32 -