Forum: PC-Programmierung WPF Console nach "verschwinden"


von berry (Gast)


Lesenswert?

Guten Morgen liebes Forum,

gegeben ist folgendes Problem: Mit dem unten stehendem Code kann ich in 
einer WPF C# Anwendung eine Konsole "zeigen" und verschwinden "lassen" 
(Funkion
1
Toggle()
.

Wenn ich beim ersten mal "zeigen" die Farbe mit
1
Console.BackgroundColor = ConsoleColor.Red

setze, funktioniert dies auch immer. Lass ich die Console "verschwinden" 
und wieder "zeigen", kann ich keine Farbe setzten. Der Konsolenoutput 
funktioniert aber.

Ausserdem hat das Konsolenfenster nach jedes mal eine neue Handler Id. (
1
GetConsoleWindow()
1
[SuppressUnmanagedCodeSecurity]
2
public static class ConsoleManager
3
{
4
    private const string Kernel32DllName = "kernel32.dll";
5
6
    [DllImport(Kernel32DllName)]
7
    private static extern bool AllocConsole();
8
    [DllImport(Kernel32DllName)]
9
    private static extern bool AttachConsole();
10
11
    [DllImport(Kernel32DllName)]
12
    private static extern bool FreeConsole();
13
14
    [DllImport(Kernel32DllName)]
15
    public static extern IntPtr GetConsoleWindow();
16
17
    public static bool HasConsole
18
    {
19
        get { return GetConsoleWindow() != IntPtr.Zero; }
20
    }
21
22
    /// <summary>
23
    /// Creates a new console instance if the process is not attached to a console already.
24
    /// </summary>
25
    public static void Show()
26
    {
27
28
        //#if DEBUG
29
        if (!HasConsole)
30
        {
31
            AllocConsole();
32
            InvalidateOutAndError();
33
        }
34
        //#endif
35
    }
36
37
    /// <summary>
38
    /// If the process has a console attached to it, it will be detached and no longer visible. Writing to the System.Console is still possible, but no output will be shown.
39
    /// </summary>
40
    public static void Hide()
41
    {
42
        //#if DEBUG
43
        if (HasConsole)
44
        {
45
            SetOutAndErrorNull();
46
            FreeConsole();
47
        }
48
        //#endif
49
    }
50
51
    public static void Toggle()
52
    {
53
54
        if (HasConsole)
55
        {
56
            Hide();
57
        }
58
        else
59
        {
60
            Show();
61
62
        }
63
    }
64
65
    static void InvalidateOutAndError()
66
    {
67
68
        Type lType = typeof(Console);
69
70
        System.Reflection.FieldInfo lOut = lType.GetField("_out",
71
            System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic);
72
73
        System.Reflection.FieldInfo lError = lType.GetField("_error",
74
            System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic);
75
76
        System.Reflection.MethodInfo lInitializeStdOutError = lType.GetMethod("InitializeStdOutError",
77
            System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic);
78
79
        Debug.Assert(lOut != null);
80
        Debug.Assert(lError != null);
81
82
        Debug.Assert(lInitializeStdOutError != null);
83
84
        lOut.SetValue(null, null);
85
        lError.SetValue(null, null);
86
87
        lInitializeStdOutError.Invoke(null, new object[] {true});
88
89
    }
90
91
    static void SetOutAndErrorNull()
92
    {
93
        Console.SetOut(TextWriter.Null);
94
        Console.SetError(TextWriter.Null);
95
96
    }
97
}

weitere Information:
- getestet unter .Net Framework 4.6.2
- um den Code zu verwenden darf NICHT im Debugmode gestartet werden (mit 
Ctrl+F5 starten)
- Original Code: 
https://stackoverflow.com/questions/160587/no-output-to-console-from-a-wpf-application

Vielen Dank schon einmal fürs anschauen. Sollten weiter Fragen 
aufkommen, antworte ich natührlich gerne :)

MFG,
Berry

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.