성태의 닷넷 이야기
홈 주인
모아 놓은 자료
프로그래밍
질문/답변
사용자 관리
사용자
메뉴
아티클
외부 아티클
유용한 코드
온라인 기능
MathJax 입력기
최근 덧글
[정성태] Java - How to use the Foreign Funct...
[정성태] 제가 큰 실수를 했군요. ^^; Delegate를 통한 Bein...
[정성태] Working with Rust Libraries from C#...
[정성태] Detecting blocking calls using asyn...
[정성태] 아쉽게도, 커뮤니티는 아니고 개인 블로그입니다. ^^
[정성태] 질문이 잘 이해가 안 됩니다. 우선, 해당 소스코드에서 ILis...
[양승조
] var대신 dinamic으로 선언해서 해결은 했습니다. 맞는 해...
[양승조
] 또 막혔습니다. ㅠㅠ var list = props[i].Ge...
[양승조
] 아. 감사합니다. 어제는 안됐던것 같은데....정신을 차려야겠네...
[정성태] "props[i].GetValue(props[i])" 코드에서 ...
글쓰기
제목
이름
암호
전자우편
HTML
홈페이지
유형
제니퍼 .NET
닷넷
COM 개체 관련
스크립트
VC++
VS.NET IDE
Windows
Team Foundation Server
디버깅 기술
오류 유형
개발 환경 구성
웹
기타
Linux
Java
DDK
Math
Phone
Graphics
사물인터넷
부모글 보이기/감추기
내용
<div style='display: inline'> <h1 style='font-family: Malgun Gothic, Consolas; font-size: 20pt; color: #006699; text-align: center; font-weight: bold'>WinForm/WPF에서 Console 창을 띄워 출력하는 방법 (2) - Output 디버깅 출력을 AllocConsole로 우회</h1> <p> 지난 글에서 Windows UI 응용 프로그램에서 Console 창을 띄워 사용하는 방법을 설명했습니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > WinForm/WPF에서 Console 창을 띄워 출력하는 방법 ; <a target='tab' href='https://www.sysnet.pe.kr/2/0/12000'>https://www.sysnet.pe.kr/2/0/12000</a> </pre> <br /> 위의 구현은 2가지 문제가 있다고 했는데요, 1) <a target='tab' href='https://docs.microsoft.com/en-us/windows/console/allocconsole'>AllocConsole</a> 이전에 <a target='tab' href='https://docs.microsoft.com/en-us/dotnet/api/system.console.writeline'>Console.WriteLine</a>을 호출하면 안 되고, 2) 비주얼 스튜디오가 F5 디버깅으로 실행한 경우에는 Output 창을 이용하므로 안 됩니다. 그런데 가만 생각해 보니 Output 창도 어차피 Console 출력을 우회한 것이기 때문에 관련 redirection을 잘 조절해 보면 방법이 있을 것 같습니다. 그리고, 실제로 찾아보니 다음과 같이 이미 구현 코드가 있군요. ^^<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > Console.Out Output is showing in Output Window, Needed in AllocConsole() ; <a target='tab' href='https://stackoverflow.com/questions/41624103/console-out-output-is-showing-in-output-window-needed-in-allocconsole'>https://stackoverflow.com/questions/41624103/console-out-output-is-showing-in-output-window-needed-in-allocconsole</a> </pre> <br /> 이를 바탕으로 ConsoleOutRedirector 코드를 좀 더 다듬을 수 있고,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > using Microsoft.Win32.SafeHandles; using System; using System.Diagnostics; using System.IO; using System.Runtime.InteropServices; using System.Text; namespace WindowsFormsApp1 { public class ConsoleOutRedirector : IDisposable { public ConsoleOutRedirector() { NativeMethods.AllocConsole(); InitConsole(); } private void InitConsole() { IntPtr stdHandle = NativeMethods.CreateFile( "CONOUT$", NativeMethods.GENERIC_WRITE, NativeMethods.FILE_SHARE_WRITE, 0, NativeMethods.OPEN_EXISTING, 0, 0 ); SafeFileHandle safeFileHandle = new SafeFileHandle(stdHandle, true); FileStream fileStream = new FileStream(safeFileHandle, FileAccess.Write); Encoding encoding = System.Text.Encoding.GetEncoding(NativeMethods.MY_CODE_PAGE); StreamWriter standardOutput = new StreamWriter(fileStream, encoding); standardOutput.AutoFlush = true; Console.SetOut(standardOutput); } public void Dispose() { NativeMethods.FreeConsole(); } } static class NativeMethods { internal const int MY_CODE_PAGE = 437; internal const uint GENERIC_WRITE = 0x40000000; internal const uint FILE_SHARE_WRITE = 0x2; internal const uint OPEN_EXISTING = 0x3; [DllImport("kernel32.dll", SetLastError = true)] internal static extern IntPtr CreateFile( string lpFileName, uint dwDesiredAccess, uint dwShareMode, uint lpSecurityAttributes, uint dwCreationDisposition, uint dwFlagsAndAttributes, uint hTemplateFile); [DllImport("kernel32.dll", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] internal static extern bool AllocConsole(); [DllImport("kernel32.dll", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] internal static extern bool FreeConsole(); } } </pre> <br /> 사용은 다음과 같이 하면 됩니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > using System; using System.Windows.Forms; namespace WindowsFormsApp1 { static class Program { [STAThread] static void Main() { #if DEBUG using (var consoleOutRedirector = new ConsoleOutRedirector()) #endif { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } } } } </pre> <br /> 그러면, F5와 Ctrl + F5 모두 정상적으로 콘솔 창이 뜨고 Console.Write 출력을 가져옵니다. 게다가 AllocConsole 이전에 Console.Write를 해도,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > Console.WriteLine("TEST"); // 디버깅 시 Output 창에 출력 // 그 외에는 콘솔 창이 없으므로 누락 using (var consoleOutRedirector = new ConsoleOutRedirector()) { Console.WriteLine("Shown in AllocConsole window"); // AllocConsole 창에 출력 Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } </pre> <br /> 정상적으로 AllocConsole 창에 출력이 됩니다.<br /><br /> <img src='/SysWebRes/bbs/alloc_console_1.png' /><br /> <br /> <hr style='width: 50%' /><br /> <br /> 이 글의 예제 코드는 다음의 github repo에 있습니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > DotNetSamples/WinForms/ConsoleOutRedirect/ ; <a target='tab' href='https://github.com/stjeong/DotNetSamples/tree/master/WinForms/ConsoleOutRedirect'>https://github.com/stjeong/DotNetSamples/tree/master/WinForms/ConsoleOutRedirect</a> </pre> </p><br /> <br /><hr /><span style='color: Maroon'>[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]</span> </div>
첨부파일
스팸 방지용 인증 번호
1617
(왼쪽의 숫자를 입력해야 합니다.)