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
Show file tree
Hide file tree
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
Expand Up @@ -45,7 +45,7 @@
GetKeyState
CreateDirectoryFromApp
WNetCancelConnection2
NET_USE_CONNECT_FLAGS

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

View workflow job for this annotation

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

View workflow job for this annotation

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

View workflow job for this annotation

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

View workflow job for this annotation

GitHub Actions / build (Release, x64)

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

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

View workflow job for this annotation

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

View workflow job for this annotation

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

View workflow job for this annotation

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

View workflow job for this annotation

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
Expand Down Expand Up @@ -134,7 +134,7 @@
CoTaskMemFree
QueryDosDevice
DeviceIoControl
GetLastError

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

View workflow job for this annotation

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

View workflow job for this annotation

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

View workflow job for this annotation

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

View workflow job for this annotation

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
Expand Down Expand Up @@ -226,3 +226,7 @@
RoGetAgileReference
IQueryInfo
QITIPF_FLAGS
CancelIoEx
OVERLAPPED

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

View workflow job for this annotation

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

View workflow job for this annotation

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

View workflow job for this annotation

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

View workflow job for this annotation

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
Expand Up @@ -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,
Expand Down
54 changes: 32 additions & 22 deletions src/Files.App/ViewModels/ShellViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
{
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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

View workflow job for this annotation

GitHub Actions / build (Debug, x64)

) expected

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

View workflow job for this annotation

GitHub Actions / build (Debug, arm64)

) expected

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

View workflow job for this annotation

GitHub Actions / build (Release, arm64)

) expected

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

View workflow job for this annotation

GitHub Actions / build (Release, x64)

) expected
const uint INFINITE = 0xFFFFFFFF;

while (x.Status != AsyncStatus.Canceled)
Expand All @@ -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]);
Expand All @@ -2280,7 +2290,7 @@
}
}

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

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

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