■ SetConsoleCtrlHandler API 함수를 선언하는 방법을 보여준다.
▶ 예제 코드 (C#)
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 |
using System.Runtime.InteropServices; #region 콘솔 컨트롤 핸들러 설정하기 - SetConsoleCtrlHandler(handler, add) /// <summary> /// 콘솔 컨트롤 핸들러 설정하기 /// </summary> /// <param name="handler">핸들러</param> /// <param name="add">추가 여부</param> /// <returns>처리 결과</returns> [DllImport("kernel32")] private static extern bool SetConsoleCtrlHandler(SetConsoleCtrlHandlerDelegate handler, bool add); #endregion #region 콘솔 컨트롤 핸들러 설정하기 대리자 - SetConsoleCtrlHandlerDelegate(controlType) /// <summary> /// 콘솔 컨트롤 핸들러 설정하기 대리자 /// </summary> /// <param name="controlType">컨트롤 타입</param> /// <returns>처리 결과</returns> private delegate bool SetConsoleCtrlHandlerDelegate(ControlType controlType); #endregion /// <summary> /// 컨트롤 타입 /// </summary> public enum ControlType { /// <summary> /// CTRL_C_EVENT /// </summary> CTRL_C_EVENT = 0, /// <summary> /// CTRL_BREAK_EVENT /// </summary> CTRL_BREAK_EVENT = 1, /// <summary> /// CTRL_CLOSE_EVENT /// </summary> CTRL_CLOSE_EVENT = 2, /// <summary> /// CTRL_LOGOFF_EVENT /// </summary> CTRL_LOGOFF_EVENT = 5, /// <summary> /// CTRL_SHUTDOWN_EVENT /// </summary> CTRL_SHUTDOWN_EVENT = 6 } |