use CoCreateInstance to activate ShellLinkW

This commit is contained in:
Lightczx
2023-11-14 09:24:37 +08:00
parent d8b6315e7e
commit fe594864f2
7 changed files with 31 additions and 11 deletions

View File

@@ -314,12 +314,6 @@ public sealed class ResxGenerator : IIncrementalGenerator
string callParams = string.Join(", ", Enumerable.Range(0, args + 1).Select(arg => "arg" + arg.ToString(CultureInfo.InvariantCulture)));
sb.AppendLine($$"""
/// {{comment}}
public static string Format{{ToCSharpNameIdentifier(entry.Name!)}}(global::System.Globalization.CultureInfo? provider, {{inParams}})
{
return GetString(provider, "{{entry.Name}}", {{callParams}})!;
}
/// {{comment}}
public static string Format{{ToCSharpNameIdentifier(entry.Name!)}}({{inParams}})
{

View File

@@ -28,6 +28,7 @@ WaitForSingleObject
WriteProcessMemory
// OLE32
CoCreateInstance
CoWaitForMultipleObjects
// USER32

View File

@@ -0,0 +1,18 @@
using System;
using Windows.Win32.Foundation;
using Windows.Win32.System.Com;
namespace Windows.Win32;
internal static partial class PInvoke
{
/// <inheritdoc cref="CoCreateInstance(Guid*, object, CLSCTX, Guid*, out object)"/>
internal static unsafe HRESULT CoCreateInstance<TClass, TInterface>(object? pUnkOuter, CLSCTX dwClsContext, out TInterface ppv)
where TInterface : class
{
HRESULT hr = CoCreateInstance(typeof(TClass).GUID, pUnkOuter, dwClsContext, typeof(TInterface).GUID, out object o);
ppv = (TInterface)o;
return hr;
}
}

View File

@@ -69,4 +69,4 @@ internal class WinRTCustomMarshaler : ICustomMarshaler
return Marshal.GetObjectForIUnknown(pNativeData);
}
}
}
}

View File

@@ -55,6 +55,9 @@ internal static class DependencyInjection
CultureInfo.CurrentCulture = cultureInfo;
CultureInfo.CurrentUICulture = cultureInfo;
ApplicationLanguages.PrimaryLanguageOverride = cultureInfo.Name;
SH.Culture = cultureInfo;
}
}

View File

@@ -2,11 +2,14 @@
// Licensed under the MIT license.
using System.IO;
using System.Runtime.InteropServices;
using Windows.Storage;
using Windows.Win32;
using Windows.Win32.Foundation;
using Windows.Win32.System.Com;
using Windows.Win32.UI.Shell;
using Windows.Win32.UI.WindowsAndMessaging;
using static Windows.Win32.PInvoke;
namespace Snap.Hutao.Core.Shell;
@@ -37,16 +40,17 @@ internal sealed partial class ShellLinkInterop : IShellLinkInterop
return false;
}
IShellLinkW shellLink = (IShellLinkW)new ShellLink();
HRESULT result = CoCreateInstance<ShellLink, IShellLinkW>(null, CLSCTX.CLSCTX_INPROC_SERVER, out IShellLinkW shellLink);
Marshal.ThrowExceptionForHR(result);
shellLink.SetPath("powershell");
shellLink.SetArguments($"""
-Command "Start-Process shell:AppsFolder\{runtimeOptions.FamilyName}!App -verb runas"
""");
shellLink.SetShowCmd(SHOW_WINDOW_CMD.SW_SHOWMINNOACTIVE);
shellLink.SetIconLocation(targetLogoPath, 0);
string desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string target = Path.Combine(desktop, $"{SH.AppNameAndVersion.Format(runtimeOptions.Version)}.lnk");
string target = Path.Combine(desktop, $"{SH.FormatAppNameAndVersion(runtimeOptions.Version)}.lnk");
IPersistFile persistFile = (IPersistFile)shellLink;
try

View File

@@ -98,7 +98,7 @@ internal sealed partial class MetadataService : IMetadataService, IMetadataServi
else
{
int code = (int)(ex.StatusCode ?? 0);
infoBarService.Error(SH.FormatServiceMetadataHttpRequestFailed(CultureInfo.CurrentCulture, code));
infoBarService.Error(SH.FormatServiceMetadataHttpRequestFailed(code));
}
return false;