■ ExitWindowsEx API 함수를 사용해 윈도우즈를 종료하고 재부팅하고 로그오프하는 방법을 보여준다.
▶ WIN32Helper.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 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 |
using System; using System.Runtime.InteropServices; namespace TestProject { /// <summary> /// WIN32 헬퍼 /// </summary> public static class WIN32Helper { //////////////////////////////////////////////////////////////////////////////////////////////////// Import ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 현재 프로세스 구하기 - GetCurrentProcess() /// <summary> /// 현재 프로세스 구하기 /// </summary> /// <returns>현재 프로세스 핸들</returns> [DllImport("kernel32", ExactSpelling = true)] private static extern IntPtr GetCurrentProcess(); #endregion #region 프로세스 토큰 열기 - OpenProcessToken(processHandle, desiredAccess, tokenHandle) /// <summary> /// 프로세스 토큰 열기 /// </summary> /// <param name="processHandle">프로세스 핸들</param> /// <param name="desiredAccess">희망 액세스</param> /// <param name="tokenHandle">토큰 핸들</param> /// <returns>처리 결과</returns> [DllImport("advapi32", ExactSpelling = true, SetLastError = true)] private static extern bool OpenProcessToken(IntPtr processHandle, int desiredAccess, ref IntPtr tokenHandle); #endregion #region 특권 값 조사하기 - LookupPrivilegeValue(hostName, name, luid) /// <summary> /// 특권 값 조사하기 /// </summary> /// <param name="hostName">호스트명</param> /// <param name="name">명칭</param> /// <param name="luid">LUID</param> /// <returns>처리 결과</returns> [DllImport("advapi32", SetLastError=true) ] private static extern bool LookupPrivilegeValue(string hostName, string name, ref long luid); #endregion #region 토큰 특권 조정하기 - AdjustTokenPrivileges(tokenHandle, disableAllPrivileges, newState, bufferLength, previousState, returnLength) /// <summary> /// 토큰 특권 조정하기 /// </summary> /// <param name="tokenHandle">토큰 핸들</param> /// <param name="disableAllPrivileges">모든 특권 비활성화 여부</param> /// <param name="newState">새로운 상태</param> /// <param name="bufferLength">버퍼 길이</param> /// <param name="previousState">이전 상태</param> /// <param name="returnLength">반환 길이</param> /// <returns>처리 결과</returns> [DllImport("advapi32", ExactSpelling = true, SetLastError = true)] private static extern bool AdjustTokenPrivileges ( IntPtr tokenHandle, bool disableAllPrivileges, ref TOKEN_PRIVILEGES newState, int bufferLength, IntPtr previousState, IntPtr returnLength ); #endregion #region 윈도우 종료하기 - ExitWindowsEx(flag, reserved) /// <summary> /// 윈도우 종료하기 /// </summary> /// <param name="flag">플래그</param> /// <param name="reserved">예약</param> /// <returns>처리 결과</returns> [DllImport("user32", ExactSpelling=true, SetLastError=true) ] private static extern bool ExitWindowsEx(int flag, int reserved); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Structure ////////////////////////////////////////////////////////////////////////////////////////// Private #region 토큰 특권 - TOKEN_PRIVILEGES /// <summary> /// 토큰 특권 /// </summary> [StructLayout(LayoutKind.Sequential, Pack = 1)] private struct TOKEN_PRIVILEGES { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Public #region Field /// <summary> /// 특권 카운트 /// </summary> public int PrivilegeCount; /// <summary> /// LUID /// </summary> public long LUID; /// <summary> /// 속성 /// </summary> public int Attributes; #endregion } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Constant //////////////////////////////////////////////////////////////////////////////// Public #region Field /// <summary> /// SE_PRIVILEGE_ENABLED /// </summary> public const int SE_PRIVILEGE_ENABLED = 0x00000002; /// <summary> /// TOKEN_QUERY /// </summary> public const int TOKEN_QUERY = 0x00000008; /// <summary> /// TOKEN_ADJUST_PRIVILEGES /// </summary> public const int TOKEN_ADJUST_PRIVILEGES = 0x00000020; /// <summary> /// SE_SHUTDOWN_NAME /// </summary> public const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege"; /// <summary> /// EWX_LOGOFF /// </summary> public const int EWX_LOGOFF = 0x00000000; /// <summary> /// EWX_SHUTDOWN /// </summary> public const int EWX_SHUTDOWN = 0x00000001; /// <summary> /// EWX_REBOOT /// </summary> public const int EWX_REBOOT = 0x00000002; /// <summary> /// EWX_FORCE /// </summary> public const int EWX_FORCE = 0x00000004; /// <summary> /// EWX_POWEROFF /// </summary> public const int EWX_POWEROFF = 0x00000008; /// <summary> /// EWX_FORCEIFHUNG /// </summary> public const int EWX_FORCEIFHUNG = 0x00000010; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 윈도우 종료하기 - ExitWindow(flag) /// <summary> /// 윈도우 종료하기 /// </summary> /// <param name="flag">플래그</param> public static void ExitWindow(int flag) { bool result; IntPtr processHandle = GetCurrentProcess(); IntPtr tokenHandle = IntPtr.Zero; result = OpenProcessToken(processHandle, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref tokenHandle); TOKEN_PRIVILEGES tokenPrivileges; tokenPrivileges.PrivilegeCount = 1; tokenPrivileges.LUID = 0; tokenPrivileges.Attributes = SE_PRIVILEGE_ENABLED; result = LookupPrivilegeValue(null, SE_SHUTDOWN_NAME, ref tokenPrivileges.LUID); result = AdjustTokenPrivileges(tokenHandle, false, ref tokenPrivileges, 0, IntPtr.Zero, IntPtr.Zero); result = ExitWindowsEx(flag, 0); } #endregion #region 셧다운 하기 - Shutdown(false) /// <summary> /// 셧다운 하기 /// </summary> /// <param name="forced">강제 여부</param> public static void Shutdown(bool forced = false) { if(forced) { ExitWindow(EWX_SHUTDOWN | EWX_FORCE); } else { ExitWindow(EWX_SHUTDOWN); } } #endregion #region 리부팅 하기 - Reboot(false) /// <summary> /// 리부팅 하기 /// </summary> /// <param name="forced">강제 여부</param> public static void Reboot(bool forced = false) { if(forced) { ExitWindow(EWX_REBOOT | EWX_FORCE); } else { ExitWindow(EWX_REBOOT); } } #endregion #region 로그 오프 하기 - LogOff(false) /// <summary> /// 로그 오프 하기 /// </summary> /// <param name="forced">강제 여부</param> public static void LogOff(bool forced = false) { if(forced) { ExitWindow(EWX_LOGOFF | EWX_FORCE); } else { ExitWindow(EWX_LOGOFF); } } #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; namespace TestProject { /// <summary> /// 프로그램 /// </summary> class Program { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 프로그램 시작하기 - Main() /// <summary> /// 프로그램 시작하기 /// </summary> private static void Main() { Console.Title = "ExitWindowsEx API 함수 : 윈도우즈 종료하기/재부팅하기/로그오프하기"; WIN32Helper.Shutdown(true); //WIN32Helper.Reboot(true); //WIN32Helper.LogOff(true); } #endregion } } |
※ 윈도우즈 10에서 작동한다.