From 4c88a59f17e907492db18f3c304b67ae20c2d16d Mon Sep 17 00:00:00 2001 From: Pavel Djundik Date: Mon, 26 Aug 2024 18:26:52 +0300 Subject: [PATCH 1/3] Display a message box when exe is not lauched from console --- DepotDownloader/PlatformUtilities.cs | 30 ++++++++++++++++++++++++++++ DepotDownloader/Program.cs | 6 ++++++ 2 files changed, 36 insertions(+) diff --git a/DepotDownloader/PlatformUtilities.cs b/DepotDownloader/PlatformUtilities.cs index 2386eae..31d26e9 100644 --- a/DepotDownloader/PlatformUtilities.cs +++ b/DepotDownloader/PlatformUtilities.cs @@ -145,5 +145,35 @@ namespace DepotDownloader : mode & ~ModeExecute))); } } + + #region WIN32_CONSOLE_STUFF + [DllImport("kernel32.dll", SetLastError = true)] + static extern uint GetConsoleProcessList(uint[] processList, uint processCount); + + [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)] + static extern int MessageBox(IntPtr hWnd, string text, string caption, uint type); + + const uint MB_OK = 0x0; + const uint MB_ICONWARNING = 0x30; + + public static void VerifyConsoleLaunch() + { + // Reference: https://devblogs.microsoft.com/oldnewthing/20160125-00/?p=92922 + var processList = new uint[2]; + var processCount = GetConsoleProcessList(processList, (uint)processList.Length); + + if (processCount != 1) + { + return; + } + + _ = MessageBox( + IntPtr.Zero, + "Depot Downloader is a console application; there is no GUI.\n\nIf you do not pass any command line parameters, it prints usage info and exits.\n\nYou must use this from a terminal/console.", + "DepotDownloader", + MB_OK | MB_ICONWARNING + ); + } + #endregion } } diff --git a/DepotDownloader/Program.cs b/DepotDownloader/Program.cs index 058654c..50db98b 100644 --- a/DepotDownloader/Program.cs +++ b/DepotDownloader/Program.cs @@ -21,6 +21,12 @@ namespace DepotDownloader if (args.Length == 0) { PrintUsage(); + + if (OperatingSystem.IsWindows()) + { + PlatformUtilities.VerifyConsoleLaunch(); + } + return 1; } From 1620090c1992f9c6d0695f304bc9f4fabfc3e126 Mon Sep 17 00:00:00 2001 From: Pavel Djundik Date: Mon, 26 Aug 2024 18:58:07 +0300 Subject: [PATCH 2/3] Make extens stricter --- DepotDownloader/PlatformUtilities.cs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/DepotDownloader/PlatformUtilities.cs b/DepotDownloader/PlatformUtilities.cs index 31d26e9..d3d184f 100644 --- a/DepotDownloader/PlatformUtilities.cs +++ b/DepotDownloader/PlatformUtilities.cs @@ -147,11 +147,13 @@ namespace DepotDownloader } #region WIN32_CONSOLE_STUFF - [DllImport("kernel32.dll", SetLastError = true)] + [DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] static extern uint GetConsoleProcessList(uint[] processList, uint processCount); - [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)] - static extern int MessageBox(IntPtr hWnd, string text, string caption, uint type); + [DllImport("user32.dll", SetLastError = true, ExactSpelling = true, CharSet = CharSet.Unicode)] + [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] + static extern int MessageBoxW(IntPtr hWnd, string text, string caption, uint type); const uint MB_OK = 0x0; const uint MB_ICONWARNING = 0x30; @@ -167,7 +169,7 @@ namespace DepotDownloader return; } - _ = MessageBox( + _ = MessageBoxW( IntPtr.Zero, "Depot Downloader is a console application; there is no GUI.\n\nIf you do not pass any command line parameters, it prints usage info and exits.\n\nYou must use this from a terminal/console.", "DepotDownloader", From 0ce116fdcee7b189756c71facd824f3cb0901aac Mon Sep 17 00:00:00 2001 From: Pavel Djundik Date: Tue, 27 Aug 2024 09:56:10 +0300 Subject: [PATCH 3/3] Use CsWin32 --- DepotDownloader/DepotDownloader.csproj | 4 ++++ DepotDownloader/NativeMethods.txt | 2 ++ DepotDownloader/PlatformUtilities.cs | 25 +++++++------------------ 3 files changed, 13 insertions(+), 18 deletions(-) create mode 100644 DepotDownloader/NativeMethods.txt diff --git a/DepotDownloader/DepotDownloader.csproj b/DepotDownloader/DepotDownloader.csproj index 0b060b1..319e281 100644 --- a/DepotDownloader/DepotDownloader.csproj +++ b/DepotDownloader/DepotDownloader.csproj @@ -19,6 +19,10 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/DepotDownloader/NativeMethods.txt b/DepotDownloader/NativeMethods.txt new file mode 100644 index 0000000..8f2bb82 --- /dev/null +++ b/DepotDownloader/NativeMethods.txt @@ -0,0 +1,2 @@ +GetConsoleProcessList +MessageBox diff --git a/DepotDownloader/PlatformUtilities.cs b/DepotDownloader/PlatformUtilities.cs index d3d184f..01c75a3 100644 --- a/DepotDownloader/PlatformUtilities.cs +++ b/DepotDownloader/PlatformUtilities.cs @@ -3,6 +3,7 @@ using System; using System.Runtime.InteropServices; +using System.Runtime.Versioning; namespace DepotDownloader { @@ -146,36 +147,24 @@ namespace DepotDownloader } } - #region WIN32_CONSOLE_STUFF - [DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)] - [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] - static extern uint GetConsoleProcessList(uint[] processList, uint processCount); - - [DllImport("user32.dll", SetLastError = true, ExactSpelling = true, CharSet = CharSet.Unicode)] - [DefaultDllImportSearchPaths(DllImportSearchPath.System32)] - static extern int MessageBoxW(IntPtr hWnd, string text, string caption, uint type); - - const uint MB_OK = 0x0; - const uint MB_ICONWARNING = 0x30; - + [SupportedOSPlatform("windows5.0")] public static void VerifyConsoleLaunch() { // Reference: https://devblogs.microsoft.com/oldnewthing/20160125-00/?p=92922 var processList = new uint[2]; - var processCount = GetConsoleProcessList(processList, (uint)processList.Length); + var processCount = Windows.Win32.PInvoke.GetConsoleProcessList(processList); if (processCount != 1) { return; } - _ = MessageBoxW( - IntPtr.Zero, + _ = Windows.Win32.PInvoke.MessageBox( + Windows.Win32.Foundation.HWND.Null, "Depot Downloader is a console application; there is no GUI.\n\nIf you do not pass any command line parameters, it prints usage info and exits.\n\nYou must use this from a terminal/console.", - "DepotDownloader", - MB_OK | MB_ICONWARNING + "Depot Downloader", + Windows.Win32.UI.WindowsAndMessaging.MESSAGEBOX_STYLE.MB_OK | Windows.Win32.UI.WindowsAndMessaging.MESSAGEBOX_STYLE.MB_ICONWARNING ); } - #endregion } }