Skip to content

armgit / Filee

Public Imported from GitHub
0

Free, open-source file converter. Convert 263 formats offline by drag & drop — images, PDF, Word, Excel, PowerPoint, HWP/HWPX, video, audio, e-books and archives. No upload, no Office needed.

Fix the crash after adding the Windows 11 menu entry

KnifeLemon5ac47eafb2b5
diff --git a/src/Filee.App/ViewModels/Pages/GeneralPageViewModel.cs b/src/Filee.App/ViewModels/Pages/GeneralPageViewModel.cs
index d5ee544e..445e6115 100644
--- a/src/Filee.App/ViewModels/Pages/GeneralPageViewModel.cs
+++ b/src/Filee.App/ViewModels/Pages/GeneralPageViewModel.cs
@@ -85,7 +85,16 @@ public sealed partial class GeneralPageViewModel : ObservableObject
return;
ModernMenuBusy = true;
ModernMenuStatus = _loc["general.modern_menu_busy"];
- var result = await _platform.SetModernContextMenuAsync(enabled, exe);
+ ModernContextMenuResult result;
+ try
+ {
+ result = await _platform.SetModernContextMenuAsync(enabled, exe);
+ }
+ catch (Exception ex) when (ex is not OutOfMemoryException)
+ {
+ // A command that throws would end the app (async void path): report it on the page instead.
+ result = new ModernContextMenuResult(false, Error: ex.Message);
+ }
ModernMenuBusy = false;
RefreshModernMenu();
if (result.Cancelled)
diff --git a/src/Filee.Platform.Windows/ExplorerMenuRegistration.cs b/src/Filee.Platform.Windows/ExplorerMenuRegistration.cs
index e3f81368..b11060ef 100644
--- a/src/Filee.Platform.Windows/ExplorerMenuRegistration.cs
+++ b/src/Filee.Platform.Windows/ExplorerMenuRegistration.cs
@@ -282,7 +282,20 @@ public static class ExplorerMenuRegistration
return string.Join(' ', message.Split(['\r', '\n'], StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries));
}
- private static unsafe string? ExternalLocationOf(string fullName)
+ /// <summary>The install folder the package points to, or null when Windows can't tell.</summary>
+ public static unsafe string? ExternalLocationOf(string fullName)
+ {
+ try
+ {
+ return ExternalLocationOfCore(fullName);
+ }
+ catch (Exception ex) when (ex is EntryPointNotFoundException or DllNotFoundException)
+ {
+ return null; // older Windows without the API: the folder is then not compared
+ }
+ }
+
+ private static unsafe string? ExternalLocationOfCore(string fullName)
{
uint length = 0;
if (NativeMethods.GetPackagePathByFullName2(fullName, NativeMethods.PackagePathType_EffectiveExternal, ref length, null)
diff --git a/src/Filee.Platform.Windows/NativeMethods.cs b/src/Filee.Platform.Windows/NativeMethods.cs
index 592de999..1e360bb6 100644
--- a/src/Filee.Platform.Windows/NativeMethods.cs
+++ b/src/Filee.Platform.Windows/NativeMethods.cs
@@ -58,7 +58,8 @@ internal static partial class NativeMethods
[LibraryImport("kernel32.dll", StringMarshalling = StringMarshalling.Utf16)]
public static unsafe partial int GetPackagesByPackageFamily(string packageFamilyName, ref uint count, char** packageFullNames, ref uint bufferLength, char* buffer);
- [LibraryImport("kernel32.dll", StringMarshalling = StringMarshalling.Utf16)]
+ // Not exported by kernel32.dll (only by kernelbase.dll): import it from its API set, Windows 10 1903 and later.
+ [LibraryImport("api-ms-win-appmodel-runtime-l1-1-3.dll", StringMarshalling = StringMarshalling.Utf16)]
public static unsafe partial int GetPackagePathByFullName2(string packageFullName, int packagePathType, ref uint pathLength, char* path);
[LibraryImport("kernel32.dll")]
diff --git a/src/Filee.Platform.Windows/WindowsPlatformServices.cs b/src/Filee.Platform.Windows/WindowsPlatformServices.cs
index ce4a28be..5efb4239 100644
--- a/src/Filee.Platform.Windows/WindowsPlatformServices.cs
+++ b/src/Filee.Platform.Windows/WindowsPlatformServices.cs
@@ -139,10 +139,21 @@ public sealed class WindowsPlatformServices : IPlatformServices
CreateShortcut(SendToShortcutPath, executablePath, "--convert", label);
}
- public ModernContextMenuState GetModernContextMenuState(string executablePath) =>
- Path.GetDirectoryName(executablePath) is { Length: > 0 } folder
- ? ExplorerMenuRegistration.GetState(folder)
- : ModernContextMenuState.Unsupported;
+ public ModernContextMenuState GetModernContextMenuState(string executablePath)
+ {
+ try
+ {
+ return Path.GetDirectoryName(executablePath) is { Length: > 0 } folder
+ ? ExplorerMenuRegistration.GetState(folder)
+ : ModernContextMenuState.Unsupported;
+ }
+ catch (Exception ex) when (ex is not OutOfMemoryException)
+ {
+ // Asking Windows about packages must never take the app or the settings page down.
+ Trace.TraceWarning($"Reading the Explorer menu state failed: {ex}");
+ return ModernContextMenuState.Unsupported;
+ }
+ }
public Task<ModernContextMenuResult> SetModernContextMenuAsync(bool enabled, string executablePath) => Task.Run(async () =>
{
@@ -150,9 +161,17 @@ public sealed class WindowsPlatformServices : IPlatformServices
if (string.IsNullOrEmpty(folder))
return new ModernContextMenuResult(false, Error: "Unknown install folder.");
- var result = enabled
- ? await ExplorerMenuRegistration.AddAsync(folder)
- : await ExplorerMenuRegistration.RemoveAsync(allowElevation: true);
+ ModernContextMenuResult result;
+ try
+ {
+ result = enabled
+ ? await ExplorerMenuRegistration.AddAsync(folder)
+ : await ExplorerMenuRegistration.RemoveAsync(allowElevation: true);
+ }
+ catch (Exception ex) when (ex is not OutOfMemoryException)
+ {
+ result = new ModernContextMenuResult(false, Error: ex.Message);
+ }
// Keep exactly one entry: the title file exists while the Explorer menu setting is on.
if (ExplorerMenuRegistration.ReadTitle() is { Length: > 0 } title)
diff --git a/tests/Filee.App.Tests/ExplorerMenuTests.cs b/tests/Filee.App.Tests/ExplorerMenuTests.cs
index 8daf4eb7..c6ed47d6 100644
--- a/tests/Filee.App.Tests/ExplorerMenuTests.cs
+++ b/tests/Filee.App.Tests/ExplorerMenuTests.cs
@@ -90,6 +90,35 @@ public class ExplorerMenuTests
Assert.Equal(ExplorerMenuPackage.FamilyName, ExplorerMenuRegistration.ComputeFamilyName());
}
+ [Fact]
+ public void Every_native_import_exists_in_the_dll_it_names()
+ {
+ // GetPackagePathByFullName2 was imported from kernel32.dll, which doesn't export it: the call only failed
+ // once a package was registered and crashed the app right after "Add to the main menu" (1.1.0).
+ var nativeMethods = typeof(ExplorerMenuRegistration).Assembly.GetType("Filee.Platform.Windows.NativeMethods", throwOnError: true)!;
+ var imports = nativeMethods.GetMethods(System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic)
+ .Select(m => (Method: m, Import: m.GetCustomAttributes(typeof(LibraryImportAttribute), false).OfType<LibraryImportAttribute>().FirstOrDefault()))
+ .Where(m => m.Import is not null)
+ .ToList();
+ Assert.NotEmpty(imports);
+ Assert.All(imports, m =>
+ {
+ var entryPoint = m.Import!.EntryPoint ?? m.Method.Name;
+ Assert.True(NativeLibrary.TryLoad(m.Import.LibraryName, out var library), $"{m.Import.LibraryName} can't be loaded");
+ Assert.True(NativeLibrary.TryGetExport(library, entryPoint, out _), $"{m.Import.LibraryName} has no {entryPoint}");
+ });
+ }
+
+ [Fact]
+ public void Reading_the_registered_package_never_throws()
+ {
+ Assert.Null(ExplorerMenuRegistration.ExternalLocationOf("Filee.NotInstalled_1.0.0.0_x64__0000000000000"));
+ // Whatever is registered on this machine (nothing on CI): a registered sparse package has an external location.
+ if (ExplorerMenuRegistration.Find() is { } package)
+ Assert.False(string.IsNullOrEmpty(package.ExternalLocation), package.FullName);
+ _ = ExplorerMenuRegistration.GetState(BuiltFolder);
+ }
+
[Fact]
public void PowerShell_literals_escape_straight_and_typographic_single_quotes()
{