From 822b22df47b8e3877c7fda6ba1887152ff256496 Mon Sep 17 00:00:00 2001 From: ema Date: Mon, 16 Dec 2024 06:28:06 +0800 Subject: [PATCH] Move ShlwApi method --- .../NativeMethods/ShlwApi.cs | 92 +++++++++++++++++++ .../WebpagePanel.cs | 73 +-------------- 2 files changed, 94 insertions(+), 71 deletions(-) create mode 100644 QuickLook.Plugin/QuickLook.Plugin.HtmlViewer/NativeMethods/ShlwApi.cs diff --git a/QuickLook.Plugin/QuickLook.Plugin.HtmlViewer/NativeMethods/ShlwApi.cs b/QuickLook.Plugin/QuickLook.Plugin.HtmlViewer/NativeMethods/ShlwApi.cs new file mode 100644 index 000000000..592ad7a24 --- /dev/null +++ b/QuickLook.Plugin/QuickLook.Plugin.HtmlViewer/NativeMethods/ShlwApi.cs @@ -0,0 +1,92 @@ +// Copyright © 2024 Frank Becker +// +// This file is part of QuickLook program. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +using System; +using System.Diagnostics; +using System.IO; +using System.Runtime.InteropServices; +using System.Text; + +namespace QuickLook.Plugin.HtmlViewer.NativeMethods; + +internal static class ShlwApi +{ + public static string GetAssociatedAppForScheme(string scheme) + { + try + { + // Try to get friendly app name first + uint pcchOut = 0; + AssocQueryString(AssocF.None, AssocStr.FriendlyAppName, scheme, null, null, ref pcchOut); + + if (pcchOut > 0) + { + var pszOut = new StringBuilder((int)pcchOut); + AssocQueryString(AssocF.None, AssocStr.FriendlyAppName, scheme, null, pszOut, ref pcchOut); + + var appName = pszOut.ToString().Trim(); + if (!string.IsNullOrEmpty(appName)) + return appName; + } + + // Fall back to executable name if friendly name is not available + pcchOut = 0; + AssocQueryString(AssocF.None, AssocStr.Executable, scheme, null, null, ref pcchOut); + + if (pcchOut > 0) + { + var pszOut = new StringBuilder((int)pcchOut); + AssocQueryString(AssocF.None, AssocStr.Executable, scheme, null, pszOut, ref pcchOut); + + var exeName = pszOut.ToString().Trim(); + if (!string.IsNullOrEmpty(exeName)) + return Path.GetFileName(exeName); + } + + return null; + } + catch (Exception ex) + { + Debug.WriteLine($"Failed to get associated app: {ex.Message}"); + return null; + } + } + + [DllImport("Shlwapi.dll", CharSet = CharSet.Unicode)] + public static extern uint AssocQueryString( + AssocF flags, + AssocStr str, + string pszAssoc, + string pszExtra, + [Out] StringBuilder pszOut, + ref uint pcchOut + ); + + [Flags] + public enum AssocF + { + None = 0, + VerifyExists = 0x1 + } + + public enum AssocStr + { + Command = 1, + Executable = 2, + FriendlyAppName = 4 + } +} diff --git a/QuickLook.Plugin/QuickLook.Plugin.HtmlViewer/WebpagePanel.cs b/QuickLook.Plugin/QuickLook.Plugin.HtmlViewer/WebpagePanel.cs index b04efbec0..63aca3e3e 100644 --- a/QuickLook.Plugin/QuickLook.Plugin.HtmlViewer/WebpagePanel.cs +++ b/QuickLook.Plugin/QuickLook.Plugin.HtmlViewer/WebpagePanel.cs @@ -18,13 +18,12 @@ using Microsoft.Web.WebView2.Core; using Microsoft.Web.WebView2.Wpf; using QuickLook.Common.Helpers; +using QuickLook.Plugin.HtmlViewer.NativeMethods; using System; using System.Diagnostics; using System.Drawing; using System.IO; using System.Reflection; -using System.Runtime.InteropServices; -using System.Text; using System.Windows; using System.Windows.Controls; @@ -128,7 +127,7 @@ private void NavigationStarting_CancelNavigation(object sender, CoreWebView2Navi } // Ask user for unsafe schemes. Use dispatcher to avoid blocking thread. - string associatedApp = GetAssociatedAppForScheme(uri.Scheme); + string associatedApp = ShlwApi.GetAssociatedAppForScheme(uri.Scheme); _ = Application.Current.Dispatcher.BeginInvoke(new Action(() => { // TODO: translation @@ -159,74 +158,6 @@ private void NavigationStarting_CancelNavigation(object sender, CoreWebView2Navi } } - #region Get Associated App For Scheme - - [DllImport("Shlwapi.dll", CharSet = CharSet.Unicode)] - private static extern uint AssocQueryString( - AssocF flags, - AssocStr str, - string pszAssoc, - string pszExtra, - [Out] StringBuilder pszOut, - ref uint pcchOut); - - [Flags] - private enum AssocF - { - None = 0, - VerifyExists = 0x1 - } - - private enum AssocStr - { - Command = 1, - Executable = 2, - FriendlyAppName = 4 - } - - private string GetAssociatedAppForScheme(string scheme) - { - try - { - // Try to get friendly app name first - uint pcchOut = 0; - AssocQueryString(AssocF.None, AssocStr.FriendlyAppName, scheme, null, null, ref pcchOut); - - if (pcchOut > 0) - { - var pszOut = new StringBuilder((int)pcchOut); - AssocQueryString(AssocF.None, AssocStr.FriendlyAppName, scheme, null, pszOut, ref pcchOut); - - var appName = pszOut.ToString().Trim(); - if (!string.IsNullOrEmpty(appName)) - return appName; - } - - // Fall back to executable name if friendly name is not available - pcchOut = 0; - AssocQueryString(AssocF.None, AssocStr.Executable, scheme, null, null, ref pcchOut); - - if (pcchOut > 0) - { - var pszOut = new StringBuilder((int)pcchOut); - AssocQueryString(AssocF.None, AssocStr.Executable, scheme, null, pszOut, ref pcchOut); - - var exeName = pszOut.ToString().Trim(); - if (!string.IsNullOrEmpty(exeName)) - return Path.GetFileName(exeName); - } - - return null; - } - catch (Exception ex) - { - Debug.WriteLine($"Failed to get associated app: {ex.Message}"); - return null; - } - } - - #endregion Get Associated App For Scheme - private void WebView_NavigationCompleted(object sender, CoreWebView2NavigationCompletedEventArgs e) { _webView.DefaultBackgroundColor = Color.White; // Reset to white after page load to match expected default behavior