From b6b1556fe4058fe3dffab998088c58ff33121d56 Mon Sep 17 00:00:00 2001 From: Paddy Xu Date: Sun, 26 Apr 2015 23:55:31 +0200 Subject: [PATCH] Remove COM DLL dependence, as #72 --- LEGUI/AppConfig.xaml.cs | 33 +++++++++++++----------------- LEGUI/LEGUI.csproj | 12 +---------- LEGUI/ShellLink.cs | 45 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 30 deletions(-) create mode 100644 LEGUI/ShellLink.cs diff --git a/LEGUI/AppConfig.xaml.cs b/LEGUI/AppConfig.xaml.cs index 4c4e0cd..eeab124 100644 --- a/LEGUI/AppConfig.xaml.cs +++ b/LEGUI/AppConfig.xaml.cs @@ -5,8 +5,8 @@ using System.IO; using System.Linq; using System.Reflection; +using System.Runtime.InteropServices.ComTypes; using System.Windows; -using IWshRuntimeLibrary; using LECommonLibrary; using File = System.IO.File; @@ -74,26 +74,21 @@ private void CreateShortcut(string path) { try { - var shortcut = - new WshShell().CreateShortcut(string.Format("{0}\\{1}.lnk", - Environment.GetFolderPath(Environment.SpecialFolder - .DesktopDirectory), - Path.GetFileNameWithoutExtension(path))) as IWshShortcut; + IShellLink link = (IShellLink)new ShellLink(); - if (shortcut == null) - { - MessageBox.Show("Create shortcut error", "LE", MessageBoxButton.OK, MessageBoxImage.Error); - return; - } + link.SetPath(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), + "LEProc.exe")); + link.SetArguments(String.Format("-run \"{0}\"", path)); + link.SetIconLocation(AssociationReader.GetAssociatedIcon(Path.GetExtension(path)).Replace("%1", path), 0); + + link.SetDescription(string.Format("Run {0} with Locale Emulator", Path.GetFileName(path))); + link.SetWorkingDirectory(Path.GetDirectoryName(path)); - shortcut.TargetPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), - "LEProc.exe"); - shortcut.Arguments = String.Format("-run \"{0}\"", path); - shortcut.WorkingDirectory = Path.GetDirectoryName(path); - shortcut.WindowStyle = 1; - shortcut.Description = string.Format("Run {0} with Locale Emulator", Path.GetFileName(path)); - shortcut.IconLocation = AssociationReader.GetAssociatedIcon(Path.GetExtension(path)).Replace("%1", path); - shortcut.Save(); + IPersistFile file = (IPersistFile)link; + file.Save( + Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), + Path.GetFileNameWithoutExtension(path) + ".lnk"), + false); } catch (Exception e) { diff --git a/LEGUI/LEGUI.csproj b/LEGUI/LEGUI.csproj index ff22aa8..fa4aa53 100644 --- a/LEGUI/LEGUI.csproj +++ b/LEGUI/LEGUI.csproj @@ -81,6 +81,7 @@ AppConfig.xaml + Designer MSBuild:Compile @@ -150,17 +151,6 @@ False - - - {F935DC20-1CF0-11D0-ADB9-00C04FD58A0B} - 1 - 0 - 0 - tlbimp - False - True - - False diff --git a/LEGUI/ShellLink.cs b/LEGUI/ShellLink.cs new file mode 100644 index 0000000..c625cc9 --- /dev/null +++ b/LEGUI/ShellLink.cs @@ -0,0 +1,45 @@ +using System; +using System.Runtime.InteropServices; +using System.Text; + +namespace LEGUI +{ + [ComImport] + [Guid("00021401-0000-0000-C000-000000000046")] + internal class ShellLink + { + } + + [ComImport] + [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] + [Guid("000214F9-0000-0000-C000-000000000046")] + internal interface IShellLink + { + void GetPath([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszFile, + int cchMaxPath, + out IntPtr pfd, + int fFlags); + + void GetIDList(out IntPtr ppidl); + void SetIDList(IntPtr pidl); + void GetDescription([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszName, int cchMaxName); + void SetDescription([MarshalAs(UnmanagedType.LPWStr)] string pszName); + void GetWorkingDirectory([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszDir, int cchMaxPath); + void SetWorkingDirectory([MarshalAs(UnmanagedType.LPWStr)] string pszDir); + void GetArguments([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszArgs, int cchMaxPath); + void SetArguments([MarshalAs(UnmanagedType.LPWStr)] string pszArgs); + void GetHotkey(out short pwHotkey); + void SetHotkey(short wHotkey); + void GetShowCmd(out int piShowCmd); + void SetShowCmd(int iShowCmd); + + void GetIconLocation([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszIconPath, + int cchIconPath, + out int piIcon); + + void SetIconLocation([MarshalAs(UnmanagedType.LPWStr)] string pszIconPath, int iIcon); + void SetRelativePath([MarshalAs(UnmanagedType.LPWStr)] string pszPathRel, int dwReserved); + void Resolve(IntPtr hwnd, int fFlags); + void SetPath([MarshalAs(UnmanagedType.LPWStr)] string pszFile); + } +}