How to get Windows user name from SessionID? How to get Windows user name from SessionID? windows windows

How to get Windows user name from SessionID?


There seems to be no .NET integrated method for this.
So this is the current solution, using Windows Terminal services API:

    [DllImport("Wtsapi32.dll")]    private static extern bool WTSQuerySessionInformation(IntPtr hServer, int sessionId, WtsInfoClass wtsInfoClass, out System.IntPtr ppBuffer, out int pBytesReturned);    [DllImport("Wtsapi32.dll")]    private static extern void WTSFreeMemory(IntPtr pointer);    public enum WtsInfoClass    {        WTSInitialProgram,        WTSApplicationName,        WTSWorkingDirectory,        WTSOEMId,        WTSSessionId,        WTSUserName,        WTSWinStationName,        WTSDomainName,        WTSConnectState,        WTSClientBuildNumber,        WTSClientName,        WTSClientDirectory,        WTSClientProductId,        WTSClientHardwareId,        WTSClientAddress,        WTSClientDisplay,        WTSClientProtocolType,        WTSIdleTime,        WTSLogonTime,        WTSIncomingBytes,        WTSOutgoingBytes,        WTSIncomingFrames,        WTSOutgoingFrames,        WTSClientInfo,        WTSSessionInfo,    }    public static string GetUsernameBySessionId(int sessionId, bool prependDomain) {        IntPtr buffer;        int strLen;        string username = "SYSTEM";        if (WTSQuerySessionInformation(IntPtr.Zero, sessionId, WtsInfoClass.WTSUserName, out buffer, out strLen) && strLen > 1) {            username = Marshal.PtrToStringAnsi(buffer);            WTSFreeMemory(buffer);            if (prependDomain) {                if (WTSQuerySessionInformation(IntPtr.Zero, sessionId, WtsInfoClass.WTSDomainName, out buffer, out strLen) && strLen > 1) {                    username = Marshal.PtrToStringAnsi(buffer) + "\\" + username;                    WTSFreeMemory(buffer);                }            }        }        return username;    }