From 20d7d33023a9bce38c5ad77a47bdaef246f21f14 Mon Sep 17 00:00:00 2001 From: Inga Lovinde <52715130+inga-lovinde@users.noreply.github.com> Date: Wed, 19 Aug 2020 02:46:48 +0300 Subject: [PATCH] Improved compatibility with systems supporting interactive services (e.g. Win7) --- RadeonResetBugFixService/ConsoleHelper.cs | 23 +++++++++++++++++++ RadeonResetBugFixService/Program.cs | 9 +++++++- .../RadeonResetBugFixService.csproj | 1 + 3 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 RadeonResetBugFixService/ConsoleHelper.cs diff --git a/RadeonResetBugFixService/ConsoleHelper.cs b/RadeonResetBugFixService/ConsoleHelper.cs new file mode 100644 index 0000000..c9010a8 --- /dev/null +++ b/RadeonResetBugFixService/ConsoleHelper.cs @@ -0,0 +1,23 @@ +namespace RadeonResetBugFixService +{ + using System; + using System.Runtime.InteropServices; + + static class ConsoleHelper + { + private static class NativeMethods + { + [DllImport("kernel32.dll")] + public static extern IntPtr GetConsoleWindow(); + + [DllImport("user32.dll")] + public static extern bool IsWindowVisible(IntPtr hWnd); + } + + // Code taken from https://stackoverflow.com/a/53716169 + public static bool HaveVisibleConsole() + { + return NativeMethods.IsWindowVisible(NativeMethods.GetConsoleWindow()); + } + } +} diff --git a/RadeonResetBugFixService/Program.cs b/RadeonResetBugFixService/Program.cs index 60ef90a..79f6adb 100644 --- a/RadeonResetBugFixService/Program.cs +++ b/RadeonResetBugFixService/Program.cs @@ -1,6 +1,7 @@ namespace RadeonResetBugFixService { using System; + using System.Runtime.InteropServices; using System.Security.Principal; using System.ServiceProcess; using ThirdParty.ServiceHelpers; @@ -17,7 +18,13 @@ throw new ArgumentNullException(nameof(args)); } - if (Environment.UserInteractive) + if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + Console.Error.WriteLine("This program only runs on Windows"); + return -1; + } + + if (ConsoleHelper.HaveVisibleConsole()) { if (!HasAdministratorPrivileges()) { diff --git a/RadeonResetBugFixService/RadeonResetBugFixService.csproj b/RadeonResetBugFixService/RadeonResetBugFixService.csproj index 8b48649..40855ee 100644 --- a/RadeonResetBugFixService/RadeonResetBugFixService.csproj +++ b/RadeonResetBugFixService/RadeonResetBugFixService.csproj @@ -58,6 +58,7 @@ +