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

Draft
wants to merge 8 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 @@ -225,3 +225,7 @@ QITIPF_FLAGS
GetKeyboardState
MapVirtualKey
GetKeyboardLayout
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
14 changes: 0 additions & 14 deletions src/Files.App/Helpers/Win32/Win32PInvoke.Methods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,6 @@ public static extern int RmGetList(
[In, Out] RM_PROCESS_INFO[] rgAffectedApps,
ref uint lpdwRebootReasons
);

[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("ole32.dll")]
public static extern uint CoWaitForMultipleObjects(
uint dwFlags,
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