Fix IsMutexPresent() to avoid incorrect use of GetLastWin32Error() (#4910)

This commit is contained in:
Andy Jordan
2025-08-28 14:21:42 -07:00
committed by GitHub
parent 4e2cb6030e
commit 1cdbf46c9e
+8 -8
View File
@@ -8,6 +8,7 @@ using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;
using Microsoft.PowerShell;
using Microsoft.PowerShell.Internal;
using Microsoft.Win32.SafeHandles;
@@ -79,19 +80,18 @@ static class PlatformWindows
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
internal static extern IntPtr GetStdHandle(uint handleId);
internal const int ERROR_ALREADY_EXISTS = 0xB7;
internal static bool IsMutexPresent(string name)
{
try
{
using var mutex = new System.Threading.Mutex(false, name);
return Marshal.GetLastWin32Error() == ERROR_ALREADY_EXISTS;
}
catch
{
return false;
if (Mutex.TryOpenExisting(name, out var tempMutex))
{
tempMutex.Dispose();
return true;
}
}
catch { }
return false;
}
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]