diff --git a/src/Files.App.CsWin32/NativeMethods.txt b/src/Files.App.CsWin32/NativeMethods.txt index 598c9b689ca7..f3aee42e4e97 100644 --- a/src/Files.App.CsWin32/NativeMethods.txt +++ b/src/Files.App.CsWin32/NativeMethods.txt @@ -222,3 +222,6 @@ SetCurrentProcessExplicitAppUserModelID GdipCreateBitmapFromScan0 BITMAP GetObject +CreateEvent +SetEvent +ResetEvent diff --git a/src/Files.App/App.xaml.cs b/src/Files.App/App.xaml.cs index a7b67a98829f..745cc517dd63 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* fileDialog = "FILEDIALOG") + { + HANDLE eventHandle = PInvoke.CreateEvent(bManualReset: false, bInitialState: false, lpName: fileDialog); + PInvoke.SetEvent(eventHandle); + PInvoke.CloseHandle(eventHandle); + } + } } // 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 9af7345111ec..2ea8b596776a 100644 --- a/src/Files.App/Helpers/Win32/Win32PInvoke.Methods.cs +++ b/src/Files.App/Helpers/Win32/Win32PInvoke.Methods.cs @@ -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 @@ -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, diff --git a/src/Files.App/Program.cs b/src/Files.App/Program.cs index 6d7807095797..28b3ca403e2c 100644 --- a/src/Files.App/Program.cs +++ b/src/Files.App/Program.cs @@ -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; @@ -248,17 +250,17 @@ 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 eventHandle = PInvoke.CreateEvent(bManualReset: true, bInitialState: false, lpName: null); Task.Run(() => { keyInstance.RedirectActivationToAsync(args).AsTask().Wait(); - SetEvent(eventHandle); + PInvoke.SetEvent(eventHandle); }); - _ = CoWaitForMultipleObjects( + _ = Win32PInvoke.CoWaitForMultipleObjects( CWMO_DEFAULT, INFINITE, 1, @@ -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); Task.Run(() => { LaunchHelper.LaunchAppAsync(filePath, null, null).Wait(); - SetEvent(eventHandle); + PInvoke.SetEvent(eventHandle); }); _ = CoWaitForMultipleObjects( diff --git a/src/Files.App/ViewModels/ShellViewModel.cs b/src/Files.App/ViewModels/ShellViewModel.cs index 68472561dc36..e34f5c0a336b 100644 --- a/src/Files.App/ViewModels/ShellViewModel.cs +++ b/src/Files.App/ViewModels/ShellViewModel.cs @@ -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; @@ -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(); const uint INFINITE = 0xFFFFFFFF; while (x.Status != AsyncStatus.Canceled) @@ -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(); const uint INFINITE = 0xFFFFFFFF; while (x.Status != AsyncStatus.Canceled)