diff --git a/src/Files.App.CsWin32/NativeMethods.txt b/src/Files.App.CsWin32/NativeMethods.txt index ce5524f6885e..dbc4961a4565 100644 --- a/src/Files.App.CsWin32/NativeMethods.txt +++ b/src/Files.App.CsWin32/NativeMethods.txt @@ -225,3 +225,7 @@ QITIPF_FLAGS GetKeyboardState MapVirtualKey GetKeyboardLayout +CreateEvent +SetEvent +ResetEvent +CoWaitForMultipleObjects diff --git a/src/Files.App/App.xaml.cs b/src/Files.App/App.xaml.cs index a7b67a98829f..be0556db4752 100644 --- a/src/Files.App/App.xaml.cs +++ b/src/Files.App/App.xaml.cs @@ -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; @@ -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 diff --git a/src/Files.App/Helpers/Win32/Win32PInvoke.Methods.cs b/src/Files.App/Helpers/Win32/Win32PInvoke.Methods.cs index 5d700736a2fa..f7dc7d44cf97 100644 --- a/src/Files.App/Helpers/Win32/Win32PInvoke.Methods.cs +++ b/src/Files.App/Helpers/Win32/Win32PInvoke.Methods.cs @@ -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, diff --git a/src/Files.App/Program.cs b/src/Files.App/Program.cs index 6d7807095797..b054fca0ce53 100644 --- a/src/Files.App/Program.cs +++ b/src/Files.App/Program.cs @@ -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 { @@ -248,22 +251,26 @@ private static async void OnActivated(object? sender, AppActivationArguments arg /// /// Redirects on another thread and uses a non-blocking wait method to wait for the redirection to complete. /// - 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) @@ -271,21 +278,21 @@ 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); } }