|
6 | 6 | using Windows.Win32.System.RestartManager; |
7 | 7 | using Windows.Win32.System.WindowsProgramming; |
8 | 8 | using static deadlock_dotnet_sdk.Domain.FileLockerEx; |
| 9 | +using static deadlock_dotnet_sdk.Domain.NativeMethods; |
9 | 10 | using static Windows.Win32.PInvoke; |
10 | 11 | using NTSTATUS = PInvoke.NTSTATUS; |
11 | 12 | using Win32Exception = System.ComponentModel.Win32Exception; |
@@ -40,85 +41,82 @@ internal static partial class NativeMethods |
40 | 41 | /// <param name="path">Path to the file</param> |
41 | 42 | /// <param name="rethrowExceptions">True if inner exceptions should be rethrown, otherwise false</param> |
42 | 43 | /// <returns>A collection of processes that are locking a file</returns> |
43 | | - internal static IEnumerable<Process> FindLockingProcesses(string path, bool rethrowExceptions) |
| 44 | + internal static unsafe IEnumerable<Process> FindLockingProcesses(string path, bool rethrowExceptions) |
44 | 45 | { |
45 | | - unsafe |
| 46 | + using (PWSTR key = new((char*)Marshal.StringToHGlobalUni(Guid.NewGuid().ToString()))) |
46 | 47 | { |
47 | | - using (PWSTR key = new((char*)Marshal.StringToHGlobalUni(Guid.NewGuid().ToString()))) |
| 48 | + List<Process> processes = new(); |
| 49 | + |
| 50 | + // todo: new RmStartSession overload in CsWin32_NativeMethods.cs which can throw a StartSessionException derived from System.ComponentModel.Win32Exception |
| 51 | + // Why? <c>new Win32Exception()</c> will get the last PInvoke error code in addition to the system's message for that Win32ErrorCode. |
| 52 | + uint res = RmStartSession(out var handle, 0, key); |
| 53 | + if (res != 0) |
48 | 54 | { |
49 | | - List<Process> processes = new(); |
| 55 | + throw new StartSessionException(); |
| 56 | + } |
50 | 57 |
|
51 | | - // todo: new RmStartSession overload in CsWin32_NativeMethods.cs which can throw a StartSessionException derived from System.ComponentModel.Win32Exception |
52 | | - // Why? <c>new Win32Exception()</c> will get the last PInvoke error code in addition to the system's message for that Win32ErrorCode. |
53 | | - uint res = RmStartSession(out var handle, 0, key); |
54 | | - if (res != 0) |
55 | | - { |
56 | | - throw new StartSessionException(); |
57 | | - } |
| 58 | + try |
| 59 | + { |
| 60 | + const int errorMoreData = 234; |
| 61 | + uint pnProcInfo = 0; |
| 62 | + uint lpdwRebootReasons = RmRebootReasonNone; |
| 63 | + |
| 64 | + string[] resources = { path }; |
58 | 65 |
|
59 | | - try |
| 66 | + // "using" blocks have hidden "finally" blocks which are executed before exceptions leave this context. |
| 67 | + using (PWSTR pResources = (char*)Marshal.StringToHGlobalUni(path)) |
60 | 68 | { |
61 | | - const int errorMoreData = 234; |
62 | | - uint pnProcInfo = 0; |
63 | | - uint lpdwRebootReasons = RmRebootReasonNone; |
| 69 | + res = RmRegisterResources(handle, new Span<PWSTR>(new PWSTR[] { pResources }), rgApplications: new(), new()); |
64 | 70 |
|
65 | | - string[] resources = { path }; |
| 71 | + if (res != 0) |
| 72 | + { |
| 73 | + throw new RegisterResourceException(); |
| 74 | + } |
| 75 | + |
| 76 | + res = RmGetList(handle, out var pnProcInfoNeeded, ref pnProcInfo, null, out lpdwRebootReasons); |
66 | 77 |
|
67 | | - // "using" blocks have hidden "finally" blocks which are executed before exceptions leave this context. |
68 | | - using (PWSTR pResources = (char*)Marshal.StringToHGlobalUni(path)) |
| 78 | + if (res == errorMoreData) |
69 | 79 | { |
70 | | - res = RmRegisterResources(handle, new Span<PWSTR>(new PWSTR[] { pResources }), rgApplications: new(), new()); |
| 80 | + ReadOnlySpan<RM_PROCESS_INFO> processInfo = new RM_PROCESS_INFO[pnProcInfoNeeded]; |
| 81 | + pnProcInfo = pnProcInfoNeeded; |
71 | 82 |
|
72 | | - if (res != 0) |
| 83 | + fixed (RM_PROCESS_INFO* pProcessInfo = processInfo) |
73 | 84 | { |
74 | | - throw new RegisterResourceException(); |
| 85 | + res = RmGetList(handle, out pnProcInfoNeeded, ref pnProcInfo, pProcessInfo, out lpdwRebootReasons); |
75 | 86 | } |
76 | | - |
77 | | - res = RmGetList(handle, out var pnProcInfoNeeded, ref pnProcInfo, null, out lpdwRebootReasons); |
78 | | - |
79 | | - if (res == errorMoreData) |
| 87 | + if (res == 0) |
80 | 88 | { |
81 | | - ReadOnlySpan<RM_PROCESS_INFO> processInfo = new RM_PROCESS_INFO[pnProcInfoNeeded]; |
82 | | - pnProcInfo = pnProcInfoNeeded; |
| 89 | + processes = new List<Process>((int)pnProcInfo); |
83 | 90 |
|
84 | | - fixed (RM_PROCESS_INFO* pProcessInfo = processInfo) |
85 | | - { |
86 | | - res = RmGetList(handle, out pnProcInfoNeeded, ref pnProcInfo, pProcessInfo, out lpdwRebootReasons); |
87 | | - } |
88 | | - if (res == 0) |
| 91 | + for (int i = 0; i < pnProcInfo; i++) |
89 | 92 | { |
90 | | - processes = new List<Process>((int)pnProcInfo); |
91 | | - |
92 | | - for (int i = 0; i < pnProcInfo; i++) |
| 93 | + try |
93 | 94 | { |
94 | | - try |
95 | | - { |
96 | | - processes.Add(Process.GetProcessById((int)processInfo[i].Process.dwProcessId)); |
97 | | - } |
98 | | - catch (ArgumentException) |
99 | | - { |
100 | | - if (rethrowExceptions) throw; |
101 | | - } |
| 95 | + processes.Add(Process.GetProcessById((int)processInfo[i].Process.dwProcessId)); |
| 96 | + } |
| 97 | + catch (ArgumentException) |
| 98 | + { |
| 99 | + if (rethrowExceptions) throw; |
102 | 100 | } |
103 | | - } |
104 | | - else |
105 | | - { |
106 | | - throw new RmListException(); |
107 | 101 | } |
108 | 102 | } |
109 | | - else if (res != 0) |
| 103 | + else |
110 | 104 | { |
111 | | - throw new UnauthorizedAccessException(); |
| 105 | + throw new RmListException(); |
112 | 106 | } |
113 | 107 | } |
| 108 | + else if (res != 0) |
| 109 | + { |
| 110 | + throw new UnauthorizedAccessException(); |
| 111 | + } |
114 | 112 | } |
115 | | - finally |
116 | | - { |
117 | | - _ = RmEndSession(handle); |
118 | | - } |
119 | | - |
120 | | - return processes; |
121 | 113 | } |
| 114 | + finally |
| 115 | + { |
| 116 | + _ = RmEndSession(handle); |
| 117 | + } |
| 118 | + |
| 119 | + return processes; |
122 | 120 | } |
123 | 121 | } |
124 | 122 |
|
|
0 commit comments