public static List<string> GetInteractiveUserList()
{
List<string> result = new List<string>();
IntPtr sessionInfoList = IntPtr.Zero;
int sessionCount = 0;
int retVal = SafeNativeMethods.WTSEnumerateSessions(SafeNativeMethods.WTS_CURRENT_SERVER_HANDLE, 0, 1, ref sessionInfoList, ref sessionCount);
if(retVal != 0)
{
int dataSize = Marshal.SizeOf(typeof(SafeNativeMethods.WTS_SESSION_INFO));
int currentSession = (int) sessionInfoList;
for(int x = 0; x < sessionCount; x++)
{
SafeNativeMethods.WTS_SESSION_INFO sessionInfo = (SafeNativeMethods.WTS_SESSION_INFO)Marshal.PtrToStructure((IntPtr)currentSession, typeof(SafeNativeMethods.WTS_SESSION_INFO));
currentSession += dataSize;
uint bytes = 0;
IntPtr userInfo = IntPtr.Zero;
IntPtr domainInfo = IntPtr.Zero;
bool sResult = SafeNativeMethods.WTSQuerySessionInformation(SafeNativeMethods.WTS_CURRENT_SERVER_HANDLE, sessionInfo.SessionID, SafeNativeMethods.WTS_INFO_CLASS.WTSUserName, out userInfo, out bytes);
if (!sResult)
{
LibraryLogging.Error("GetInteractiveUserList() WTSQuerySessionInformation WTSUserName Error:{0}", LastError());
}
string user = Marshal.PtrToStringAnsi(userInfo);
SafeNativeMethods.WTSFreeMemory(userInfo);
/*
sResult = SafeNativeMethods.WTSQuerySessionInformation(SafeNativeMethods.WTS_CURRENT_SERVER_HANDLE, sessionInfo.SessionID, SafeNativeMethods.WTS_INFO_CLASS.WTSDomainName, out domainInfo, out bytes);
if (!sResult)
{
LibraryLogging.Error("GetInteractiveUserList() WTSQuerySessionInformation WTSDomainName Error:{0}", LastError());
}
string domain = Marshal.PtrToStringAnsi(domainInfo);
SafeNativeMethods.WTSFreeMemory(domainInfo);
*/
/*
if (!string.IsNullOrEmpty(domain))
{
result.Add(string.Format("{0}\\{1}", domain, user));
}*/
if (!string.IsNullOrEmpty(user))
{
result.Add(string.Format("{0}\\{1}", sessionInfo.SessionID, user));
}
}
SafeNativeMethods.WTSFreeMemory(sessionInfoList);
}
//LibraryLogging.Info("InteractiveUsers:{0}", String.Join<string>(", ", result));
return result;
}