Switch to using BetterWin32Exceptions

This commit is contained in:
Cyberboss
2018-10-02 14:00:38 -04:00
parent e162ca57b0
commit b9e296a93f
3 changed files with 15 additions and 17 deletions
@@ -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 <IntPtr>)gcChildhandlesList.Target;
var childHandles = (List<IntPtr>)gcChildhandlesList.Target;
childHandles.Add(hWnd);
return true;
@@ -61,12 +61,10 @@ namespace Tgstation.Server.Host.Components.Watchdog
static List<IntPtr> GetAllChildHandles(IntPtr main)
{
var childHandles = new List<IntPtr>();
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...");
@@ -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);
}
@@ -20,6 +20,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="BetterWin32Errors" Version="0.2.0" />
<PackageReference Include="Byond.TopicSender" Version="1.1.4" />
<PackageReference Include="Cyberboss.AspNetCore.AsyncInitializer" Version="1.2.0" />
<PackageReference Include="Cyberboss.SmartIrc4net.Standard" Version="0.4.6" />