|
9 | 9 | using System.Runtime.Versioning;
|
10 | 10 | // ReSharper disable ArrangeTypeMemberModifiers
|
11 | 11 | // ReSharper disable NotAccessedField.Local
|
| 12 | +// ReSharper disable PartialTypeWithSinglePart |
12 | 13 |
|
13 | 14 | #pragma warning disable CS8500 // This takes the address of, gets the size of, or declares a pointer to a managed type
|
14 | 15 | namespace Hi3Helper.Win32.ShellLinkCOM
|
@@ -94,25 +95,24 @@ public nint GetIcon(bool large)
|
94 | 95 | else
|
95 | 96 | {
|
96 | 97 | // Use ExtractIconEx to get the icon:
|
97 |
| - nint[] hIconEx = new nint[1] { nint.Zero }; |
98 |
| - uint iconCount = 0; |
| 98 | + nint[] hIconEx = [nint.Zero]; |
99 | 99 | if (large)
|
100 | 100 | {
|
101 |
| - iconCount = PInvoke.ExtractIconEx( |
102 |
| - iconFile, |
103 |
| - iconIndex, |
104 |
| - hIconEx, |
105 |
| - null, |
106 |
| - 1); |
| 101 | + PInvoke.ExtractIconEx( |
| 102 | + iconFile, |
| 103 | + iconIndex, |
| 104 | + hIconEx, |
| 105 | + null, |
| 106 | + 1); |
107 | 107 | }
|
108 | 108 | else
|
109 | 109 | {
|
110 |
| - iconCount = PInvoke.ExtractIconEx( |
111 |
| - iconFile, |
112 |
| - iconIndex, |
113 |
| - null, |
114 |
| - hIconEx, |
115 |
| - 1); |
| 110 | + PInvoke.ExtractIconEx( |
| 111 | + iconFile, |
| 112 | + iconIndex, |
| 113 | + null, |
| 114 | + hIconEx, |
| 115 | + 1); |
116 | 116 | }
|
117 | 117 |
|
118 | 118 | return hIconEx[0];
|
@@ -154,7 +154,7 @@ public unsafe int IconIndex
|
154 | 154 | /// </summary>
|
155 | 155 | public unsafe string Target
|
156 | 156 | {
|
157 |
| - get => _target ??= GetStringAndW32FindDataFromIMethod(260, out _, (ptr, fd, len) => linkW?.GetPath(ptr, len, fd, (uint)EShellLinkGP.SLGP_UNCPRIORITY)); |
| 157 | + get => _target ??= GetStringAndW32FindDataFromIMethod(260, (ptr, fd, len) => linkW?.GetPath(ptr, len, fd, (uint)EShellLinkGP.SLGP_UNCPRIORITY)); |
158 | 158 | set => linkW?.SetPath(_target = value);
|
159 | 159 | }
|
160 | 160 |
|
@@ -217,20 +217,12 @@ public short HotKey
|
217 | 217 | set => linkW?.SetHotkey(value);
|
218 | 218 | }
|
219 | 219 |
|
220 |
| - private unsafe Win32FindDataW* GetWin32FindDataFromBuffer(byte[] buffer) |
| 220 | + private unsafe string GetStringAndW32FindDataFromIMethod(int length, ToDelegateWithW32FindDataInvoke toInvokeDelegate) |
221 | 221 | {
|
222 |
| - fixed (void* refPtr = &buffer[0]) |
223 |
| - { |
224 |
| - return (Win32FindDataW*)refPtr; |
225 |
| - } |
226 |
| - } |
| 222 | + int sizeOfFindData = Marshal.SizeOf<Win32FindDataW>(); |
227 | 223 |
|
228 |
| - private unsafe string GetStringAndW32FindDataFromIMethod(int length, out byte[] win32FindDataBuffer, ToDelegateWithW32FindDataInvoke toInvokeDelegate) |
229 |
| - { |
230 |
| - int sizeOfFindData = Marshal.SizeOf<Win32FindDataW>(); |
231 |
| - |
232 |
| - win32FindDataBuffer = new byte[sizeOfFindData]; |
233 |
| - char[] buffer = ArrayPool<char>.Shared.Rent(length); |
| 224 | + var win32FindDataBuffer = new byte[sizeOfFindData]; |
| 225 | + char[] buffer = ArrayPool<char>.Shared.Rent(length); |
234 | 226 | try
|
235 | 227 | {
|
236 | 228 | fixed (char* bufferPtr = &buffer[0])
|
@@ -272,26 +264,6 @@ private static unsafe string GetStringFromNullTerminatedPtr(char* bufferPtr)
|
272 | 264 | return outString;
|
273 | 265 | }
|
274 | 266 |
|
275 |
| - private unsafe string GetFileNameFromDataPtr(Win32FindDataW* win32FindData) |
276 |
| - { |
277 |
| - byte* fdPtr = (byte*)win32FindData; |
278 |
| - void* offset = fdPtr + 44; // Fixed pos of cFileName field |
279 |
| - char* offsetField = (char*)offset; |
280 |
| - |
281 |
| - ReadOnlySpan<char> spanNullTerminated = MemoryMarshal.CreateReadOnlySpanFromNullTerminated(offsetField); |
282 |
| - return spanNullTerminated.ToString(); |
283 |
| - } |
284 |
| - |
285 |
| - private unsafe string GetAlternativeFileNameFromDataPtr(Win32FindDataW* win32FindData) |
286 |
| - { |
287 |
| - byte* fdPtr = (byte*)win32FindData; |
288 |
| - void* offset = fdPtr + 44 + 520; // Fixed pos of cFileName + sizeof(cFileName) field |
289 |
| - char* offsetField = (char*)offset; |
290 |
| - |
291 |
| - ReadOnlySpan<char> spanNullTerminated = MemoryMarshal.CreateReadOnlySpanFromNullTerminated(offsetField); |
292 |
| - return spanNullTerminated.ToString(); |
293 |
| - } |
294 |
| - |
295 | 267 | /// <summary>
|
296 | 268 | /// Sets the appUserModelId
|
297 | 269 | /// </summary>
|
@@ -372,18 +344,6 @@ public void Save()
|
372 | 344 | /// <param name="timeOut">Timeout if SLR_NO_UI is specified, in ms.</param>
|
373 | 345 | public void Open(string linkFile, nint hWnd, EShellLinkResolveFlags resolveFlags, ushort timeOut)
|
374 | 346 | {
|
375 |
| - uint flags; |
376 |
| - |
377 |
| - if ((resolveFlags & EShellLinkResolveFlags.SLR_NO_UI) |
378 |
| - == EShellLinkResolveFlags.SLR_NO_UI) |
379 |
| - { |
380 |
| - flags = (uint)((int)resolveFlags | (timeOut << 16)); |
381 |
| - } |
382 |
| - else |
383 |
| - { |
384 |
| - flags = (uint)resolveFlags; |
385 |
| - } |
386 |
| - |
387 | 347 | persistFileW?.Load(linkFile, 0);
|
388 | 348 | this.shortcutFile = linkFile;
|
389 | 349 | }
|
|
0 commit comments