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

728x90
반응형
728x170

TestProject.zip
0.01MB

▶ WTS_CONNECTSTATE_CLASS.cs

namespace TestProject
{
    /// <summary>
    /// WTS 연결 상태 클래스
    /// </summary>
    public enum WTS_CONNECTSTATE_CLASS
    {
        /// <summary>
        /// WTSActive
        /// </summary>
        WTSActive,

        /// <summary>
        /// WTSConnected
        /// </summary>
        WTSConnected,

        /// <summary>
        /// WTSConnectQuery
        /// </summary>
        WTSConnectQuery,

        /// <summary>
        /// WTSShadow
        /// </summary>
        WTSShadow,

        /// <summary>
        /// WTSDisconnected
        /// </summary>
        WTSDisconnected,

        /// <summary>
        /// WTSIdle
        /// </summary>
        WTSIdle,

        /// <summary>
        /// WTSListen
        /// </summary>
        WTSListen,

        /// <summary>
        /// WTSReset
        /// </summary>
        WTSReset,

        /// <summary>
        /// WTSDown
        /// </summary>
        WTSDown,

        /// <summary>
        /// WTSInit
        /// </summary>
        WTSInit
    }
}

 

▶ 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>
        /// WTSIncomingBytes
        /// </summary>
        WTSIncomingBytes,

        /// <summary>
        /// WTSOutgoingBytes
        /// </summary>
        WTSOutgoingBytes,

        /// <summary>
        /// WTSIncomingFrames
        /// </summary>
        WTSIncomingFrames,

        /// <summary>
        /// WTSOutgoingFrames
        /// </summary>
        WTSOutgoingFrames,

        /// <summary>
        /// WTSClientInfo
        /// </summary>
        WTSClientInfo,

        /// <summary>
        /// WTSSessionInfo
        /// </summary>
        WTSSessionInfo,

        /// <summary>
        /// WTSSessionInfoEx
        /// </summary>
        WTSSessionInfoEx,

        /// <summary>
        /// WTSConfigInfo
        /// </summary>
        WTSConfigInfo,

        /// <summary>
        /// WTSValidationInfo
        /// </summary>
        WTSValidationInfo,

        /// <summary>
        /// WTSSessionAddressV4
        /// </summary>
        WTSSessionAddressV4,

        /// <summary>
        /// WTSIsRemoteSession
        /// </summary>
        WTSIsRemoteSession
    }
}

 

▶ LockStatus.cs

namespace TestProject
{
    /// <summary>
    /// 잠금 상태
    /// </summary>
    public enum LockStatus
    {
        /// <summary>
        /// 잠금
        /// </summary>
        Locked,

        /// <summary>
        /// 잠금 해제
        /// </summary>
        NotLocked,

        /// <summary>
        /// 알 수 없음
        /// </summary>
        Unknown
    }
}

 

▶ LARGE_INTEGER.cs

using System.Runtime.InteropServices;

namespace TestProject
{
    /// <summary>
    /// 배정도 정수
    /// </summary>
    [StructLayout(LayoutKind.Explicit)]
    public struct LARGE_INTEGER
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region Field

        /// <summary>
        /// 하위 파트
        /// </summary>
        [FieldOffset(0)]
        public uint LowPart;

        /// <summary>
        /// 상위 파트
        /// </summary>
        [FieldOffset(4)]
        public int HighPart;

        /// <summary>
        /// 쿼드 파트
        /// </summary>
        [FieldOffset(0)]
        public long QuadPart;

        #endregion
    }
}

 

▶ WTSINFOEX_LEVEL1_W.cs

using System.Runtime.InteropServices;

namespace TestProject
{
    /// <summary>
    /// WTS 정보 확장 레벨 1 (유니코드)
    /// </summary>
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    public struct WTSINFOEX_LEVEL1_W
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region Field

        /// <summary>
        /// 세션 ID
        /// </summary>
        public int SessionID;

        /// <summary>
        /// 세션 상태
        /// </summary>
        public WTS_CONNECTSTATE_CLASS SessionState;

        /// <summary>
        /// 세션 플래그
        /// </summary>
        public int SessionFlag;

        /// <summary>
        /// WIN 스테이션명
        /// </summary>
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 33)]
        public string WinStationName;

        /// <summary>
        /// 사용자명
        /// </summary>
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 21)]
        public string UserName;

        /// <summary>
        /// 도메인명
        /// </summary>
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 18)]
        public string DomainName;

        /// <summary>
        /// 로그온 시간
        /// </summary>
        public LARGE_INTEGER LogonTime;

        /// <summary>
        /// 연결 시간
        /// </summary>
        public LARGE_INTEGER ConnectTime;

        /// <summary>
        /// 연결 중단 시간
        /// </summary>
        public LARGE_INTEGER DisconnectTime;

        /// <summary>
        /// 최근 입력 시간
        /// </summary>
        public LARGE_INTEGER LastInputTime;

        /// <summary>
        /// 현재 시간
        /// </summary>
        public LARGE_INTEGER CurrentTime;

        /// <summary>
        /// 수신 바이트 카운트
        /// </summary>
        public uint IncomingByteCount;

        /// <summary>
        /// 송신 바이트 카운트
        /// </summary>
        public uint OutgoingByteCount;

        /// <summary>
        /// 수신 프레임 카운트
        /// </summary>
        public uint IncomingFrameCount;

        /// <summary>
        /// 송신 프레임 카운트
        /// </summary>
        public uint OutgoingFrameCount;

        /// <summary>
        /// 수신 압축 바이트 카운트
        /// </summary>
        public uint IncomingCompressedByteCount;

        /// <summary>
        /// 송신 압축 바이트 카운트
        /// </summary>
        public uint OutgoingCompressedByteCount;

        #endregion
    }
}

 

▶ WTSINFOEX_LEVEL_W.cs

using System.Runtime.InteropServices;

namespace TestProject
{
    /// <summary>
    /// WTS 정보 확장 레벨 (유니코드)
    /// </summary>
    [StructLayout(LayoutKind.Sequential)]
    public struct WTSINFOEX_LEVEL_W
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region Field

        /// <summary>
        /// WTS 정보 확장 레벨 1
        /// </summary>
        public WTSINFOEX_LEVEL1_W WTSInfoExLevel1;

        #endregion
    }
}

 

▶ WTSINFOEXW.cs

using System.Runtime.InteropServices;

namespace TestProject
{
    /// <summary>
    /// WTS 정보 확장 (유니코드)
    /// </summary>
    [StructLayout(LayoutKind.Sequential)]
    public struct WTSINFOEXW
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region Field

        /// <summary>
        /// 레벨
        /// </summary>
        public int Level;
        
        /// <summary>
        /// 데이터
        /// </summary>
        public WTSINFOEX_LEVEL_W Data;

        #endregion
    }
}

 

▶ LockScreenHelper.cs

using System;
using System.Runtime.InteropServices;

namespace TestProject
{
    /// <summary>
    /// 잠금 화면 헬퍼
    /// </summary>
    public static class LockScreenHelper
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Import
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Private

        #region WTS 활성 콘솔 세션 ID 구하기 - WTSGetActiveConsoleSessionId()

        /// <summary>
        /// WTS 활성 콘솔 세션 ID 구하기
        /// </summary>
        /// <returns>처리 결과</returns>
        [DllImport("kernel32", CharSet = CharSet.Unicode)]
        private static extern uint WTSGetActiveConsoleSessionId();

        #endregion
        #region WTS 세션 정보 질의하기 - WTSQuerySessionInformationW(serverHandle, sessionID, wtsInfoClass, bufferHandle, byteCountReturned)

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

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

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

        #endregion

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

        #region 잠금 여부 구하기 - IsLocked()

        /// <summary>
        /// 잠금 여부 구하기
        /// </summary>
        /// <returns>잠금 여부</returns>
        public static LockStatus IsLocked()
        {
            uint   sessionID         = WTSGetActiveConsoleSessionId();
            uint   byteCountReturned = 0;
            int    flag              = 0;
            IntPtr bufferHandle      = IntPtr.Zero;

            WTSQuerySessionInformationW(IntPtr.Zero, sessionID, WTS_INFO_CLASS.WTSSessionInfoEx, ref bufferHandle, ref byteCountReturned);

            WTSINFOEXW info = (WTSINFOEXW)Marshal.PtrToStructure(bufferHandle, typeof(WTSINFOEXW));
 
            if(info.Level == 1)
            {
                flag = info.Data.WTSInfoExLevel1.SessionFlag;
            }

            switch(flag)
            {
                case 0  : return LockStatus.Locked;
                case 1  : return LockStatus.NotLocked;
                default : return LockStatus.Unknown;
            }
        }

        #endregion
    }
}

 

▶ Program.cs

using System;
using System.Threading;

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

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

        /// <summary>
        /// 프로그램 시작하기
        /// </summary>
        private static void Main()
        {
            while(true)
            {
                Console.WriteLine(LockScreenHelper.IsLocked());

                Thread.Sleep(1000);
            }
        }

        #endregion
    }
}
728x90
반응형
그리드형
Posted by 사용자 icodebroker

댓글을 달아 주세요