From 9ba0066f40ee04dcbbfb8b77cea371321066fc1c Mon Sep 17 00:00:00 2001 From: qhy040404 Date: Sat, 20 Apr 2024 15:16:22 +0800 Subject: [PATCH] allow to open elevated app from taskbar --- .../Snap.Hutao/Core/Shell/ShellLinkInterop.cs | 30 +++++-------------- src/Snap.Hutao/Snap.Hutao/Snap.Hutao.csproj | 4 +++ .../Snap.Hutao/Win32/UI/Shell/IShellLinkW.cs | 8 +++++ 3 files changed, 19 insertions(+), 23 deletions(-) diff --git a/src/Snap.Hutao/Snap.Hutao/Core/Shell/ShellLinkInterop.cs b/src/Snap.Hutao/Snap.Hutao/Core/Shell/ShellLinkInterop.cs index 47ce89e9..7b3e7690 100644 --- a/src/Snap.Hutao/Snap.Hutao/Core/Shell/ShellLinkInterop.cs +++ b/src/Snap.Hutao/Snap.Hutao/Core/Shell/ShellLinkInterop.cs @@ -20,23 +20,14 @@ internal sealed partial class ShellLinkInterop : IShellLinkInterop public async ValueTask TryCreateDesktopShoutcutForElevatedLaunchAsync() { - string targetLogoPath = Path.Combine(runtimeOptions.DataFolder, "ShellLinkLogo.ico"); + Uri elevatedLauncherUri = "ms-appx:///Snap.Hutao.Elevated.Launcher.exe".ToUri(); + StorageFile launcherFile = await StorageFile.GetFileFromApplicationUriAsync(elevatedLauncherUri); + string elevatedLauncherPath = launcherFile.Path; - try - { - Uri sourceLogoUri = "ms-appx:///Assets/Logo.ico".ToUri(); - StorageFile iconFile = await StorageFile.GetFileFromApplicationUriAsync(sourceLogoUri); - await iconFile.OverwriteCopyAsync(targetLogoPath).ConfigureAwait(false); - } - catch - { - return false; - } - - return UnsafeTryCreateDesktopShoutcutForElevatedLaunch(targetLogoPath); + return UnsafeTryCreateDesktopShoutcutForElevatedLaunch(elevatedLauncherPath); } - private unsafe bool UnsafeTryCreateDesktopShoutcutForElevatedLaunch(string targetLogoPath) + private unsafe bool UnsafeTryCreateDesktopShoutcutForElevatedLaunch(string elevatedLauncherPath) { bool result = false; @@ -44,16 +35,9 @@ internal sealed partial class ShellLinkInterop : IShellLinkInterop HRESULT hr = CoCreateInstance(in ShellLink.CLSID, default, CLSCTX.CLSCTX_INPROC_SERVER, in IShellLinkW.IID, out IShellLinkW* pShellLink); if (SUCCEEDED(hr)) { - pShellLink->SetPath($"shell:AppsFolder\\{runtimeOptions.FamilyName}!App"); + pShellLink->SetPath(elevatedLauncherPath); + pShellLink->SetArguments(runtimeOptions.FamilyName); pShellLink->SetShowCmd(SHOW_WINDOW_CMD.SW_NORMAL); - pShellLink->SetIconLocation(targetLogoPath, 0); - - if (SUCCEEDED(pShellLink->QueryInterface(in IShellLinkDataList.IID, out IShellLinkDataList* pShellLinkDataList))) - { - pShellLinkDataList->GetFlags(out uint flags); - pShellLinkDataList->SetFlags(flags | (uint)SHELL_LINK_DATA_FLAGS.SLDF_RUNAS_USER); - pShellLinkDataList->Release(); - } if (SUCCEEDED(pShellLink->QueryInterface(in IPersistFile.IID, out IPersistFile* pPersistFile))) { diff --git a/src/Snap.Hutao/Snap.Hutao/Snap.Hutao.csproj b/src/Snap.Hutao/Snap.Hutao/Snap.Hutao.csproj index 32071b4a..fb0d09c7 100644 --- a/src/Snap.Hutao/Snap.Hutao/Snap.Hutao.csproj +++ b/src/Snap.Hutao/Snap.Hutao/Snap.Hutao.csproj @@ -327,6 +327,10 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive + + + all + runtime; build; native; contentfiles; analyzers all diff --git a/src/Snap.Hutao/Snap.Hutao/Win32/UI/Shell/IShellLinkW.cs b/src/Snap.Hutao/Snap.Hutao/Win32/UI/Shell/IShellLinkW.cs index 2ce4447b..d4104b58 100644 --- a/src/Snap.Hutao/Snap.Hutao/Win32/UI/Shell/IShellLinkW.cs +++ b/src/Snap.Hutao/Snap.Hutao/Win32/UI/Shell/IShellLinkW.cs @@ -70,6 +70,14 @@ internal unsafe struct IShellLinkW } } + public HRESULT SetArguments(string szArgs) + { + fixed (char* pszArgs = szArgs) + { + return ThisPtr->SetArguments((IShellLinkW*)Unsafe.AsPointer(ref this), pszArgs); + } + } + internal unsafe readonly struct Vftbl { internal readonly IUnknown.Vftbl IUnknownVftbl;