Skip to content

Code Quality: Replaced DllImport calls with CsWin32 generations for CancelIO and related methods #17108

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Files.App.CsWin32/NativeMethods.txt
Original file line number Diff line number Diff line change
@@ -45,7 +45,7 @@
GetKeyState
CreateDirectoryFromApp
WNetCancelConnection2
NET_USE_CONNECT_FLAGS

Check warning on line 48 in src/Files.App.CsWin32/NativeMethods.txt

GitHub Actions / build (Debug, x64)

Method, type or constant "NET_USE_CONNECT_FLAGS" not found

Check warning on line 48 in src/Files.App.CsWin32/NativeMethods.txt

GitHub Actions / build (Debug, arm64)

Method, type or constant "NET_USE_CONNECT_FLAGS" not found

Check warning on line 48 in src/Files.App.CsWin32/NativeMethods.txt

GitHub Actions / build (Release, arm64)

Method, type or constant "NET_USE_CONNECT_FLAGS" not found

Check warning on line 48 in src/Files.App.CsWin32/NativeMethods.txt

GitHub Actions / build (Release, x64)

Method, type or constant "NET_USE_CONNECT_FLAGS" not found
NETRESOURCEW
WNetAddConnection3
CREDENTIALW
@@ -78,7 +78,7 @@
SetEntriesInAcl
ACL_SIZE_INFORMATION
DeleteAce
EXPLICIT_ACCESS

Check warning on line 81 in src/Files.App.CsWin32/NativeMethods.txt

GitHub Actions / build (Debug, x64)

Method, type or constant "EXPLICIT_ACCESS" not found. Did you mean or "EXPLICIT_ACCESS_A" or "EXPLICIT_ACCESS_W"?

Check warning on line 81 in src/Files.App.CsWin32/NativeMethods.txt

GitHub Actions / build (Debug, arm64)

Method, type or constant "EXPLICIT_ACCESS" not found. Did you mean or "EXPLICIT_ACCESS_A" or "EXPLICIT_ACCESS_W"?

Check warning on line 81 in src/Files.App.CsWin32/NativeMethods.txt

GitHub Actions / build (Release, arm64)

Method, type or constant "EXPLICIT_ACCESS" not found. Did you mean or "EXPLICIT_ACCESS_A" or "EXPLICIT_ACCESS_W"?

Check warning on line 81 in src/Files.App.CsWin32/NativeMethods.txt

GitHub Actions / build (Release, x64)

Method, type or constant "EXPLICIT_ACCESS" not found. Did you mean or "EXPLICIT_ACCESS_A" or "EXPLICIT_ACCESS_W"?
ACCESS_ALLOWED_ACE
LookupAccountSid
GetComputerName
@@ -134,7 +134,7 @@
CoTaskMemFree
QueryDosDevice
DeviceIoControl
GetLastError

Check warning on line 137 in src/Files.App.CsWin32/NativeMethods.txt

GitHub Actions / build (Debug, x64)

This API will not be generated. Do not generate GetLastError. Call Marshal.GetLastWin32Error() instead. Learn more from https://docs.microsoft.com/dotnet/api/system.runtime.interopservices.marshal.getlastwin32error

Check warning on line 137 in src/Files.App.CsWin32/NativeMethods.txt

GitHub Actions / build (Debug, arm64)

This API will not be generated. Do not generate GetLastError. Call Marshal.GetLastWin32Error() instead. Learn more from https://docs.microsoft.com/dotnet/api/system.runtime.interopservices.marshal.getlastwin32error

Check warning on line 137 in src/Files.App.CsWin32/NativeMethods.txt

GitHub Actions / build (Release, arm64)

This API will not be generated. Do not generate GetLastError. Call Marshal.GetLastWin32Error() instead. Learn more from https://docs.microsoft.com/dotnet/api/system.runtime.interopservices.marshal.getlastwin32error

Check warning on line 137 in src/Files.App.CsWin32/NativeMethods.txt

GitHub Actions / build (Release, x64)

This API will not be generated. Do not generate GetLastError. Call Marshal.GetLastWin32Error() instead. Learn more from https://docs.microsoft.com/dotnet/api/system.runtime.interopservices.marshal.getlastwin32error
CreateFile
GetVolumeInformation
COMPRESSION_FORMAT
@@ -226,3 +226,7 @@
RoGetAgileReference
IQueryInfo
QITIPF_FLAGS
CancelIoEx
OVERLAPPED

Check warning on line 230 in src/Files.App.CsWin32/NativeMethods.txt

GitHub Actions / build (Debug, x64)

This API will not be generated. Use System.Threading.NativeOverlapped instead.

Check warning on line 230 in src/Files.App.CsWin32/NativeMethods.txt

GitHub Actions / build (Debug, arm64)

This API will not be generated. Use System.Threading.NativeOverlapped instead.

Check warning on line 230 in src/Files.App.CsWin32/NativeMethods.txt

GitHub Actions / build (Release, arm64)

This API will not be generated. Use System.Threading.NativeOverlapped instead.

Check warning on line 230 in src/Files.App.CsWin32/NativeMethods.txt

GitHub Actions / build (Release, x64)

This API will not be generated. Use System.Threading.NativeOverlapped instead.
CreateEvent
ReadDirectoryChangesW
11 changes: 0 additions & 11 deletions src/Files.App/Helpers/Win32/Win32PInvoke.Methods.cs
Original file line number Diff line number Diff line change
@@ -115,17 +115,6 @@ public static extern bool GetOverlappedResult(
bool bWait
);

[DllImport("api-ms-win-core-io-l1-1-1.dll")]
public static extern bool CancelIo(
IntPtr hFile
);

[DllImport("api-ms-win-core-io-l1-1-1.dll")]
public static extern bool CancelIoEx(
IntPtr hFile,
IntPtr lpOverlapped
);

[DllImport("api-ms-win-core-synch-l1-2-0.dll")]
public static extern uint WaitForMultipleObjectsEx(
uint nCount,
54 changes: 32 additions & 22 deletions src/Files.App/ViewModels/ShellViewModel.cs
Original file line number Diff line number Diff line change
@@ -14,6 +14,9 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Vanara.Windows.Shell;
using Windows.Win32;
using Windows.Win32.Foundation;
using Windows.Win32.Storage.FileSystem;
using Windows.Foundation;
using Windows.Storage;
using Windows.Storage.FileProperties;
@@ -23,6 +26,8 @@
using FileAttributes = System.IO.FileAttributes;
using ByteSize = ByteSizeLib.ByteSize;
using Windows.Win32.System.SystemServices;
using FINDEX_SEARCH_OPS = Windows.Win32.Storage.FileSystem.FINDEX_SEARCH_OPS;
using FINDEX_INFO_LEVELS = Windows.Win32.Storage.FileSystem.FINDEX_INFO_LEVELS;

namespace Files.App.ViewModels
{
@@ -1748,7 +1753,7 @@
var findInfoLevel = FINDEX_INFO_LEVELS.FindExInfoBasic;
var additionalFlags = FIND_FIRST_EX_LARGE_FETCH;

IntPtr hFileTsk = FindFirstFileExFromApp(
IntPtr hFileTsk = PInvoke.FindFirstFileEx(
path + "\\*.*",
findInfoLevel,
out WIN32_FIND_DATA findDataTsk,
@@ -2101,7 +2106,7 @@
});
}

private void WatchForDirectoryChanges(string path, CloudDriveSyncStatus syncStatus)
private unsafe void WatchForDirectoryChanges(string path, CloudDriveSyncStatus syncStatus)
{
Debug.WriteLine($"WatchForDirectoryChanges: {path}");
var hWatchDir = Win32PInvoke.CreateFileFromApp(path, 1, 1 | 2 | 4,
@@ -2206,23 +2211,27 @@
Debug.WriteLine("watcher canceled");
}

CancelIoEx(hWatchDir, IntPtr.Zero);
CloseHandle(hWatchDir);
PInvoke.CancelIoEx((HANDLE)hWatchDir);
PInvoke.CloseHandle((HANDLE)hWatchDir);
});
}

private void WatchForGitChanges()
private unsafe void WatchForGitChanges()
{
var hWatchDir = Win32PInvoke.CreateFileFromApp(
GitDirectory!,
1,
1 | 2 | 4,
IntPtr.Zero,
3,
(uint)Win32PInvoke.File_Attributes.BackupSemantics | (uint)Win32PInvoke.File_Attributes.Overlapped,
IntPtr.Zero);
HANDLE hWatchDir;

if (hWatchDir.ToInt64() == -1)
fixed (char* gitDir = GitDirectory)
{
hWatchDir = PInvoke.CreateFile(
gitDir!,
1,
FILE_SHARE_MODE.FILE_SHARE_READ | FILE_SHARE_MODE.FILE_SHARE_WRITE | FILE_SHARE_MODE.FILE_SHARE_DELETE,
dwCreationDisposition: FILE_CREATION_DISPOSITION.OPEN_EXISTING,
dwFlagsAndAttributes: FILE_FLAGS_AND_ATTRIBUTES.FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAGS_AND_ATTRIBUTES.FILE_FLAG_OVERLAPPED,
hTemplateFile: (HANDLE)null);
}

if (hWatchDir.IsNull)
return;

gitProcessQueueAction ??= Task.Factory.StartNew(() => ProcessGitChangesQueueAsync(watcherCTS.Token), default,
@@ -2234,8 +2243,8 @@
var rand = Guid.NewGuid();
var notifyFilters = FILE_NOTIFY_CHANGE_DIR_NAME | FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_LAST_WRITE | FILE_NOTIFY_CHANGE_SIZE | FILE_NOTIFY_CHANGE_CREATION;

var overlapped = new OVERLAPPED();
overlapped.hEvent = CreateEvent(IntPtr.Zero, false, false, null);
var overlapped = new Overlapped();
overlapped.EventHandleIntPtr = PInvoke.CreateEvent((bManualReset: false, bInitialState: false, lpName: null);

Check failure on line 2247 in src/Files.App/ViewModels/ShellViewModel.cs

GitHub Actions / build (Debug, x64)

) expected

Check failure on line 2247 in src/Files.App/ViewModels/ShellViewModel.cs

GitHub Actions / build (Debug, arm64)

) expected

Check failure on line 2247 in src/Files.App/ViewModels/ShellViewModel.cs

GitHub Actions / build (Release, arm64)

) expected

Check failure on line 2247 in src/Files.App/ViewModels/ShellViewModel.cs

GitHub Actions / build (Release, x64)

) expected
const uint INFINITE = 0xFFFFFFFF;

while (x.Status != AsyncStatus.Canceled)
@@ -2248,15 +2257,16 @@
if (x.Status == AsyncStatus.Canceled)
break;

ReadDirectoryChangesW(hWatchDir, pBuff,
PInvoke.ReadDirectoryChanges(hWatchDir, pBuff,
4096, true,
notifyFilters, null,
ref overlapped, null);
notifyFilters,
lpOverlapped: ref overlapped,
lpCompletionRoutine: null);

if (x.Status == AsyncStatus.Canceled)
break;

var rc = WaitForSingleObjectEx(overlapped.hEvent, INFINITE, true);
var rc = WaitForSingleObjectEx(overlapped.EventHandleIntPtr, INFINITE, true);

uint offset = 0;
ref var notifyInfo = ref Unsafe.As<byte, FILE_NOTIFY_INFORMATION>(ref buff[offset]);
@@ -2280,7 +2290,7 @@
}
}

CloseHandle(overlapped.hEvent);
PInvoke.CloseHandle((HANDLE)overlapped.EventHandleIntPtr);
gitChangesQueue.Clear();
});

@@ -2294,7 +2304,7 @@
gitWatcherAction = null;
}

CancelIoEx(hWatchDir, IntPtr.Zero);
PInvoke.CancelIoEx(hWatchDir);
CloseHandle(hWatchDir);
});
}

Unchanged files with check annotations Beta

/// <summary>
/// Initializes a new instance of the <see cref="BladeItem"/> class.
/// </summary>
public BladeItem()

Check warning on line 18 in src/Files.App.Controls/BladeView/BladeItem.cs

GitHub Actions / build (Debug, x64)

Non-nullable field '_closeButton' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the field as nullable.
{
DefaultStyleKey = typeof(BladeItem);
}
SetHasContent( false );
}
UpdateContent( newContent );

Check warning on line 175 in src/Files.App.Controls/Toolbar/ToolbarToggleButton/ToolbarToggleButton.cs

GitHub Actions / build (Debug, x64)

Possible null reference argument for parameter 'newContent' in 'void ToolbarToggleButton.UpdateContent(object newContent)'.

Check warning on line 175 in src/Files.App.Controls/Toolbar/ToolbarToggleButton/ToolbarToggleButton.cs

GitHub Actions / build (Debug, arm64)

Possible null reference argument for parameter 'newContent' in 'void ToolbarToggleButton.UpdateContent(object newContent)'.

Check warning on line 175 in src/Files.App.Controls/Toolbar/ToolbarToggleButton/ToolbarToggleButton.cs

GitHub Actions / build (Release, arm64)

Possible null reference argument for parameter 'newContent' in 'void ToolbarToggleButton.UpdateContent(object newContent)'.

Check warning on line 175 in src/Files.App.Controls/Toolbar/ToolbarToggleButton/ToolbarToggleButton.cs

GitHub Actions / build (Release, x64)

Possible null reference argument for parameter 'newContent' in 'void ToolbarToggleButton.UpdateContent(object newContent)'.
}
#endregion
var newDocument = context.Document.WithSyntaxRoot(newRoot);
context.RegisterCodeFix(
CodeAction.Create(

Check warning on line 57 in src/Files.Core.SourceGenerator/CodeFixProviders/StringsPropertyCodeFixProvider.cs

GitHub Actions / build (Debug, x64)

Provide an explicit argument for optional parameter 'equivalenceKey', which is non-null and unique for each kind of code action created by this fixer

Check warning on line 57 in src/Files.Core.SourceGenerator/CodeFixProviders/StringsPropertyCodeFixProvider.cs

GitHub Actions / build (Debug, arm64)

Provide an explicit argument for optional parameter 'equivalenceKey', which is non-null and unique for each kind of code action created by this fixer

Check warning on line 57 in src/Files.Core.SourceGenerator/CodeFixProviders/StringsPropertyCodeFixProvider.cs

GitHub Actions / build (Release, arm64)

Provide an explicit argument for optional parameter 'equivalenceKey', which is non-null and unique for each kind of code action created by this fixer

Check warning on line 57 in src/Files.Core.SourceGenerator/CodeFixProviders/StringsPropertyCodeFixProvider.cs

GitHub Actions / build (Release, x64)

Provide an explicit argument for optional parameter 'equivalenceKey', which is non-null and unique for each kind of code action created by this fixer
CodeFixProviderTitle,
c => Task.FromResult(newDocument),
null),
/// <summary>
/// Analyzer that detects if string literals can be replaced with constants from the <c>Strings</c> class.
/// </summary>
[DiagnosticAnalyzer(LanguageNames.CSharp)]

Check warning on line 12 in src/Files.Core.SourceGenerator/Analyzers/StringsPropertyAnalyzer.cs

GitHub Actions / build (Debug, x64)

This compiler extension should not be implemented in an assembly containing a reference to Microsoft.CodeAnalysis.Workspaces. The Microsoft.CodeAnalysis.Workspaces assembly is not provided during command line compilation scenarios, so references to it could cause the compiler extension to behave unpredictably. (https://github.com/dotnet/roslyn-analyzers/blob/main/docs/rules/RS1038.md)

Check warning on line 12 in src/Files.Core.SourceGenerator/Analyzers/StringsPropertyAnalyzer.cs

GitHub Actions / build (Debug, arm64)

This compiler extension should not be implemented in an assembly containing a reference to Microsoft.CodeAnalysis.Workspaces. The Microsoft.CodeAnalysis.Workspaces assembly is not provided during command line compilation scenarios, so references to it could cause the compiler extension to behave unpredictably. (https://github.com/dotnet/roslyn-analyzers/blob/main/docs/rules/RS1038.md)

Check warning on line 12 in src/Files.Core.SourceGenerator/Analyzers/StringsPropertyAnalyzer.cs

GitHub Actions / build (Release, arm64)

This compiler extension should not be implemented in an assembly containing a reference to Microsoft.CodeAnalysis.Workspaces. The Microsoft.CodeAnalysis.Workspaces assembly is not provided during command line compilation scenarios, so references to it could cause the compiler extension to behave unpredictably. (https://github.com/dotnet/roslyn-analyzers/blob/main/docs/rules/RS1038.md)

Check warning on line 12 in src/Files.Core.SourceGenerator/Analyzers/StringsPropertyAnalyzer.cs

GitHub Actions / build (Release, x64)

This compiler extension should not be implemented in an assembly containing a reference to Microsoft.CodeAnalysis.Workspaces. The Microsoft.CodeAnalysis.Workspaces assembly is not provided during command line compilation scenarios, so references to it could cause the compiler extension to behave unpredictably. (https://github.com/dotnet/roslyn-analyzers/blob/main/docs/rules/RS1038.md)
internal sealed class StringsPropertyAnalyzer : DiagnosticAnalyzer
{
/// <summary>
/// <summary>
/// Generates properties for strings based on resource files.
/// </summary>
[Generator]

Check warning on line 13 in src/Files.Core.SourceGenerator/Generators/StringsPropertyGenerator.cs

GitHub Actions / build (Debug, x64)

This compiler extension should not be implemented in an assembly containing a reference to Microsoft.CodeAnalysis.Workspaces. The Microsoft.CodeAnalysis.Workspaces assembly is not provided during command line compilation scenarios, so references to it could cause the compiler extension to behave unpredictably. (https://github.com/dotnet/roslyn-analyzers/blob/main/docs/rules/RS1038.md)

Check warning on line 13 in src/Files.Core.SourceGenerator/Generators/StringsPropertyGenerator.cs

GitHub Actions / build (Debug, arm64)

This compiler extension should not be implemented in an assembly containing a reference to Microsoft.CodeAnalysis.Workspaces. The Microsoft.CodeAnalysis.Workspaces assembly is not provided during command line compilation scenarios, so references to it could cause the compiler extension to behave unpredictably. (https://github.com/dotnet/roslyn-analyzers/blob/main/docs/rules/RS1038.md)

Check warning on line 13 in src/Files.Core.SourceGenerator/Generators/StringsPropertyGenerator.cs

GitHub Actions / build (Release, arm64)

This compiler extension should not be implemented in an assembly containing a reference to Microsoft.CodeAnalysis.Workspaces. The Microsoft.CodeAnalysis.Workspaces assembly is not provided during command line compilation scenarios, so references to it could cause the compiler extension to behave unpredictably. (https://github.com/dotnet/roslyn-analyzers/blob/main/docs/rules/RS1038.md)

Check warning on line 13 in src/Files.Core.SourceGenerator/Generators/StringsPropertyGenerator.cs

GitHub Actions / build (Release, x64)

This compiler extension should not be implemented in an assembly containing a reference to Microsoft.CodeAnalysis.Workspaces. The Microsoft.CodeAnalysis.Workspaces assembly is not provided during command line compilation scenarios, so references to it could cause the compiler extension to behave unpredictably. (https://github.com/dotnet/roslyn-analyzers/blob/main/docs/rules/RS1038.md)
internal sealed class StringsPropertyGenerator : IIncrementalGenerator
{
// Static HashSet to track generated file names
/// <summary>
/// A generator for serializing/deserializing objects to/from the Windows Registry using attributes.
/// </summary>
[Generator]

Check warning on line 9 in src/Files.Core.SourceGenerator/Generators/RegistrySerializationGenerator.cs

GitHub Actions / build (Debug, x64)

This compiler extension should not be implemented in an assembly containing a reference to Microsoft.CodeAnalysis.Workspaces. The Microsoft.CodeAnalysis.Workspaces assembly is not provided during command line compilation scenarios, so references to it could cause the compiler extension to behave unpredictably. (https://github.com/dotnet/roslyn-analyzers/blob/main/docs/rules/RS1038.md)

Check warning on line 9 in src/Files.Core.SourceGenerator/Generators/RegistrySerializationGenerator.cs

GitHub Actions / build (Debug, arm64)

This compiler extension should not be implemented in an assembly containing a reference to Microsoft.CodeAnalysis.Workspaces. The Microsoft.CodeAnalysis.Workspaces assembly is not provided during command line compilation scenarios, so references to it could cause the compiler extension to behave unpredictably. (https://github.com/dotnet/roslyn-analyzers/blob/main/docs/rules/RS1038.md)

Check warning on line 9 in src/Files.Core.SourceGenerator/Generators/RegistrySerializationGenerator.cs

GitHub Actions / build (Release, arm64)

This compiler extension should not be implemented in an assembly containing a reference to Microsoft.CodeAnalysis.Workspaces. The Microsoft.CodeAnalysis.Workspaces assembly is not provided during command line compilation scenarios, so references to it could cause the compiler extension to behave unpredictably. (https://github.com/dotnet/roslyn-analyzers/blob/main/docs/rules/RS1038.md)

Check warning on line 9 in src/Files.Core.SourceGenerator/Generators/RegistrySerializationGenerator.cs

GitHub Actions / build (Release, x64)

This compiler extension should not be implemented in an assembly containing a reference to Microsoft.CodeAnalysis.Workspaces. The Microsoft.CodeAnalysis.Workspaces assembly is not provided during command line compilation scenarios, so references to it could cause the compiler extension to behave unpredictably. (https://github.com/dotnet/roslyn-analyzers/blob/main/docs/rules/RS1038.md)
internal sealed class RegistrySerializationGenerator : IIncrementalGenerator
{
/// <summary>
private static void OnOneRowModeEnabledChanged(DependencyObject d, object newValue)
{
var self = d as AdaptiveGridView;
self.DetermineOneRowMode();

Check warning on line 65 in src/Files.App.Controls/AdaptiveGridView/AdaptiveGridView.Properties.cs

GitHub Actions / build (Debug, arm64)

Dereference of a possibly null reference.
}
private static void DesiredWidthChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
_ = PInvoke.RoInitialize(RO_INIT_TYPE.RO_INIT_MULTITHREADED);
var classIds = typeof(Program).Assembly.GetTypes()

Check warning on line 28 in src/Files.App.Server/Program.cs

GitHub Actions / build (Release, arm64)

Using member 'System.Reflection.Assembly.GetTypes()' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Types might be removed.

Check warning on line 28 in src/Files.App.Server/Program.cs

GitHub Actions / build (Release, x64)

Using member 'System.Reflection.Assembly.GetTypes()' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Types might be removed.
.Where(t => t.IsSealed && t.IsPublic && t.IsClass)
.Select(t => t.FullName!)
.Where(name => name.StartsWith("Files.App.Server.", StringComparison.Ordinal))