Skip to content

Commit df72935

Browse files
committed
Fix IsMutexPresent() to avoid incorrect use of GetLastError()
Since we're not actually making a system call.
1 parent 4e2cb60 commit df72935

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

PSReadLine/PlatformWindows.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Diagnostics;
99
using System.Linq;
1010
using System.Runtime.InteropServices;
11+
using System.Threading;
1112
using Microsoft.PowerShell;
1213
using Microsoft.PowerShell.Internal;
1314
using Microsoft.Win32.SafeHandles;
@@ -79,19 +80,18 @@ IntPtr templateFileWin32Handle
7980
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
8081
internal static extern IntPtr GetStdHandle(uint handleId);
8182

82-
internal const int ERROR_ALREADY_EXISTS = 0xB7;
83-
8483
internal static bool IsMutexPresent(string name)
8584
{
8685
try
8786
{
88-
using var mutex = new System.Threading.Mutex(false, name);
89-
return Marshal.GetLastWin32Error() == ERROR_ALREADY_EXISTS;
90-
}
91-
catch
92-
{
93-
return false;
87+
if (Mutex.TryOpenExisting(name, out var tempMutex))
88+
{
89+
tempMutex.Dispose();
90+
return true;
91+
}
9492
}
93+
catch { }
94+
return false;
9595
}
9696

9797
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]

0 commit comments

Comments
 (0)