■ 콘솔을 사용하는 방법을 보여준다.
▶ MainForm.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 |
using Microsoft.Win32.SafeHandles; using System; using System.IO; using System.Runtime.InteropServices; using System.Windows.Forms; namespace TestProject { /// <summary> /// 메인 폼 /// </summary> public partial class MainForm : Form { //////////////////////////////////////////////////////////////////////////////////////////////////// Import ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 콘솔 할당하기 - AllocConsole() /// <summary> /// 콘솔 할당하기 /// </summary> /// <returns></returns> [DllImport("kernel32.dll")] private static extern bool AllocConsole(); #endregion #region 콘솔 연결하기 - AttachConsole(processID) /// <summary> /// 콘솔 연결하기 /// </summary> /// <param name="processID">프로세스 ID</param> /// <returns>처리 결과</returns> [DllImport("kernel32.dll")] private static extern bool AttachConsole(int processID); #endregion #region 표준 핸들 구하기 - GetStdHandle(standardDevice) /// <summary> /// 표준 핸들 구하기 /// </summary> /// <param name="standardDevice">표준 장치</param> /// <returns>표준 핸들</returns> /// <remarks> /// standardDevice /// STD_INPUT_HANDLE : (DWORD)-10 /// STD_OUTPUT_HANDLE : (DWORD)-11 /// STD_ERROR_HANDLE : (DWORD)-12 /// </remarks> [DllImport("kernel32.dll", SetLastError = true)] private static extern IntPtr GetStdHandle(int standardDevice); #endregion #region 콘솔 창 숨기기 - FreeConsole() /// <summary> /// 콘솔 창 숨기기 /// </summary> /// <returns>처리 결과</returns> [DllImport("kernel32.dll", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool FreeConsole(); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainForm() /// <summary> /// 생성자 /// </summary> public MainForm() { InitializeComponent(); this.showButton.Click += showButton_Click; this.hideButton.Click += hidebutton_Click; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 콘솔 표시하기 - showButton_Click(sender, e) /// <summary> /// 콘솔 표시하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void showButton_Click(object sender, EventArgs e) { RunConsole(); } #endregion #region 콘솔 숨기기 - hidebutton_Click(sender, e) /// <summary> /// 콘솔 숨기기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void hidebutton_Click(object sender, EventArgs e) { FreeConsole(); } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 콘솔 실행하기 - RunConsole() /// <summary> /// 콘솔 실행하기 /// </summary> /// <returns>처리 결과</returns> private bool RunConsole() { if(!AttachConsole(-1)) { AllocConsole(); } IntPtr consoleHandle = GetStdHandle(-11); SafeFileHandle safeFileHandle = new SafeFileHandle(consoleHandle, true); FileStream stream = new FileStream(safeFileHandle, FileAccess.Write); using(StreamWriter writer = new StreamWriter(stream)) { writer.WriteLine("Hello Console!"); } return true; } #endregion } } |