Skip to content

Commit 092caa3

Browse files
committed
Init
1 parent 6bcec6d commit 092caa3

File tree

5 files changed

+22
-30
lines changed

5 files changed

+22
-30
lines changed

src/Files.App.CsWin32/NativeMethods.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,3 +222,7 @@ SetCurrentProcessExplicitAppUserModelID
222222
GdipCreateBitmapFromScan0
223223
BITMAP
224224
GetObject
225+
CreateEvent
226+
SetEvent
227+
ResetEvent
228+
CoWaitForMultipleObjects

src/Files.App/App.xaml.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
using Microsoft.Extensions.Logging;
66
using Microsoft.UI.Xaml;
77
using Microsoft.UI.Xaml.Controls;
8+
using Microsoft.Win32.SafeHandles;
9+
using Windows.Win32;
810
using Microsoft.Windows.AppLifecycle;
911
using Windows.ApplicationModel;
1012
using Windows.ApplicationModel.DataTransfer;
@@ -223,9 +225,9 @@ private async void Window_Closed(object sender, WindowEventArgs args)
223225
var results = items.Select(x => x.ItemPath).ToList();
224226
System.IO.File.WriteAllLines(OutputPath, results);
225227

226-
IntPtr eventHandle = Win32PInvoke.CreateEvent(IntPtr.Zero, false, false, "FILEDIALOG");
227-
Win32PInvoke.SetEvent(eventHandle);
228-
Win32PInvoke.CloseHandle(eventHandle);
228+
SafeFileHandle eventHandle = PInvoke.CreateEvent(null, false, false, "FILEDIALOG");
229+
PInvoke.SetEvent(eventHandle);
230+
eventHandle.Close();
229231
}
230232

231233
// Continue running the app on the background

src/Files.App/Helpers/Win32/Win32PInvoke.Methods.cs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -63,19 +63,6 @@ public static extern bool SetPropW(
6363
IntPtr hData
6464
);
6565

66-
[DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
67-
public static extern IntPtr CreateEvent(
68-
IntPtr lpEventAttributes,
69-
bool bManualReset,
70-
bool bInitialState,
71-
string lpName
72-
);
73-
74-
[DllImport("kernel32.dll")]
75-
public static extern bool SetEvent(
76-
IntPtr hEvent
77-
);
78-
7966
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
8067
public static extern int GetDpiForWindow(
8168
IntPtr hwnd
@@ -135,11 +122,6 @@ public static extern uint WaitForMultipleObjectsEx(
135122
bool bAlertable
136123
);
137124

138-
[DllImport("api-ms-win-core-synch-l1-2-0.dll", SetLastError = true)]
139-
public static extern bool ResetEvent(
140-
IntPtr hEvent
141-
);
142-
143125
[DllImport("api-ms-win-core-synch-l1-2-0.dll", SetLastError = true)]
144126
public static extern uint WaitForSingleObjectEx(
145127
IntPtr hHandle,

src/Files.App/Program.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@
55
using Microsoft.UI.Dispatching;
66
using Microsoft.UI.Xaml;
77
using Microsoft.Windows.AppLifecycle;
8+
using Windows.Win32;
9+
using Windows.Win32.Foundation;
810
using System.IO;
911
using System.Text;
1012
using Windows.ApplicationModel.Activation;
1113
using Windows.Storage;
1214
using static Files.App.Helpers.Win32PInvoke;
15+
using Microsoft.Win32.SafeHandles;
1316

1417
namespace Files.App
1518
{
@@ -250,19 +253,19 @@ private static async void OnActivated(object? sender, AppActivationArguments arg
250253
/// </remarks>
251254
public static void RedirectActivationTo(AppInstance keyInstance, AppActivationArguments args)
252255
{
253-
IntPtr eventHandle = CreateEvent(IntPtr.Zero, true, false, null);
256+
SafeFileHandle eventHandle = PInvoke.CreateEvent(null, true, false, null);
254257

255258
Task.Run(() =>
256259
{
257260
keyInstance.RedirectActivationToAsync(args).AsTask().Wait();
258-
SetEvent(eventHandle);
261+
PInvoke.SetEvent(eventHandle);
259262
});
260263

261-
_ = CoWaitForMultipleObjects(
264+
_ = Win32PInvoke.CoWaitForMultipleObjects(
262265
CWMO_DEFAULT,
263266
INFINITE,
264267
1,
265-
[eventHandle],
268+
[eventHandle.DangerousGetHandle()],
266269
out uint handleIndex);
267270
}
268271

@@ -273,19 +276,19 @@ public static void OpenShellCommandInExplorer(string shellCommand, int pid)
273276

274277
public static void OpenFileFromTile(string filePath)
275278
{
276-
IntPtr eventHandle = CreateEvent(IntPtr.Zero, true, false, null);
279+
SafeFileHandle eventHandle = PInvoke.CreateEvent(null, true, false, null);
277280

278281
Task.Run(() =>
279282
{
280283
LaunchHelper.LaunchAppAsync(filePath, null, null).Wait();
281-
SetEvent(eventHandle);
284+
PInvoke.SetEvent(eventHandle);
282285
});
283286

284287
_ = CoWaitForMultipleObjects(
285288
CWMO_DEFAULT,
286289
INFINITE,
287290
1,
288-
[eventHandle],
291+
[eventHandle.DangerousGetHandle()],
289292
out uint handleIndex);
290293
}
291294
}

src/Files.App/ViewModels/ShellViewModel.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
using System.Runtime.CompilerServices;
1515
using System.Runtime.InteropServices;
1616
using Vanara.Windows.Shell;
17+
using Windows.Win32;
1718
using Windows.Foundation;
1819
using Windows.Storage;
1920
using Windows.Storage.FileProperties;
@@ -2124,7 +2125,7 @@ private void WatchForDirectoryChanges(string path, CloudDriveSyncStatus syncStat
21242125
notifyFilters |= FILE_NOTIFY_CHANGE_ATTRIBUTES;
21252126

21262127
var overlapped = new OVERLAPPED();
2127-
overlapped.hEvent = CreateEvent(IntPtr.Zero, false, false, null);
2128+
overlapped.hEvent = PInvoke.CreateEvent(null, false, false, null).DangerousGetHandle();
21282129
const uint INFINITE = 0xFFFFFFFF;
21292130

21302131
while (x.Status != AsyncStatus.Canceled)
@@ -2235,7 +2236,7 @@ private void WatchForGitChanges()
22352236
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;
22362237

22372238
var overlapped = new OVERLAPPED();
2238-
overlapped.hEvent = CreateEvent(IntPtr.Zero, false, false, null);
2239+
overlapped.hEvent = PInvoke.CreateEvent(null, false, false, null).DangerousGetHandle();
22392240
const uint INFINITE = 0xFFFFFFFF;
22402241

22412242
while (x.Status != AsyncStatus.Canceled)

0 commit comments

Comments
 (0)