2012-10-24 2 views
0

AllocConsole으로 만든 콘솔을 사용하는 GUI C# 응용 프로그램이 있습니다. 그것은 정상적인 조건에서 작동하지만, 응용 프로그램이 Visual Studio에서 디버그 모드로 시작되면 모든 출력은 Visual Studio 출력 창에서 끝납니다. 어떻게 그걸 막을 수 있니?VC#가 콘솔 출력을 출력 창으로 재전송하지 않게하는 방법은 무엇입니까?

C# 3.5 및 Visual Studio Pro 2010을 사용하고 있습니다. 프로세스 호스팅 옵션이 해제되어 있습니다.

당신은 다르게 너무 (참고 1) 같은 응용 프로그램을 실행하여 출력을 리디렉션 할 수
+0

아니요, C# 코드 *가 실제로 Console.Write/Line()을 사용하면 괜찮습니다. Debug.Print 또는 추적과 같은 작업을 수행하면 출력은 OutputDebugString()에 의해 작성되고 디버거 창에서 끝납니다. 출력 창. SysInternals의 DebugView 유틸리티를 고려하십시오. –

+0

Console.WriteLine을 사용하고 내 콘솔 대신 Visual Studio 출력 창에서 종료됩니다. –

+0

설명하기가 어렵습니다. 호스팅 프로세스 옵션을 해제하십시오. –

답변

3

내 솔루션은 표준 스트림을 직접 새로 생성 된 콘솔로 재설정하는 것이 었습니다. 여기에 코드가 있습니다 :

public static class ConsoleHelper 
{ 
    /// <summary> 
    /// Allocates a console and resets the standard stream handles. 
    /// </summary> 
    public static void Alloc() 
    { 
     if (!AllocConsole()) 
      throw new Win32Exception(); 
     SetStdHandle(StdHandle.Output, GetConsoleStandardOutput()); 
     SetStdHandle(StdHandle.Input, GetConsoleStandardInput()); 
    } 

    private static IntPtr GetConsoleStandardInput() 
    { 
     var handle = CreateFile 
      ("CONIN$" 
      , DesiredAccess.GenericRead | DesiredAccess.GenericWrite 
      , FileShare.ReadWrite 
      , IntPtr.Zero 
      , FileMode.Open 
      , FileAttributes.Normal 
      , IntPtr.Zero 
      ); 
     if (handle == InvalidHandleValue) 
      throw new Win32Exception(); 
     return handle; 
    } 

    private static IntPtr GetConsoleStandardOutput() 
    { 
     var handle = CreateFile 
      ("CONOUT$" 
      , DesiredAccess.GenericWrite | DesiredAccess.GenericWrite 
      , FileShare.ReadWrite 
      , IntPtr.Zero 
      , FileMode.Open 
      , FileAttributes.Normal 
      , IntPtr.Zero 
      ); 
     if (handle == InvalidHandleValue) 
      throw new Win32Exception(); 
     return handle; 
    } 

    [DllImport("kernel32.dll", SetLastError = true)] 
    private static extern bool AllocConsole(); 

    [DllImport("kernel32.dll")] 
    private static extern bool SetStdHandle(StdHandle nStdHandle, IntPtr hHandle); 

    [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)] 
    private static extern IntPtr CreateFile 
     (        string   lpFileName 
     , [MarshalAs(UnmanagedType.U4)] DesiredAccess dwDesiredAccess 
     , [MarshalAs(UnmanagedType.U4)] FileShare  dwShareMode 
     ,        IntPtr   lpSecurityAttributes 
     , [MarshalAs(UnmanagedType.U4)] FileMode  dwCreationDisposition 
     , [MarshalAs(UnmanagedType.U4)] FileAttributes dwFlagsAndAttributes 
     ,        IntPtr   hTemplateFile 
     ); 

    [Flags] 
    enum DesiredAccess : uint 
    { 
     GenericRead = 0x80000000, 
     GenericWrite = 0x40000000, 
     GenericExecute = 0x20000000, 
     GenericAll  = 0x10000000 
    } 

    private enum StdHandle : int 
    { 
     Input = -10, 
     Output = -11, 
     Error = -12 
    } 

    private static readonly IntPtr InvalidHandleValue = new IntPtr(-1); 
} 
+0

네임 스페이스가'DesiredAccess'를 가져 왔습니까? –

+0

아, http://www.pinvoke.net/default.aspx/kernel32.createfile에서 파생 된 것처럼 보입니다. –

0

:

다음 (참고 1)에 의해 (필요한 경우) 또한 출력을 소비 할 수
// 
// Setup the process with the ProcessStartInfo class. 
// 
ProcessStartInfo start = new ProcessStartInfo(); 
start.FileName = @"C:\MyExe.exe"; // Specify exe name. 
start.UseShellExecute = false; 
start.RedirectStandardOutput = true; 

:

// 
// Start the process. 
// 
using (Process process = Process.Start(start)) 
{ 
    // 
    // Read in all the text from the process with the StreamReader. 
    // 
    using (StreamReader reader = process.StandardOutput) 
    { 
    string result = reader.ReadToEnd(); 
    Console.Write(result); 
    } 
} 

참조 : (1) http://www.dotnetperls.com/redirectstandardoutput

+0

다른 실행 파일의 스트림을 리디렉션하지 않습니다. –

관련 문제