From b9e296a93ff8949da79b3fde0ce8eb0893d30849 Mon Sep 17 00:00:00 2001 From: Cyberboss Date: Tue, 2 Oct 2018 14:00:38 -0400 Subject: [PATCH] Switch to using BetterWin32Exceptions --- .../Watchdog/WindowsNetworkPromptReaper.cs | 20 +++++++++---------- .../IO/WindowsSymlinkFactory.cs | 11 ++++------ .../Tgstation.Server.Host.csproj | 1 + 3 files changed, 15 insertions(+), 17 deletions(-) diff --git a/src/Tgstation.Server.Host/Components/Watchdog/WindowsNetworkPromptReaper.cs b/src/Tgstation.Server.Host/Components/Watchdog/WindowsNetworkPromptReaper.cs index 2298971e6a..ef11168489 100644 --- a/src/Tgstation.Server.Host/Components/Watchdog/WindowsNetworkPromptReaper.cs +++ b/src/Tgstation.Server.Host/Components/Watchdog/WindowsNetworkPromptReaper.cs @@ -1,8 +1,8 @@ -using Microsoft.Extensions.Hosting; +using BetterWin32Errors; +using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; -using System.ComponentModel; using System.Linq; using System.Runtime.InteropServices; using System.Text; @@ -49,10 +49,10 @@ namespace Tgstation.Server.Host.Components.Watchdog { var gcChildhandlesList = GCHandle.FromIntPtr(lParam); - if (gcChildhandlesList == null || gcChildhandlesList.Target == null) + if (gcChildhandlesList.Target == null) return false; - var childHandles = (List )gcChildhandlesList.Target; + var childHandles = (List)gcChildhandlesList.Target; childHandles.Add(hWnd); return true; @@ -61,12 +61,10 @@ namespace Tgstation.Server.Host.Components.Watchdog static List GetAllChildHandles(IntPtr main) { var childHandles = new List(); - var gcChildhandlesList = GCHandle.Alloc(childHandles); - var pointerChildHandlesList = GCHandle.ToIntPtr(gcChildhandlesList); - try { + var pointerChildHandlesList = GCHandle.ToIntPtr(gcChildhandlesList); NativeMethods.EnumWindowProc childProc = new NativeMethods.EnumWindowProc(EnumWindow); NativeMethods.EnumChildWindows(main, childProc, pointerChildHandlesList); } @@ -74,7 +72,6 @@ namespace Tgstation.Server.Host.Components.Watchdog { gcChildhandlesList.Free(); } - return childHandles; } @@ -129,7 +126,7 @@ namespace Tgstation.Server.Host.Components.Watchdog if (NativeMethods.GetWindowText(I, stringBuilder, MaxLength) == 0) { - logger.LogWarning("Error calling GetWindowText! Exception: {0}", new Win32Exception(Marshal.GetLastWin32Error())); + logger.LogWarning("Error calling GetWindowText! Exception: {0}", new Win32Exception()); continue; } @@ -152,7 +149,10 @@ namespace Tgstation.Server.Host.Components.Watchdog logger.LogDebug("Unable to find \"Yes\" button for \"Network Accessibility\" window in owned process {0}!", processId); } } - catch (OperationCanceledException) { } + catch (OperationCanceledException) + { + logger.LogTrace("Cancelled!"); + } finally { logger.LogDebug("Exiting network prompt reaper..."); diff --git a/src/Tgstation.Server.Host/IO/WindowsSymlinkFactory.cs b/src/Tgstation.Server.Host/IO/WindowsSymlinkFactory.cs index e1b54d7eca..13aae902c2 100644 --- a/src/Tgstation.Server.Host/IO/WindowsSymlinkFactory.cs +++ b/src/Tgstation.Server.Host/IO/WindowsSymlinkFactory.cs @@ -1,7 +1,6 @@ -using System; -using System.ComponentModel; +using BetterWin32Errors; +using System; using System.IO; -using System.Runtime.InteropServices; using System.Threading; using System.Threading.Tasks; @@ -28,15 +27,13 @@ namespace Tgstation.Server.Host.IO cancellationToken.ThrowIfCancellationRequested(); if (!NativeMethods.CreateSymbolicLink(linkPath, targetPath, flags)) { - var error = Marshal.GetLastWin32Error(); - if (error == 87) //INVALID_PARAMETER, SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE isn't supported + if (Win32Exception.GetLastWin32Error() == Win32Error.ERROR_INVALID_PARAMETER) //SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE isn't supported { flags &= ~NativeMethods.CreateSymbolicLinkFlags.AllowUnprivilegedCreate; if (NativeMethods.CreateSymbolicLink(linkPath, targetPath, flags)) return; - error = Marshal.GetLastWin32Error(); } - throw new Win32Exception(error); + throw new Win32Exception(); } }, cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Current); } diff --git a/src/Tgstation.Server.Host/Tgstation.Server.Host.csproj b/src/Tgstation.Server.Host/Tgstation.Server.Host.csproj index a4717a77e7..9d33cd974a 100644 --- a/src/Tgstation.Server.Host/Tgstation.Server.Host.csproj +++ b/src/Tgstation.Server.Host/Tgstation.Server.Host.csproj @@ -20,6 +20,7 @@ +