■ 윈도우즈 화면 잠금 여부를 구하는 방법을 보여준다.
▶ WTS_CONNECTSTATE_CLASS.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
namespace TestProject { /// <summary> /// 잠금 상태 /// </summary> public enum LockStatus { /// <summary> /// 잠금 /// </summary> Locked, /// <summary> /// 잠금 해제 /// </summary> NotLocked, /// <summary> /// 알 수 없음 /// </summary> Unknown } } |
▶ LARGE_INTEGER.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
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 } } |