diff --git a/QuickLook/Helpers/AutoStartupHelper.cs b/QuickLook/Helpers/AutoStartupHelper.cs index 6e80a7e..7ccdf00 100644 --- a/QuickLook/Helpers/AutoStartupHelper.cs +++ b/QuickLook/Helpers/AutoStartupHelper.cs @@ -36,10 +36,7 @@ namespace QuickLook.Helpers { File.Create(StartupFullPath).Close(); - var shl = new Shell(); - var dir = shl.NameSpace(Path.GetDirectoryName(StartupFullPath)); - var itm = dir.Items().Item(Path.GetFileName(StartupFullPath)); - var lnk = (ShellLinkObject) itm.GetLink; + var lnk = ShellLinkHelper.OpenShellLink(StartupFullPath); lnk.Path = App.AppFullPath; lnk.Arguments = "/autorun"; // silent diff --git a/QuickLook/Helpers/ShellLinkHelper.cs b/QuickLook/Helpers/ShellLinkHelper.cs new file mode 100644 index 0000000..44b5c82 --- /dev/null +++ b/QuickLook/Helpers/ShellLinkHelper.cs @@ -0,0 +1,78 @@ +// Copyright © 2018 Paddy Xu +// +// 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.IO; +using System.Threading; +using System.Threading.Tasks; +using Shell32; + +namespace QuickLook.Helpers +{ + public class ShellLinkHelper + { + public static ShellLinkObject OpenShellLink(string path) + { + if (Thread.CurrentThread.GetApartmentState() != ApartmentState.STA) + return StartSTATask(() => OpenShellLink(path)).Result; + + var shl = new Shell(); + var dir = shl.NameSpace(Path.GetDirectoryName(path)); + var itm = dir.Items().Item(Path.GetFileName(path)); + var lnk = (ShellLinkObject) itm.GetLink; + return lnk; + } + + public static string GetTarget(string path) + { + if (Path.GetExtension(path).ToLower() != ".lnk") + return path; + + try + { + return Thread.CurrentThread.GetApartmentState() != ApartmentState.STA + ? StartSTATask(() => GetTarget(path)).Result + : OpenShellLink(path).Target.Path; + } + catch (Exception) + { + // ignored + } + + return path; + } + + private static Task StartSTATask(Func func) + { + var tcs = new TaskCompletionSource(); + var thread = new Thread(() => + { + try + { + tcs.SetResult(func()); + } + catch (Exception e) + { + tcs.SetException(e); + } + }); + thread.SetApartmentState(ApartmentState.STA); + thread.Start(); + return tcs.Task; + } + } +} \ No newline at end of file diff --git a/QuickLook/PipeServerManager.cs b/QuickLook/PipeServerManager.cs index d6ae731..d56e5a1 100644 --- a/QuickLook/PipeServerManager.cs +++ b/QuickLook/PipeServerManager.cs @@ -22,6 +22,7 @@ using System.IO.Pipes; using System.Threading.Tasks; using System.Windows; using System.Windows.Threading; +using QuickLook.Helpers; namespace QuickLook { @@ -121,6 +122,8 @@ namespace QuickLook var wParam = msg.Substring(0, split); var lParam = msg.Substring(split + 1, msg.Length - split - 1); + lParam = ShellLinkHelper.GetTarget(lParam); + switch (wParam) { case PipeMessages.RunAndClose: diff --git a/QuickLook/QuickLook.csproj b/QuickLook/QuickLook.csproj index cc931a0..5497c67 100644 --- a/QuickLook/QuickLook.csproj +++ b/QuickLook/QuickLook.csproj @@ -143,6 +143,7 @@ +