첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
본 블로그는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 블로그 콘텐츠 향상을 위해 쓰여집니다.

728x90
반응형

TestProject.zip
0.01MB

▶ WTS_INFO_CLASS.cs

namespace TestProject
{
    /// <summary>
    /// WTS 정보 클래스
    /// </summary>
    public enum WTS_INFO_CLASS
    {
        /// <summary>
        /// WTSInitialProgram
        /// </summary>
        WTSInitialProgram,

        /// <summary>
        /// WTSApplicationName
        /// </summary>
        WTSApplicationName,

        /// <summary>
        /// WTSWorkingDirectory
        /// </summary>
        WTSWorkingDirectory,

        /// <summary>
        /// WTSOEMID
        /// </summary>
        WTSOEMID,

        /// <summary>
        /// WTSSessionID
        /// </summary>
        WTSSessionID,

        /// <summary>
        /// WTSUserName
        /// </summary>
        WTSUserName,

        /// <summary>
        /// WTSWinStationName
        /// </summary>
        WTSWinStationName,

        /// <summary>
        /// WTSDomainName
        /// </summary>
        WTSDomainName,

        /// <summary>
        /// WTSConnectState
        /// </summary>
        WTSConnectState,

        /// <summary>
        /// WTSClientBuildNumber
        /// </summary>
        WTSClientBuildNumber,

        /// <summary>
        /// WTSClientName
        /// </summary>
        WTSClientName,

        /// <summary>
        /// WTSClientDirectory
        /// </summary>
        WTSClientDirectory,

        /// <summary>
        /// WTSClientProductID
        /// </summary>
        WTSClientProductID,

        /// <summary>
        /// WTSClientHardwareID
        /// </summary>
        WTSClientHardwareID,

        /// <summary>
        /// WTSClientAddress
        /// </summary>
        WTSClientAddress,

        /// <summary>
        /// WTSClientDisplay
        /// </summary>
        WTSClientDisplay,

        /// <summary>
        /// WTSClientProtocolType
        /// </summary>
        WTSClientProtocolType,

        /// <summary>
        /// WTSIdleTime
        /// </summary>
        WTSIdleTime,

        /// <summary>
        /// WTSLogonTime
        /// </summary>
        WTSLogonTime,

        /// <summary>
        /// WTSIncomingByteCount
        /// </summary>
        WTSIncomingByteCount,

        /// <summary>
        /// WTSOutgoingByteCount
        /// </summary>
        WTSOutgoingByteCount,

        /// <summary>
        /// WTSIncomingFrameCount
        /// </summary>
        WTSIncomingFrameCount,

        /// <summary>
        /// WTSOutgoingFrameCount
        /// </summary>
        WTSOutgoingFrameCount,

        /// <summary>
        /// WTSSessionInfo
        /// </summary>
        WTSSessionInfo = 24
    }
}

 

728x90

 

▶ TerminalServiceHelper.cs

using System;
using System.Runtime.InteropServices;

namespace TestProject
{
    /// <summary>
    /// 터미널 서비스 헬퍼
    /// </summary>
    public static class TerminalServiceHelper
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Import
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Private

        #region WTS 세션 정보 질의하기 - WTSQuerySessionInformation(serverHandle, sessionID, infoClass, bufferHandle, byteCountReturned)

        /// <summary>
        /// WTS 세션 정보 질의하기
        /// </summary>
        /// <param name="serverHandle">서버 핸들</param>
        /// <param name="sessionID">세션 ID</param>
        /// <param name="infoClass">WTS 정보 클래스</param>
        /// <param name="bufferHandle">버퍼 핸들</param>
        /// <param name="byteCountReturned">반환 바이트 카운트</param>
        /// <returns>처리 결과</returns>
        [DllImport("wtsapi32", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern bool WTSQuerySessionInformation(IntPtr serverHandle, int sessionID, WTS_INFO_CLASS infoClass, out IntPtr bufferHandle, out int byteCountReturned);

        #endregion
        #region WTS 메모리 해제하기 - WTSFreeMemory(memoryHandle)

        /// <summary>
        /// WTS 메모리 해제하기
        /// </summary>
        /// <param name="memoryHandle">메모리 핸들</param>
        [DllImport("wtsapi32")]
        private static extern void WTSFreeMemory(IntPtr memoryHandle);

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Public

        #region 세션 정보 질의하기 - QuerySessionInformation(sessionID, infoClass)

        /// <summary>
        /// 세션 정보 질의하기
        /// </summary>
        /// <param name="sessionID">세션 ID</param>
        /// <param name="infoClass">WTS 정보 클래스</param>
        /// <returns>세션 정보</returns>
        public static string QuerySessionInformation(int sessionID, WTS_INFO_CLASS infoClass)
        {
            string value = string.Empty;

            if(WTSQuerySessionInformation(IntPtr.Zero, sessionID, infoClass, out IntPtr bufferHandle, out int byteCountReturned))
            {
                try
                {
                    if(byteCountReturned > 0)
                    {
                        value = Marshal.PtrToStringAuto(bufferHandle);
                    }
                }
                finally
                {
                    WTSFreeMemory(bufferHandle);
                }
            }

            return value;
        }

        #endregion

        #region 사용자명 구하기 - GetUserName(sessionID)

        /// <summary>
        /// 사용자명 구하기
        /// </summary>
        /// <param name="sessionID">세션 ID</param>
        /// <returns>사용자명</returns>
        public static string GetUserName(int sessionID)
        {
            return QuerySessionInformation(sessionID, WTS_INFO_CLASS.WTSUserName);
        }

        #endregion
        #region 도메인명 구하기 - GetDomainName(sessionID)

        /// <summary>
        /// 도메인명 구하기
        /// </summary>
        /// <param name="sessionID">세션 ID</param>
        /// <returns>도메인명</returns>
        public static string GetDomainName(int sessionID)
        {
            return QuerySessionInformation(sessionID, WTS_INFO_CLASS.WTSDomainName);
        }

        #endregion
    }
}

 

300x250

 

▶ Program.cs

using System;

namespace TestProject
{
    /// <summary>
    /// 프로그램
    /// </summary>
    class Program
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Private

        #region 프로그램 시작하기 - Main()

        /// <summary>
        /// 프로그램 시작하기
        /// </summary>
        private static void Main()
        {
            int sessionID = 11;

            Console.WriteLine($"세션 ID  : {sessionID}");
            Console.WriteLine($"도메인명 : {TerminalServiceHelper.GetDomainName(sessionID)}");
            Console.WriteLine($"사용자명 : {TerminalServiceHelper.GetUserName(sessionID)}");
        }

        #endregion
    }
}
728x90
반응형
그리드형(광고전용)
Posted by 사용자 icodebroker
TAG , ,

댓글을 달아 주세요