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 8 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
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 (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, 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 (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 (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, 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 (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 (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, 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 (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 @@ -226,3 +226,7 @@
RoGetAgileReference
IQueryInfo
QITIPF_FLAGS
CreateEvent
SetEvent
ResetEvent
CoWaitForMultipleObjects
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* pszEventObjectName = "FILEDIALOG")
{
HANDLE hEventHandle = PInvoke.CreateEvent((Windows.Win32.Security.SECURITY_ATTRIBUTES*)null, false, false, pszEventObjectName);
PInvoke.SetEvent(hEventHandle);
PInvoke.CloseHandle(hEventHandle);
}
}
}

// Continue running the app on the background
Expand Down
10 changes: 0 additions & 10 deletions src/Files.App/Helpers/Win32/Win32PInvoke.Methods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,6 @@ public static extern IntPtr CreateEvent(
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 +130,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
29 changes: 18 additions & 11 deletions src/Files.App/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
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;
using Windows.Storage;
using static Files.App.Helpers.Win32PInvoke;
using Windows.Win32.Security;

namespace Files.App
{
Expand Down Expand Up @@ -248,44 +251,48 @@ 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 hEventHandle = PInvoke.CreateEvent((SECURITY_ATTRIBUTES*)null, true, false, null);

HANDLE* pHandles = stackalloc HANDLE[1];
pHandles[0] = hEventHandle;

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

_ = CoWaitForMultipleObjects(
uint dwIndex = 0u;
PInvoke.CoWaitForMultipleObjects(
CWMO_DEFAULT,
INFINITE,
1,
[eventHandle],
out uint handleIndex);
1u,
pHandles,
&dwIndex);
}

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 hEventHandle = PInvoke.CreateEvent((SECURITY_ATTRIBUTES*)null, true, false, null);

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

_ = CoWaitForMultipleObjects(
CWMO_DEFAULT,
INFINITE,
1,
[eventHandle],
[hEventHandle],
out uint handleIndex);
}
}
Expand Down
Loading