Skip to content

Code Quality: Replaced DllImport calls with CsWin32 generations for event object operations #17086

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 3 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 (Release, 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 (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 (Debug, arm64)

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 (Release, 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 (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 (Debug, arm64)

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 (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

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 (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
CreateFile
GetVolumeInformation
COMPRESSION_FORMAT
Expand Down Expand Up @@ -222,3 +222,6 @@
GdipCreateBitmapFromScan0
BITMAP
GetObject
CreateEvent
SetEvent
ResetEvent
14 changes: 11 additions & 3 deletions src/Files.App/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using Microsoft.Extensions.Logging;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Windows.Win32.Foundation;
using Windows.Win32;
using Microsoft.Windows.AppLifecycle;
using Windows.ApplicationModel;
using Windows.ApplicationModel.DataTransfer;
Expand Down Expand Up @@ -223,9 +225,15 @@ private async void Window_Closed(object sender, WindowEventArgs args)
var results = items.Select(x => x.ItemPath).ToList();
System.IO.File.WriteAllLines(OutputPath, results);

IntPtr eventHandle = Win32PInvoke.CreateEvent(IntPtr.Zero, false, false, "FILEDIALOG");
Win32PInvoke.SetEvent(eventHandle);
Win32PInvoke.CloseHandle(eventHandle);
unsafe
{
fixed (char* fileDialog = "FILEDIALOG")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pszEventObjectName please.

{
HANDLE eventHandle = PInvoke.CreateEvent(bManualReset: false, bInitialState: false, lpName: fileDialog);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't you just:

Suggested change
HANDLE eventHandle = PInvoke.CreateEvent(bManualReset: false, bInitialState: false, lpName: fileDialog);
HANDLE eventHandle = PInvoke.CreateEvent((SECURITY_ATTRIBUTES*)null, false, false, fileDialog);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also hEventHandle please.

PInvoke.SetEvent(eventHandle);
PInvoke.CloseHandle(eventHandle);
}
}
}

// Continue running the app on the background
Expand Down
18 changes: 0 additions & 18 deletions src/Files.App/Helpers/Win32/Win32PInvoke.Methods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,6 @@ public static extern bool SetPropW(
IntPtr hData
);

[DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
public static extern IntPtr CreateEvent(
IntPtr lpEventAttributes,
bool bManualReset,
bool bInitialState,
string lpName
);

[DllImport("kernel32.dll")]
public static extern bool SetEvent(
IntPtr hEvent
);

[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern int GetDpiForWindow(
IntPtr hwnd
Expand Down Expand Up @@ -135,11 +122,6 @@ public static extern uint WaitForMultipleObjectsEx(
bool bAlertable
);

[DllImport("api-ms-win-core-synch-l1-2-0.dll", SetLastError = true)]
public static extern bool ResetEvent(
IntPtr hEvent
);

[DllImport("api-ms-win-core-synch-l1-2-0.dll", SetLastError = true)]
public static extern uint WaitForSingleObjectEx(
IntPtr hHandle,
Expand Down
16 changes: 9 additions & 7 deletions src/Files.App/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using Microsoft.UI.Dispatching;
using Microsoft.UI.Xaml;
using Microsoft.Windows.AppLifecycle;
using Windows.Win32;
using Windows.Win32.Foundation;
using System.IO;
using System.Text;
using Windows.ApplicationModel.Activation;
Expand Down Expand Up @@ -248,17 +250,17 @@ private static async void OnActivated(object? sender, AppActivationArguments arg
/// <remarks>
/// Redirects on another thread and uses a non-blocking wait method to wait for the redirection to complete.
/// </remarks>
public static void RedirectActivationTo(AppInstance keyInstance, AppActivationArguments args)
public static unsafe void RedirectActivationTo(AppInstance keyInstance, AppActivationArguments args)
{
IntPtr eventHandle = CreateEvent(IntPtr.Zero, true, false, null);
HANDLE eventHandle = PInvoke.CreateEvent(bManualReset: true, bInitialState: false, lpName: null);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above.


Task.Run(() =>
{
keyInstance.RedirectActivationToAsync(args).AsTask().Wait();
SetEvent(eventHandle);
PInvoke.SetEvent(eventHandle);
});

_ = CoWaitForMultipleObjects(
_ = Win32PInvoke.CoWaitForMultipleObjects(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pretty sure you should pin the array.

CWMO_DEFAULT,
INFINITE,
1,
Expand All @@ -271,14 +273,14 @@ public static void OpenShellCommandInExplorer(string shellCommand, int pid)
Win32Helper.OpenFolderInExistingShellWindow(shellCommand);
}

public static void OpenFileFromTile(string filePath)
public static unsafe void OpenFileFromTile(string filePath)
{
IntPtr eventHandle = CreateEvent(IntPtr.Zero, true, false, null);
HANDLE eventHandle = PInvoke.CreateEvent(bManualReset: true, bInitialState: false, lpName: null);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above.


Task.Run(() =>
{
LaunchHelper.LaunchAppAsync(filePath, null, null).Wait();
SetEvent(eventHandle);
PInvoke.SetEvent(eventHandle);
});

_ = CoWaitForMultipleObjects(
Expand Down
5 changes: 3 additions & 2 deletions src/Files.App/ViewModels/ShellViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Vanara.Windows.Shell;
using Windows.Win32;
using Windows.Foundation;
using Windows.Storage;
using Windows.Storage.FileProperties;
Expand Down Expand Up @@ -2124,7 +2125,7 @@ private void WatchForDirectoryChanges(string path, CloudDriveSyncStatus syncStat
notifyFilters |= FILE_NOTIFY_CHANGE_ATTRIBUTES;

var overlapped = new OVERLAPPED();
overlapped.hEvent = CreateEvent(IntPtr.Zero, false, false, null);
overlapped.hEvent = PInvoke.CreateEvent(null, false, false, null).DangerousGetHandle();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above.

const uint INFINITE = 0xFFFFFFFF;

while (x.Status != AsyncStatus.Canceled)
Expand Down Expand Up @@ -2235,7 +2236,7 @@ private void WatchForGitChanges()
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);
overlapped.hEvent = PInvoke.CreateEvent(null, false, false, null).DangerousGetHandle();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Asa above.

const uint INFINITE = 0xFFFFFFFF;

while (x.Status != AsyncStatus.Canceled)
Expand Down
Loading