From 9874a6d0bf9893aa125cf32ea2131d90f57b6707 Mon Sep 17 00:00:00 2001 From: js6pak Date: Mon, 23 May 2022 19:26:28 +0200 Subject: [PATCH] Improve ConsoleWindow exceptions --- BepInEx.Core/Console/Windows/ConsoleWindow.cs | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/BepInEx.Core/Console/Windows/ConsoleWindow.cs b/BepInEx.Core/Console/Windows/ConsoleWindow.cs index 6332d6b..d08f051 100644 --- a/BepInEx.Core/Console/Windows/ConsoleWindow.cs +++ b/BepInEx.Core/Console/Windows/ConsoleWindow.cs @@ -4,6 +4,7 @@ // -------------------------------------------------- using System; +using System.ComponentModel; using System.Runtime.InteropServices; using BepInEx; using BepInEx.ConsoleUtil; @@ -13,6 +14,7 @@ namespace UnityInjector.ConsoleUtil; internal class ConsoleWindow { + private const int STD_OUTPUT_HANDLE = -11; private const uint SC_CLOSE = 0xF060; private const uint MF_BYCOMMAND = 0x00000000; @@ -52,7 +54,7 @@ internal class ConsoleWindow Initialize(); if (OriginalStdoutHandle == IntPtr.Zero) - OriginalStdoutHandle = GetStdHandle(-11); + OriginalStdoutHandle = GetStdHandle(STD_OUTPUT_HANDLE); var cur = GetConsoleWindow(); @@ -62,7 +64,7 @@ internal class ConsoleWindow var currWnd = getForeground(); if (!AllocConsole()) - throw new Exception("AllocConsole() failed"); + throw new Win32Exception("AllocConsole() failed"); // Restore Foreground setForeground(currWnd); @@ -71,8 +73,8 @@ internal class ConsoleWindow ConsoleOutHandle = CreateFile("CONOUT$", 0x80000000 | 0x40000000, 2, IntPtr.Zero, 3, 0, IntPtr.Zero); Kon.conOut = ConsoleOutHandle; - if (!SetStdHandle(-11, ConsoleOutHandle)) - throw new Exception("SetStdHandle() failed"); + if (!SetStdHandle(STD_OUTPUT_HANDLE, ConsoleOutHandle)) + throw new Win32Exception("SetStdHandle() failed"); if (OriginalStdoutHandle != IntPtr.Zero && ConsoleManager.ConfigConsoleOutRedirectType.Value == ConsoleManager.ConsoleOutRedirectType.ConsoleOut) @@ -99,15 +101,15 @@ internal class ConsoleWindow return; if (!CloseHandle(ConsoleOutHandle)) - throw new Exception("CloseHandle() failed"); + throw new Win32Exception("CloseHandle() failed"); ConsoleOutHandle = IntPtr.Zero; if (!FreeConsole()) - throw new Exception("FreeConsole() failed"); + throw new Win32Exception("FreeConsole() failed"); - if (!SetStdHandle(-11, OriginalStdoutHandle)) - throw new Exception("SetStdHandle() failed"); + if (!SetStdHandle(STD_OUTPUT_HANDLE, OriginalStdoutHandle)) + throw new Win32Exception("SetStdHandle() failed"); IsAttached = false; }