• List connected users–similar to task manager


    class Program 
       { 
           [DllImport("wtsapi32.dll")] 
           static extern IntPtr WTSOpenServer([MarshalAs(UnmanagedType.LPStr)] String pServerName);
    
           [DllImport("wtsapi32.dll")] 
           static extern void WTSCloseServer(IntPtr hServer);
    
           [DllImport("wtsapi32.dll")] 
           static extern Int32 WTSEnumerateSessions( 
               IntPtr hServer, 
               [MarshalAs(UnmanagedType.U4)] Int32 Reserved, 
               [MarshalAs(UnmanagedType.U4)] Int32 Version, 
               ref IntPtr ppSessionInfo, 
               [MarshalAs(UnmanagedType.U4)] ref Int32 pCount);
    
           [DllImport("wtsapi32.dll")] 
           static extern void WTSFreeMemory(IntPtr pMemory);
    
           [DllImport("Wtsapi32.dll")] 
           static extern bool WTSQuerySessionInformation( 
               System.IntPtr hServer, int sessionId, WTS_INFO_CLASS wtsInfoClass, out System.IntPtr ppBuffer, out uint pBytesReturned);
    
           [StructLayout(LayoutKind.Sequential)] 
           private struct WTS_SESSION_INFO 
           { 
               public Int32 SessionID;
    
               [MarshalAs(UnmanagedType.LPStr)] 
               public String pWinStationName;
    
               public WTS_CONNECTSTATE_CLASS State; 
           }
    
           public enum WTS_INFO_CLASS 
           { 
               WTSInitialProgram, 
               WTSApplicationName, 
               WTSWorkingDirectory, 
               WTSOEMId, 
               WTSSessionId, 
               WTSUserName, 
               WTSWinStationName, 
               WTSDomainName, 
               WTSConnectState, 
               WTSClientBuildNumber, 
               WTSClientName, 
               WTSClientDirectory, 
               WTSClientProductId, 
               WTSClientHardwareId, 
               WTSClientAddress, 
               WTSClientDisplay, 
               WTSClientProtocolType 
           } 
           public enum WTS_CONNECTSTATE_CLASS 
           { 
               WTSActive, 
               WTSConnected, 
               WTSConnectQuery, 
               WTSShadow, 
               WTSDisconnected, 
               WTSIdle, 
               WTSListen, 
               WTSReset, 
               WTSDown, 
               WTSInit 
           }
    
           static void Main(string[] args) 
           { 
               ListUsers(Environment.MachineName); 
               Console.ReadKey(); 
           }
    
           public static IntPtr OpenServer(String Name) 
           { 
               IntPtr server = WTSOpenServer(Name); 
               return server; 
           } 
           public static void CloseServer(IntPtr ServerHandle) 
           { 
               WTSCloseServer(ServerHandle); 
           } 
           public static void ListUsers(String ServerName) 
           { 
               IntPtr serverHandle = IntPtr.Zero; 
               List<String> resultList = new List<string>(); 
               serverHandle = OpenServer(ServerName);
    
               try 
               { 
                   IntPtr SessionInfoPtr = IntPtr.Zero; 
                   IntPtr userPtr = IntPtr.Zero; 
                   IntPtr domainPtr = IntPtr.Zero; 
                   Int32 sessionCount = 0; 
                   Int32 retVal = WTSEnumerateSessions(serverHandle, 0, 1, ref SessionInfoPtr, ref sessionCount); 
                   Int32 dataSize = Marshal.SizeOf(typeof(WTS_SESSION_INFO)); 
                   Int32 currentSession = (int)SessionInfoPtr; 
                   uint bytes = 0;
    
                   if (retVal != 0) 
                   { 
                       for (int i = 0; i < sessionCount; i++) 
                       { 
                           WTS_SESSION_INFO si = (WTS_SESSION_INFO)Marshal.PtrToStructure((System.IntPtr)currentSession, typeof(WTS_SESSION_INFO)); 
                           currentSession += dataSize;
    
                           WTSQuerySessionInformation(serverHandle, si.SessionID, WTS_INFO_CLASS.WTSUserName, out userPtr, out bytes); 
                           WTSQuerySessionInformation(serverHandle, si.SessionID, WTS_INFO_CLASS.WTSDomainName, out domainPtr, out bytes);
    
                           Console.WriteLine(string.Format("SessionID : {0} Domain : {1} User : {2} StationName : {3} State : {4}", 
                               si.SessionID, Marshal.PtrToStringAnsi(domainPtr), Marshal.PtrToStringAnsi(userPtr), si.pWinStationName, si.State));
    
                           WTSFreeMemory(userPtr); 
                           WTSFreeMemory(domainPtr); 
                       }
    
                       WTSFreeMemory(SessionInfoPtr); 
                   } 
               } 
               finally 
               { 
                   CloseServer(serverHandle); 
               }
    
           }
    
       }
  • 相关阅读:
    [转载]vue中全局和局部引入批量组件方法
    [转]详解在vue-test-utils中mock全局对象
    [转]imageMagick 在nodejs中报错Error: spawn identify ENOENT的解决方案
    使用 dva 如何配置异步加载路由组件
    dva.js 上手
    [转]axios的兼容性处理
    【转】链接伪类(:hover)CSS背景图片有闪动BUG
    【转】 svn: Server sent unexpected return value (403 Forbidden) in response to CHECKOUT request for-解决方法
    vue 路由相同路径跳转报错
    新手小白第一次与后端联调
  • 原文地址:https://www.cnblogs.com/sskset/p/3607447.html
Copyright © 2020-2023  润新知