Click here to Skip to main content
15,899,679 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi everyone. I have written a code to enumerate all user list but

problem 1:
it is displaying names of active users only on local system. but when i take remote of other system and run my code it retrieves nothing.whereas i want the list of all users including active users,disconnected users,console users

problem 2:
i also want to display the states of user (like active,disconnected,console).

how do i acheive this can any one help?


#include "stdafx.h"



#ifndef _WIN32_WINNT		      // Allow use of features specific to Windows X or later.                   
#define _WIN32_WINNT 0x0501	// Change this to the appropriate value to target other versions of Windows.
#endif	

#include <windows.h>
#include <WtsApi32.h>
#include <tchar.h>


#pragma comment(lib, "WtsApi32.lib")




int _tmain(int argc, TCHAR** argv)
{
	LPTSTR szUserName = NULL;
	LPTSTR szDomainName = NULL;
	DWORD dwLen = 0;
	BOOL bStatus = WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE,
		WTS_CURRENT_SESSION,
		WTSDomainName,
		&szDomainName,
		&dwLen);
	if (bStatus)
	{
		bStatus = WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE,
			WTS_CURRENT_SESSION,
			WTSUserName,
			&szUserName,
			&dwLen);
		if (bStatus)
		{
			DWORD cbUpn = _tcslen(szUserName) + 1 + _tcslen(szDomainName);
			LPTSTR szUpn = (LPTSTR)LocalAlloc(0, (cbUpn + 1) * sizeof(TCHAR));

		/*	_tcscpy(szUpn, szUserName);
			_tcscat(szUpn, _T("@"));
			_tcscat(szUpn, szDomainName);
*/
			_tprintf(_T("UPN = %s\n"), szUserName);

			LocalFree(szUpn);
			WTSFreeMemory(szUserName);
		}
		else
		{
			_tprintf(_T("WTSQuerySessionInformation on WTSUserName failed with error 0x%.8X\n"), GetLastError());
		}
		WTSFreeMemory(szDomainName);
	}
	else
	{
		_tprintf(_T("WTSQuerySessionInformation on WTSDomainName failed with error 0x%.8X\n"), GetLastError());
	}
	return 0;
}
Posted

1 solution

WTS_CURRENT_SESSION is invalid when accessing remote servers:
If WTS_CURRENT_SESSION is specified when querying session information on a remote server, the returned session information will be inconsistent. Do not use the returned data.

(See[^] documentation).
To perform this task on a remote server you have to provide a valid sessionId. You can get it for a call to WTSEnumerateSessions[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900