Skip to content

Commit f47b53f

Browse files
committed
CodeQA
1 parent 363717d commit f47b53f

30 files changed

+138
-181
lines changed

FileDialogCOM/FileDialogNative.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ private static ValueTask<object> GetPickerOpenTask<T>(object defaultValue, Dicti
7474
{
7575
IShellItem resShell;
7676
dialog.GetResult(out resShell);
77-
resShell!.GetDisplayName(SIGDN.SIGDN_FILESYSPATH, out nint resultPtr);
77+
resShell.GetDisplayName(SIGDN.SIGDN_FILESYSPATH, out nint resultPtr);
7878
return new ValueTask<object>(COMPtrToUnicodeString(resultPtr) ?? "");
7979
}
8080
}
@@ -108,7 +108,7 @@ private static ValueTask<string> GetPickerSaveTask<T>(string defaultValue, Dicti
108108

109109
IShellItem resShell;
110110
dialog.GetResult(out resShell);
111-
resShell!.GetDisplayName(SIGDN.SIGDN_FILESYSPATH, out nint resultPtr);
111+
resShell.GetDisplayName(SIGDN.SIGDN_FILESYSPATH, out nint resultPtr);
112112
return new ValueTask<string>(COMPtrToUnicodeString(resultPtr) ?? "");
113113
}
114114
finally
@@ -155,7 +155,7 @@ private static nint ArrayToHGlobalPtr<T>(T[] array)
155155
{
156156
int sizeOf = Marshal.SizeOf<T>();
157157

158-
nint structPtr = Marshal.AllocHGlobal(sizeOf * array!.Length);
158+
nint structPtr = Marshal.AllocHGlobal(sizeOf * array.Length);
159159
long partPtrLong = structPtr.ToInt64();
160160
for (int i = 0; i < array.Length; i++)
161161
{

FileDialogCOM/Interfaces.cs

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Runtime.InteropServices;
33
using System.Runtime.InteropServices.Marshalling;
4+
// ReSharper disable PartialTypeWithSinglePart
45

56
namespace Hi3Helper.Win32.FileDialogCOM
67
{

Hi3Helper.Win32.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
<Authors>$(Company). neon-nyan, Cry0, bagusnl, shatyuka, gablm.</Authors>
2020
<Copyright>Copyright 2022-2024 $(Company)</Copyright>
2121
<IsAotCompatible>true</IsAotCompatible>
22+
<DisableRuntimeMarshalling>true</DisableRuntimeMarshalling>
2223
</PropertyGroup>
2324

2425
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">

ManagedTools/Clipboard.cs

+3-4
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,14 @@ namespace Hi3Helper.Win32.Native.ManagedTools
77
{
88
public static class Clipboard
99
{
10-
public static unsafe void CopyStringToClipboard(string? inputString, ILogger? logger = null)
10+
public static void CopyStringToClipboard(string? inputString, ILogger? logger = null)
1111
{
1212
// Flag constants
1313
const GLOBAL_ALLOC_FLAGS AllocFlags = GLOBAL_ALLOC_FLAGS.GMEM_MOVEABLE | GLOBAL_ALLOC_FLAGS.GMEM_ZEROINIT;
1414
const StandardClipboardFormats ClipboardFlags = StandardClipboardFormats.CF_UNICODETEXT;
1515

1616
// Initialize the memory pointer
17-
nint hGlobalAllocPtrPinned = nint.Zero;
18-
nint hGlobalAllocPtr = nint.Zero;
17+
nint hGlobalAllocPtr = nint.Zero;
1918
nint mStrToHGlobalPtr = nint.Zero;
2019

2120
// Get the inputString length and allocate the buffer size
@@ -55,7 +54,7 @@ public static unsafe void CopyStringToClipboard(string? inputString, ILogger? lo
5554
}
5655

5756
// Lock the Global for writing
58-
hGlobalAllocPtrPinned = PInvoke.GlobalLock(hGlobalAllocPtr);
57+
var hGlobalAllocPtrPinned = PInvoke.GlobalLock(hGlobalAllocPtr);
5958
if (hGlobalAllocPtrPinned == nint.Zero)
6059
{
6160
logger?.LogError($"[InvokeProp::CopyStringToClipboard()] Cannot lock global buffer for writing! Error: {Win32Error.GetLastWin32ErrorMessage()}");

ManagedTools/DriveTypeChecker.cs

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Runtime.InteropServices;
88
using static Hi3Helper.Win32.Native.LibraryImport.PInvoke;
99
// ReSharper disable UnusedMember.Local
10+
// ReSharper disable ShiftExpressionZeroLeftOperand
1011

1112
namespace Hi3Helper.Win32.Native.ManagedTools;
1213

ManagedTools/Sleep.cs

+2-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using Microsoft.Extensions.Logging;
33
using System;
44
using System.Diagnostics;
5-
using System.Diagnostics.CodeAnalysis;
65
using System.Threading;
76
using System.Threading.Tasks;
87
using static Hi3Helper.Win32.Native.LibraryImport.PInvoke;
@@ -53,9 +52,9 @@ public static async void PreventSleep(ILogger? logger = null)
5352
_logger?.LogInformation("[InvokeProp::PreventSleep()] Starting to prevent sleep!");
5453
// Uncomment once issue is resolved
5554
// _logger?.LogInformation($"[InvokeProp::PreventSleep()] Called by {new System.Diagnostics.StackTrace().GetFrame(2)?.GetMethod()!}");
56-
_logger?.LogDebug($"[InvokeProp::RestoreSleep()] Called by {new System.Diagnostics.StackTrace()}");
55+
_logger?.LogDebug($"[InvokeProp::RestoreSleep()] Called by {new StackTrace()}");
5756
#if DEBUG
58-
_logger?.LogDebug($"[InvokeProp::RestoreSleep()] Called by {new System.Diagnostics.StackTrace()}");
57+
_logger?.LogDebug($"[InvokeProp::RestoreSleep()] Called by {new StackTrace()}");
5958
#endif
6059
while (!_preventSleepToken.IsCancellationRequested)
6160
{

Native/Enums/ExecutionState.cs

-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
62

73
namespace Hi3Helper.Win32.Native.Enums
84
{

Native/Enums/GWL_INDEX.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace Hi3Helper.Win32.Native.Enums
22
{
3-
public enum GWL_INDEX : int
3+
public enum GWL_INDEX
44
{
55
GWLP_WNDPROC = -4,
66
GWLP_HINSTANCE = -6,

Native/Enums/HKEYCLASS.cs

-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
62

73
namespace Hi3Helper.Win32.Native.Enums
84
{

Native/Enums/RegChangeNotifyFilter.cs

-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22

33
namespace Hi3Helper.Win32.Native.Enums
44
{
5-
/// <summary>
6-
/// Filter for notifications reported by <see cref="RegistryMonitor"/>.
7-
/// </summary>
85
[Flags]
96
public enum RegChangeNotifyFilter
107
{

Native/Structs/BIND_OPTS3.cs

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using Hi3Helper.Win32.Native.Enums;
2-
using System;
32
using System.Runtime.InteropServices;
43

54
namespace Hi3Helper.Win32.Native.Structs

Native/Structs/HResult.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public readonly struct HResult
3434
/// <c>new nint(-1)</c> to ignore the current IErrorInfo interface and construct the exception
3535
/// just from the error code.
3636
/// </param>
37-
/// <returns><see langword="this"/><see cref="HRESULT"/>, if it does not reflect an error.</returns>
37+
/// <returns><see langword="this"/><see cref="HResult"/>, if it does not reflect an error.</returns>
3838
/// <seealso cref="Marshal.ThrowExceptionForHR(int, nint)"/>
3939
public HResult ThrowOnFailure(nint errorInfo = default)
4040
{

ShellLinkCOM/IPersist.cs

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Runtime.InteropServices;
33
using System.Runtime.InteropServices.Marshalling;
4+
// ReSharper disable PartialTypeWithSinglePart
45

56
namespace Hi3Helper.Win32.ShellLinkCOM
67
{

ShellLinkCOM/IPersistFile.cs

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Runtime.InteropServices;
33
using System.Runtime.InteropServices.Marshalling;
4+
// ReSharper disable PartialTypeWithSinglePart
45

56
namespace Hi3Helper.Win32.ShellLinkCOM
67
{

ShellLinkCOM/IPropertyStore.cs

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Runtime.InteropServices;
22
using System.Runtime.InteropServices.Marshalling;
3+
// ReSharper disable PartialTypeWithSinglePart
34

45
namespace Hi3Helper.Win32.ShellLinkCOM
56
{

ShellLinkCOM/IShellLinkW.cs

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Runtime.InteropServices;
22
using System.Runtime.InteropServices.Marshalling;
3+
// ReSharper disable PartialTypeWithSinglePart
34

45
namespace Hi3Helper.Win32.ShellLinkCOM
56
{

ShellLinkCOM/ShellLink.cs

+19-59
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System.Runtime.Versioning;
1010
// ReSharper disable ArrangeTypeMemberModifiers
1111
// ReSharper disable NotAccessedField.Local
12+
// ReSharper disable PartialTypeWithSinglePart
1213

1314
#pragma warning disable CS8500 // This takes the address of, gets the size of, or declares a pointer to a managed type
1415
namespace Hi3Helper.Win32.ShellLinkCOM
@@ -94,25 +95,24 @@ public nint GetIcon(bool large)
9495
else
9596
{
9697
// Use ExtractIconEx to get the icon:
97-
nint[] hIconEx = new nint[1] { nint.Zero };
98-
uint iconCount = 0;
98+
nint[] hIconEx = [nint.Zero];
9999
if (large)
100100
{
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);
107107
}
108108
else
109109
{
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);
116116
}
117117

118118
return hIconEx[0];
@@ -154,7 +154,7 @@ public unsafe int IconIndex
154154
/// </summary>
155155
public unsafe string Target
156156
{
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));
158158
set => linkW?.SetPath(_target = value);
159159
}
160160

@@ -217,20 +217,12 @@ public short HotKey
217217
set => linkW?.SetHotkey(value);
218218
}
219219

220-
private unsafe Win32FindDataW* GetWin32FindDataFromBuffer(byte[] buffer)
220+
private unsafe string GetStringAndW32FindDataFromIMethod(int length, ToDelegateWithW32FindDataInvoke toInvokeDelegate)
221221
{
222-
fixed (void* refPtr = &buffer[0])
223-
{
224-
return (Win32FindDataW*)refPtr;
225-
}
226-
}
222+
int sizeOfFindData = Marshal.SizeOf<Win32FindDataW>();
227223

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);
234226
try
235227
{
236228
fixed (char* bufferPtr = &buffer[0])
@@ -272,26 +264,6 @@ private static unsafe string GetStringFromNullTerminatedPtr(char* bufferPtr)
272264
return outString;
273265
}
274266

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-
295267
/// <summary>
296268
/// Sets the appUserModelId
297269
/// </summary>
@@ -372,18 +344,6 @@ public void Save()
372344
/// <param name="timeOut">Timeout if SLR_NO_UI is specified, in ms.</param>
373345
public void Open(string linkFile, nint hWnd, EShellLinkResolveFlags resolveFlags, ushort timeOut)
374346
{
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-
387347
persistFileW?.Load(linkFile, 0);
388348
this.shortcutFile = linkFile;
389349
}

TaskbarListCOM/Interfaces.cs

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Runtime.InteropServices;
22
using System.Runtime.InteropServices.Marshalling;
3+
// ReSharper disable PartialTypeWithSinglePart
34

45
namespace Hi3Helper.Win32.TaskbarListCOM
56
{

ToastCOM/Extensions.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ namespace Hi3Helper.Win32.ToastCOM
55
public static class Extensions
66
{
77
public static Guid GetGuidFromString(string str)
8-
=> CLSIDGuid.GetGuidFromString(str);
8+
=> ClsidGuid.GetGuidFromString(str);
99
}
1010
}

ToastCOM/Guid.cs

+6-5
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@
22
using System.Buffers;
33
using System.Security.Cryptography;
44
using System.Text;
5+
// ReSharper disable InconsistentNaming
56

67
namespace Hi3Helper.Win32.ToastCOM
78
{
8-
internal class IIDGuid
9+
internal class IidGuid
910
{
10-
internal const string INotificationActivationCallback = "53e31837-6600-4a81-9395-75cffe746f94";
11-
internal const string IClassFactory = "00000001-0000-0000-c000-000000000046";
12-
internal static Guid Guid_IClassFactory = new(IClassFactory);
11+
internal const string NotificationActivationCallback = "53e31837-6600-4a81-9395-75cffe746f94";
12+
internal const string IClassFactory = "00000001-0000-0000-c000-000000000046";
13+
internal static Guid GuidIClassFactory = new(IClassFactory);
1314
}
1415

15-
internal class CLSIDGuid
16+
internal class ClsidGuid
1617
{
1718
internal static Guid GetGuidFromString(string fromString)
1819
{

ToastCOM/Interfaces.cs

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
using System;
22
using System.Runtime.InteropServices;
33
using System.Runtime.InteropServices.Marshalling;
4+
// ReSharper disable CommentTypo
5+
// ReSharper disable IdentifierTypo
6+
// ReSharper disable PartialTypeWithSinglePart
47

58
namespace Hi3Helper.Win32.ToastCOM
69
{
7-
[Guid(IIDGuid.INotificationActivationCallback)]
10+
[Guid(IidGuid.NotificationActivationCallback)]
811
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
912
[GeneratedComInterface(Options = ComInterfaceOptions.ManagedObjectWrapper | ComInterfaceOptions.ComObjectWrapper, StringMarshalling = StringMarshalling.Utf16)]
1013
[ComVisible(true)]
@@ -18,7 +21,7 @@ uint dataCount
1821
);
1922
}
2023

21-
[Guid(IIDGuid.IClassFactory)]
24+
[Guid(IidGuid.IClassFactory)]
2225
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
2326
[GeneratedComInterface(Options = ComInterfaceOptions.ManagedObjectWrapper | ComInterfaceOptions.ComObjectWrapper)]
2427
public partial interface IClassFactory

ToastCOM/Notification/DesktopBridgeHelpers.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace Hi3Helper.Win32.ToastCOM.Notification
88
/// </summary>
99
internal class DesktopBridgeHelpers
1010
{
11-
const long APPMODEL_ERROR_NO_PACKAGE = 15700L;
11+
private const long AppModelErrorNoPackage = 15700L;
1212

1313
private static bool? _isRunningAsUwp;
1414
public static bool IsRunningAsUwp()
@@ -26,7 +26,7 @@ public static bool IsRunningAsUwp()
2626
{
2727
int len = 0;
2828
int result = PInvoke.GetCurrentPackageFullName(ref len, out _);
29-
_isRunningAsUwp = result != APPMODEL_ERROR_NO_PACKAGE;
29+
_isRunningAsUwp = result != AppModelErrorNoPackage;
3030
}
3131

3232
return _isRunningAsUwp.Value;

0 commit comments

Comments
 (0)