From 9ba0066f40ee04dcbbfb8b77cea371321066fc1c Mon Sep 17 00:00:00 2001 From: qhy040404 Date: Sat, 20 Apr 2024 15:16:22 +0800 Subject: [PATCH 1/4] 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; From 5ed5729c4e6726c271f1a6db5b6b7144770d9413 Mon Sep 17 00:00:00 2001 From: qhy040404 Date: Sat, 20 Apr 2024 15:28:07 +0800 Subject: [PATCH 2/4] extract launcher --- .../Snap.Hutao/Core/Shell/ShellLinkInterop.cs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/Snap.Hutao/Snap.Hutao/Core/Shell/ShellLinkInterop.cs b/src/Snap.Hutao/Snap.Hutao/Core/Shell/ShellLinkInterop.cs index 7b3e7690..eb0edf3a 100644 --- a/src/Snap.Hutao/Snap.Hutao/Core/Shell/ShellLinkInterop.cs +++ b/src/Snap.Hutao/Snap.Hutao/Core/Shell/ShellLinkInterop.cs @@ -20,9 +20,18 @@ internal sealed partial class ShellLinkInterop : IShellLinkInterop public async ValueTask TryCreateDesktopShoutcutForElevatedLaunchAsync() { - Uri elevatedLauncherUri = "ms-appx:///Snap.Hutao.Elevated.Launcher.exe".ToUri(); - StorageFile launcherFile = await StorageFile.GetFileFromApplicationUriAsync(elevatedLauncherUri); - string elevatedLauncherPath = launcherFile.Path; + string elevatedLauncherPath = Path.Combine(runtimeOptions.DataFolder, "Snap.Hutao.Elevated.Launcher.exe"); + + try + { + Uri elevatedLauncherUri = "ms-appx:///Snap.Hutao.Elevated.Launcher.exe".ToUri(); + StorageFile launcherFile = await StorageFile.GetFileFromApplicationUriAsync(elevatedLauncherUri); + await launcherFile.OverwriteCopyAsync(elevatedLauncherPath).ConfigureAwait(false); + } + catch + { + return false; + } return UnsafeTryCreateDesktopShoutcutForElevatedLaunch(elevatedLauncherPath); } From 6b7ffe9fe9552c8ea119ab2607a54036796a2018 Mon Sep 17 00:00:00 2001 From: qhy040404 Date: Sun, 21 Apr 2024 23:49:53 +0800 Subject: [PATCH 3/4] Update Snap.Hutao.csproj --- src/Snap.Hutao/Snap.Hutao/Snap.Hutao.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Snap.Hutao/Snap.Hutao/Snap.Hutao.csproj b/src/Snap.Hutao/Snap.Hutao/Snap.Hutao.csproj index fb0d09c7..227b8f5c 100644 --- a/src/Snap.Hutao/Snap.Hutao/Snap.Hutao.csproj +++ b/src/Snap.Hutao/Snap.Hutao/Snap.Hutao.csproj @@ -328,7 +328,7 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive - + all runtime; build; native; contentfiles; analyzers From 3fe726aa63fdf0bbfef675c38de78a49d07928c3 Mon Sep 17 00:00:00 2001 From: qhy040404 Date: Mon, 22 Apr 2024 11:36:40 +0800 Subject: [PATCH 4/4] extract logo --- .../Snap.Hutao/Core/Shell/ShellLinkInterop.cs | 10 ++++++++-- src/Snap.Hutao/Snap.Hutao/Snap.Hutao.csproj | 2 +- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/Snap.Hutao/Snap.Hutao/Core/Shell/ShellLinkInterop.cs b/src/Snap.Hutao/Snap.Hutao/Core/Shell/ShellLinkInterop.cs index eb0edf3a..f83bfa29 100644 --- a/src/Snap.Hutao/Snap.Hutao/Core/Shell/ShellLinkInterop.cs +++ b/src/Snap.Hutao/Snap.Hutao/Core/Shell/ShellLinkInterop.cs @@ -20,10 +20,15 @@ internal sealed partial class ShellLinkInterop : IShellLinkInterop public async ValueTask TryCreateDesktopShoutcutForElevatedLaunchAsync() { + string targetLogoPath = Path.Combine(runtimeOptions.DataFolder, "ShellLinkLogo.ico"); string elevatedLauncherPath = Path.Combine(runtimeOptions.DataFolder, "Snap.Hutao.Elevated.Launcher.exe"); try { + Uri sourceLogoUri = "ms-appx:///Assets/Logo.ico".ToUri(); + StorageFile iconFile = await StorageFile.GetFileFromApplicationUriAsync(sourceLogoUri); + await iconFile.OverwriteCopyAsync(targetLogoPath).ConfigureAwait(false); + Uri elevatedLauncherUri = "ms-appx:///Snap.Hutao.Elevated.Launcher.exe".ToUri(); StorageFile launcherFile = await StorageFile.GetFileFromApplicationUriAsync(elevatedLauncherUri); await launcherFile.OverwriteCopyAsync(elevatedLauncherPath).ConfigureAwait(false); @@ -33,10 +38,10 @@ internal sealed partial class ShellLinkInterop : IShellLinkInterop return false; } - return UnsafeTryCreateDesktopShoutcutForElevatedLaunch(elevatedLauncherPath); + return UnsafeTryCreateDesktopShoutcutForElevatedLaunch(targetLogoPath, elevatedLauncherPath); } - private unsafe bool UnsafeTryCreateDesktopShoutcutForElevatedLaunch(string elevatedLauncherPath) + private unsafe bool UnsafeTryCreateDesktopShoutcutForElevatedLaunch(string targetLogoPath, string elevatedLauncherPath) { bool result = false; @@ -47,6 +52,7 @@ internal sealed partial class ShellLinkInterop : IShellLinkInterop pShellLink->SetPath(elevatedLauncherPath); pShellLink->SetArguments(runtimeOptions.FamilyName); pShellLink->SetShowCmd(SHOW_WINDOW_CMD.SW_NORMAL); + pShellLink->SetIconLocation(targetLogoPath, 0); 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 227b8f5c..ad486030 100644 --- a/src/Snap.Hutao/Snap.Hutao/Snap.Hutao.csproj +++ b/src/Snap.Hutao/Snap.Hutao/Snap.Hutao.csproj @@ -328,7 +328,7 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive - + all runtime; build; native; contentfiles; analyzers