|
| 1 | +using Pepa.Wpf.Utilities; |
| 2 | +using System; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Diagnostics; |
| 5 | +using System.Runtime.InteropServices; |
| 6 | + |
| 7 | +namespace Daybreak.Services.Mutex |
| 8 | +{ |
| 9 | + public sealed class MutexHandler : IMutexHandler |
| 10 | + { |
| 11 | + public void CloseMutex(Process process, string mutexName) |
| 12 | + { |
| 13 | + CloseHandle(process, mutexName); |
| 14 | + } |
| 15 | + |
| 16 | + private static List<NativeMethods.SystemHandleInformation> GetHandles(Process targetProcess, IntPtr systemHandle) |
| 17 | + { |
| 18 | + var processHandles = new List<NativeMethods.SystemHandleInformation>(); |
| 19 | + var basePointer = systemHandle.ToInt64(); |
| 20 | + NativeMethods.SystemHandleInformation currentHandleInfo; |
| 21 | + for (int i = 0; i < Marshal.ReadInt32(systemHandle); i++) |
| 22 | + { |
| 23 | + var currentOffset = IntPtr.Size + i * Marshal.SizeOf(typeof(NativeMethods.SystemHandleInformation)); |
| 24 | + currentHandleInfo = (NativeMethods.SystemHandleInformation)Marshal.PtrToStructure(new IntPtr(basePointer + currentOffset), typeof(NativeMethods.SystemHandleInformation)); |
| 25 | + if (currentHandleInfo.OwnerPID == (uint)targetProcess.Id) |
| 26 | + { |
| 27 | + processHandles.Add(currentHandleInfo); |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + return processHandles; |
| 32 | + } |
| 33 | + |
| 34 | + private static void CloseHandle(Process targetProcess, string handleName) |
| 35 | + { |
| 36 | + var systemHandles = GetAllHandles(); |
| 37 | + if (systemHandles == IntPtr.Zero) |
| 38 | + { |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + List<NativeMethods.SystemHandleInformation> processHandles = GetHandles(targetProcess, systemHandles); |
| 43 | + Marshal.FreeHGlobal(systemHandles); |
| 44 | + var processHandle = NativeMethods.OpenProcess(NativeMethods.ProcessAccessFlags.DupHandle, false, (uint)targetProcess.Id); |
| 45 | + foreach (var handleInfo in processHandles) |
| 46 | + { |
| 47 | + if (GetHandleName(handleInfo, processHandle).Contains(handleName)) |
| 48 | + { |
| 49 | + if (CloseOwnedHandle(handleInfo.OwnerPID, new IntPtr(handleInfo.HandleValue))) |
| 50 | + { |
| 51 | + NativeMethods.CloseHandle(processHandle); |
| 52 | + return; |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + NativeMethods.CloseHandle(processHandle); |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + private static string GetHandleName(NativeMethods.SystemHandleInformation targetHandleInfo, IntPtr processHandle) |
| 62 | + { |
| 63 | + if (targetHandleInfo.AccessMask.ToInt64() == 0x0012019F) |
| 64 | + { |
| 65 | + return string.Empty; |
| 66 | + } |
| 67 | + |
| 68 | + var thisProcess = Process.GetCurrentProcess().Handle; |
| 69 | + NativeMethods.DuplicateHandle(processHandle, new IntPtr(targetHandleInfo.HandleValue), thisProcess, out var handle, 0, false, NativeMethods.DuplicateOptions.DUPLICATE_SAME_ACCESS); |
| 70 | + var bufferSize = GetHandleNameLength(handle); |
| 71 | + var stringBuffer = Marshal.AllocHGlobal(bufferSize); |
| 72 | + NativeMethods.NtQueryObject(handle, NativeMethods.ObjectInformationClass.ObjectNameInformation, stringBuffer, bufferSize, out _); |
| 73 | + NativeMethods.CloseHandle(handle); |
| 74 | + var handleName = ConvertToString(stringBuffer); |
| 75 | + Marshal.FreeHGlobal(stringBuffer); |
| 76 | + return handleName; |
| 77 | + } |
| 78 | + |
| 79 | + private static IntPtr GetAllHandles() |
| 80 | + { |
| 81 | + int bufferSize = 0x10000; |
| 82 | + var pSysInfoBuffer = Marshal.AllocHGlobal(bufferSize); |
| 83 | + var queryResult = NativeMethods.NtQuerySystemInformation(NativeMethods.SystemInformationClass.SystemHandleInformation, |
| 84 | + pSysInfoBuffer, bufferSize, out _); |
| 85 | + |
| 86 | + while (queryResult == NativeMethods.NtStatus.STATUS_INFO_LENGTH_MISMATCH) |
| 87 | + { |
| 88 | + Marshal.FreeHGlobal(pSysInfoBuffer); |
| 89 | + bufferSize *= 2; |
| 90 | + pSysInfoBuffer = Marshal.AllocHGlobal(bufferSize); |
| 91 | + queryResult = NativeMethods.NtQuerySystemInformation(NativeMethods.SystemInformationClass.SystemHandleInformation, |
| 92 | + pSysInfoBuffer, bufferSize, out _); |
| 93 | + } |
| 94 | + |
| 95 | + if (queryResult == NativeMethods.NtStatus.STATUS_SUCCESS) |
| 96 | + { |
| 97 | + return pSysInfoBuffer; |
| 98 | + } |
| 99 | + else |
| 100 | + { |
| 101 | + Marshal.FreeHGlobal(pSysInfoBuffer); |
| 102 | + return IntPtr.Zero; |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + private static int GetHandleNameLength(IntPtr handle) |
| 107 | + { |
| 108 | + var infoBufferSize = Marshal.SizeOf(typeof(NativeMethods.ObjectBasicInformation)); |
| 109 | + var pInfoBuffer = Marshal.AllocHGlobal(infoBufferSize); |
| 110 | + NativeMethods.NtQueryObject(handle, NativeMethods.ObjectInformationClass.ObjectBasicInformation, pInfoBuffer, infoBufferSize, out _); |
| 111 | + NativeMethods.ObjectBasicInformation objInfo = (NativeMethods.ObjectBasicInformation)Marshal.PtrToStructure(pInfoBuffer, typeof(NativeMethods.ObjectBasicInformation)); |
| 112 | + Marshal.FreeHGlobal(pInfoBuffer); |
| 113 | + if (objInfo.NameInformationLength == 0) |
| 114 | + { |
| 115 | + return 0x100; |
| 116 | + } |
| 117 | + else |
| 118 | + { |
| 119 | + return (int)objInfo.NameInformationLength; |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + private static string ConvertToString(IntPtr stringBuffer) |
| 124 | + { |
| 125 | + var baseAddress = stringBuffer.ToInt64(); |
| 126 | + var offset = IntPtr.Size * 2; |
| 127 | + var handleName = Marshal.PtrToStringUni(new IntPtr(baseAddress + offset)); |
| 128 | + return handleName; |
| 129 | + } |
| 130 | + |
| 131 | + private static bool CloseOwnedHandle(uint processId, IntPtr handleToClose) |
| 132 | + { |
| 133 | + var processHandle = NativeMethods.OpenProcess(NativeMethods.ProcessAccessFlags.All, false, processId); |
| 134 | + var success = NativeMethods.DuplicateHandle(processHandle, handleToClose, IntPtr.Zero, out _, 0, false, NativeMethods.DuplicateOptions.DUPLICATE_CLOSE_SOURCE); |
| 135 | + NativeMethods.CloseHandle(processHandle); |
| 136 | + return success; |
| 137 | + } |
| 138 | + } |
| 139 | +} |
0 commit comments