private static WTS_CLIENT_INFO LoadClientInfoForSession(IntPtr ptrOpenedServer, int active_session) { int returned = 0; IntPtr str = IntPtr.Zero; WTS_CLIENT_INFO ClientInfo = new WTS_CLIENT_INFO(); ClientInfo.WTSStationName = ""; ClientInfo.WTSClientName = ""; ClientInfo.Address = new byte[6]; ClientInfo.Address[2] = 0; ClientInfo.Address[3] = 0; ClientInfo.Address[4] = 0; ClientInfo.Address[5] = 0; ClientInfo.WTSClientName = GetString(ptrOpenedServer, active_session, WTS_INFO_CLASS.WTSClientName); ClientInfo.WTSStationName = GetString(ptrOpenedServer, active_session, WTS_INFO_CLASS.WTSWinStationName); ClientInfo.WTSDomainName = GetString(ptrOpenedServer, active_session, WTS_INFO_CLASS.WTSDomainName); //Get client IP address IntPtr addr = System.IntPtr.Zero; if (WTSQuerySessionInformation2(ptrOpenedServer, active_session, WTS_INFO_CLASS.WTSClientAddress, ref addr, ref returned) == true) { _WTS_CLIENT_ADDRESS obj = new _WTS_CLIENT_ADDRESS(); obj = (_WTS_CLIENT_ADDRESS)Marshal.PtrToStructure(addr, obj.GetType()); ClientInfo.Address[2] = obj.Address[2]; ClientInfo.Address[3] = obj.Address[3]; ClientInfo.Address[4] = obj.Address[4]; ClientInfo.Address[5] = obj.Address[5]; } return(ClientInfo); }
private static WTS_CLIENT_INFO LoadClientInfoForSession(IntPtr ptrOpenedServer, int active_session) { int returned = 0; IntPtr str = IntPtr.Zero; WTS_CLIENT_INFO ClientInfo = new WTS_CLIENT_INFO { WTSStationName = "", WTSClientName = "", Address = new byte[6] }; ClientInfo.Address[2] = 0; ClientInfo.Address[3] = 0; ClientInfo.Address[4] = 0; ClientInfo.Address[5] = 0; ClientInfo.WTSClientName = GetString(ptrOpenedServer, active_session, WTS_INFO_CLASS.WTSClientName); ClientInfo.WTSStationName = GetString(ptrOpenedServer, active_session, WTS_INFO_CLASS.WTSWinStationName); ClientInfo.WTSDomainName = GetString(ptrOpenedServer, active_session, WTS_INFO_CLASS.WTSDomainName); //Get client IP address IntPtr addr = IntPtr.Zero; if (WTSQuerySessionInformation2(ptrOpenedServer, active_session, WTS_INFO_CLASS.WTSClientAddress, ref addr, ref returned)) { _WTS_CLIENT_ADDRESS obj = new _WTS_CLIENT_ADDRESS(); obj = (_WTS_CLIENT_ADDRESS) Marshal.PtrToStructure(addr, obj.GetType()); ClientInfo.Address[2] = obj.Address[2]; ClientInfo.Address[3] = obj.Address[3]; ClientInfo.Address[4] = obj.Address[4]; ClientInfo.Address[5] = obj.Address[5]; } return ClientInfo; }
public static TerminalServer GetSessions(string ServerName) { TerminalServer Data = new TerminalServer(); Data.ServerName = ServerName; IntPtr ptrOpenedServer = IntPtr.Zero; try { ptrOpenedServer = WTSOpenServer(ServerName); if (ptrOpenedServer == System.IntPtr.Zero) { Data.IsATerminalServer = false; return(Data); } Data.ServerPointer = ptrOpenedServer; Data.IsATerminalServer = true; Int32 FRetVal; IntPtr ppSessionInfo = IntPtr.Zero; Int32 Count = 0; try { FRetVal = WTSEnumerateSessions(ptrOpenedServer, 0, 1, ref ppSessionInfo, ref Count); if (FRetVal != 0) { Data.Sessions = new List <Session>(); WTS_SESSION_INFO[] sessionInfo = new WTS_SESSION_INFO[Count + 1]; int i; System.IntPtr session_ptr; for (i = 0; i <= Count - 1; i++) { session_ptr = new System.IntPtr(ppSessionInfo.ToInt32() + (i * Marshal.SizeOf(sessionInfo[i]))); sessionInfo[i] = (WTS_SESSION_INFO)Marshal.PtrToStructure(session_ptr, typeof(WTS_SESSION_INFO)); Session s = new Session(); s.SessionID = sessionInfo[i].SessionID; s.State = (ConnectionStates)(int)sessionInfo[i].State; s.WindowsStationName = sessionInfo[i].pWinStationName; s.ServerName = ServerName; Data.Sessions.Add(s); } WTSFreeMemory(ppSessionInfo); strSessionsInfo[] tmpArr = new strSessionsInfo[sessionInfo.GetUpperBound(0) + 1]; for (i = 0; i <= tmpArr.GetUpperBound(0); i++) { tmpArr[i].SessionID = sessionInfo[i].SessionID; tmpArr[i].StationName = sessionInfo[i].pWinStationName; tmpArr[i].ConnectionState = GetConnectionState(sessionInfo[i].State); //MessageBox.Show(tmpArr(i).StationName & " " & tmpArr(i).SessionID & " " & tmpArr(i).ConnectionState) } // ERROR: Not supported in C#: ReDimStatement } } catch (Exception ex) { Logging.Error("Get Sessions Inner", ex); Data.Errors.Add(ex.Message + "\r\n" + System.Runtime.InteropServices.Marshal.GetLastWin32Error()); } } catch (Exception ex) { Logging.Info("Get Sessions Outer", ex); Data.Errors.Add(ex.Message + "\r\n" + System.Runtime.InteropServices.Marshal.GetLastWin32Error()); } WTS_PROCESS_INFO[] plist = WTSEnumerateProcesses(ptrOpenedServer, Data); //Get ProcessID of TS Session that executed this TS Session Int32 active_process = GetCurrentProcessId(); Int32 active_session = 0; bool success1 = ProcessIdToSessionId(active_process, ref active_session); if (active_session <= 0) { success1 = false; } if (Data != null && Data.Sessions != null) { foreach (Session s in Data.Sessions) { if (s.Client == null) { s.Client = new Client(); } WTS_CLIENT_INFO ClientInfo = LoadClientInfoForSession(Data.ServerPointer, s.SessionID); s.Client.Address = ClientInfo.Address; s.Client.AddressFamily = ClientInfo.AddressFamily; s.Client.ClientName = ClientInfo.WTSClientName; s.Client.DomianName = ClientInfo.WTSDomainName; s.Client.StationName = ClientInfo.WTSStationName; s.Client.Status = ClientInfo.WTSStatus; s.Client.UserName = ClientInfo.WTSUserName; s.IsTheActiveSession = false; if (success1 && s.SessionID == active_session) { s.IsTheActiveSession = true; } } } WTSCloseServer(ptrOpenedServer); return(Data); }
/// <param name="pServer"></param> /// <param name="pSessionInfo"></param> private TerminalSessionInfo GetSessionInfo(IntPtr pServer, IntPtr pSessionInfo) { int iCurrent = (int)pSessionInfo; uint iReturned = 0; WTS_CLIENT_ADDRESS oClientAddres = new WTS_CLIENT_ADDRESS(); WTS_CLIENT_DISPLAY oClientDisplay = new WTS_CLIENT_DISPLAY(); WTS_CLIENT_PROTOCOL_TYPE oClientProtocol = WTS_CLIENT_PROTOCOL_TYPE.UNKNOWN; WTS_CLIENT_INFO oClientInfo = new WTS_CLIENT_INFO(); WTSINFO oWtsInfo = new WTSINFO(); string sIPAddress = string.Empty; string sUserName = string.Empty, sClientName = string.Empty; string sDomain = string.Empty; string sClientApplicationDirectory = string.Empty; TerminalSessionInfo retval = new TerminalSessionInfo(0); // Get session info structure WTS_SESSION_INFO oSessionInfo = (WTS_SESSION_INFO)Marshal.PtrToStructure((System.IntPtr)iCurrent, typeof(WTS_SESSION_INFO)); //Get the IP address of the Terminal Services User IntPtr pAddress = IntPtr.Zero; if (WTSQuerySessionInformation(pServer, oSessionInfo.iSessionID, WTS_INFO_CLASS.WTSClientAddress, out pAddress, out iReturned) == true) { oClientAddres = (WTS_CLIENT_ADDRESS)Marshal.PtrToStructure(pAddress, oClientAddres.GetType()); sIPAddress = oClientAddres.bAddress[2] + "." + oClientAddres.bAddress[3] + "." + oClientAddres.bAddress[4] + "." + oClientAddres.bAddress[5]; } //Get the User Name of the Terminal Services User if (WTSQuerySessionInformation(pServer, oSessionInfo.iSessionID, WTS_INFO_CLASS.WTSUserName, out pAddress, out iReturned) == true) { sUserName = Marshal.PtrToStringAnsi(pAddress); } //Get the Client Name of the Terminal Services User if (WTSQuerySessionInformation(pServer, oSessionInfo.iSessionID, WTS_INFO_CLASS.WTSClientName, out pAddress, out iReturned) == true) { sClientName = Marshal.PtrToStringAnsi(pAddress); } //Get the Domain Name of the Terminal Services User if (WTSQuerySessionInformation(pServer, oSessionInfo.iSessionID, WTS_INFO_CLASS.WTSDomainName, out pAddress, out iReturned) == true) { sDomain = Marshal.PtrToStringAnsi(pAddress); } //Get the Display Information of the Terminal Services User if (WTSQuerySessionInformation(pServer, oSessionInfo.iSessionID, WTS_INFO_CLASS.WTSClientDisplay, out pAddress, out iReturned) == true) { oClientDisplay = (WTS_CLIENT_DISPLAY)Marshal.PtrToStructure(pAddress, oClientDisplay.GetType()); } //Get the Application Directory of the Terminal Services User if (WTSQuerySessionInformation(pServer, oSessionInfo.iSessionID, WTS_INFO_CLASS.WTSClientDirectory, out pAddress, out iReturned) == true) { sClientApplicationDirectory = Marshal.PtrToStringAnsi(pAddress); } //Get protocol type if (WTSQuerySessionInformation(pServer, oSessionInfo.iSessionID, WTS_INFO_CLASS.WTSClientProtocolType, out pAddress, out iReturned) == true) { oClientProtocol = (WTS_CLIENT_PROTOCOL_TYPE)Marshal.ReadInt16(pAddress); } //Get client info if (WTSQuerySessionInformation(pServer, oSessionInfo.iSessionID, WTS_INFO_CLASS.WTSClientInfo, out pAddress, out iReturned) == true) { oClientInfo = (WTS_CLIENT_INFO)Marshal.PtrToStructure(pAddress, oClientInfo.GetType()); //sUserName = String.IsNullOrEmpty(sUserName) ? oClientInfo.UserName : sUserName; } //Get WTS info if (WTSQuerySessionInformation(pServer, oSessionInfo.iSessionID, WTS_INFO_CLASS.WTSSessionInfo, out pAddress, out iReturned) == true) { oWtsInfo = (WTSINFO)Marshal.PtrToStructure(pAddress, oWtsInfo.GetType()); } // fill result retval.SessionInfo = oSessionInfo; //retval.SessionInfo.oState = oSessionInfo.oState; //retval.SessionInfo.sWinsWorkstationName = oSessionInfo.sWinsWorkstationName == null ? "" : oSessionInfo.sWinsWorkstationName; retval.UserName = sUserName == null ? "" : sUserName; retval.ClientMachineName = sClientName == null ? "" : sClientName; retval.ClientIPAddress = sIPAddress == null ? "" : sIPAddress; retval.Domain = sDomain == null ? "" : sDomain; retval.ProtocolType = oClientProtocol; retval.ClientInfo = oClientInfo; retval.WtsInfo = oWtsInfo; return(retval); }