mirror of
https://github.com/PowerShell/PSReadLine
synced 2026-06-08 12:12:13 +00:00
Fix IsMutexPresent() to avoid incorrect use of GetLastWin32Error() (#4910)
This commit is contained in:
@@ -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)]
|
||||
|
||||
Reference in New Issue
Block a user