Skip to content

Commit

Permalink
Fix crash when starting from non ASCII path
Browse files Browse the repository at this point in the history
This fixes a crash that occurs when the launcher is started from a
folder whose absolute path does contain non ASCII characters. This
was due to the use of the WshRuntimeLibrary (a wrapper around COM
libraries) that does not handle Unicode (as far as my deductions
go). When assigning the target path to the shortcut object, the
runtime would error.

This commit replaces the WshRuntimeLibrary with a COM wrapper around
the IShellLink interface, using its Unicode oriented functions.

The WshRuntimeLibrary is removed from dependencies and the .NET
framework is updated to 4.8 as 4.0 has reached EOL.

Tested on: Win10 64-bits.
  • Loading branch information
Sylmir committed Dec 28, 2024
1 parent 0c39c9a commit 036e218
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 28 deletions.
59 changes: 48 additions & 11 deletions Universal Tomb Launcher/Helpers/ShellHelper.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,60 @@
using IWshRuntimeLibrary;
using System;
using System.IO;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using System.Text;

namespace UniversalTombLauncher.Helpers
{
internal static class ShellHelper
{
public static IWshShortcut CreateShortcutWithIcon(string exeFilePath, string iconLocation)
public static IPersistFile CreateShortcutWithIcon(string exeFilePath, string iconLocation, string args)
{
string fileName = Path.GetFileNameWithoutExtension(exeFilePath);
IShellLink link = (IShellLink)new ShellLink();
link.SetPath(exeFilePath);
link.SetWorkingDirectory(Path.GetDirectoryName(exeFilePath));
link.SetIconLocation(iconLocation, 0);
link.SetArguments(args);
return (IPersistFile)link;
}

var shell = new WshShell();
string shortcutPath = Path.Combine(Path.GetTempPath(), fileName + ".lnk");
public static string SaveShortcut(IPersistFile shortcut, string exeFilePath)
{
string shortcutPath = Path.Combine(Path.GetTempPath(), Path.GetFileNameWithoutExtension(exeFilePath) + ".lnk");
shortcut.Save(shortcutPath, false);
return shortcutPath;
}
}

var shortcut = (IWshShortcut)shell.CreateShortcut(shortcutPath);
shortcut.TargetPath = exeFilePath;
shortcut.WorkingDirectory = Path.GetDirectoryName(exeFilePath);
shortcut.IconLocation = iconLocation;
[ComImport]
[Guid("00021401-0000-0000-C000-000000000046")]
internal class ShellLink
{
}

return shortcut;
}
[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);
}
}
7 changes: 2 additions & 5 deletions Universal Tomb Launcher/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ private static string CreateGameShortcut(string exeFilePath, bool setup, bool de
{
string iconLocation = Assembly.GetExecutingAssembly().Location; // Target icon is the icon of this launcher

var shortcut = ShellHelper.CreateShortcutWithIcon(exeFilePath, iconLocation);
var argumentsBuilder = new StringBuilder();

if (setup)
Expand All @@ -134,10 +133,8 @@ private static string CreateGameShortcut(string exeFilePath, bool setup, bool de
if (debug)
argumentsBuilder.Append("-debug ");

shortcut.Arguments = argumentsBuilder.ToString();
shortcut.Save();

return shortcut.FullName;
var shortcut = ShellHelper.CreateShortcutWithIcon(exeFilePath, iconLocation, argumentsBuilder.ToString());
return ShellHelper.SaveShortcut(shortcut, exeFilePath);
}
}
}
14 changes: 2 additions & 12 deletions Universal Tomb Launcher/Universal Tomb Launcher.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
<OutputType>WinExe</OutputType>
<RootNamespace>UniversalTombLauncher</RootNamespace>
<AssemblyName>launch</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
Expand Down Expand Up @@ -79,17 +80,6 @@
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<COMReference Include="IWshRuntimeLibrary">
<Guid>{F935DC20-1CF0-11D0-ADB9-00C04FD58A0B}</Guid>
<VersionMajor>1</VersionMajor>
<VersionMinor>0</VersionMinor>
<Lcid>0</Lcid>
<WrapperTool>tlbimp</WrapperTool>
<Isolated>False</Isolated>
<EmbedInteropTypes>True</EmbedInteropTypes>
</COMReference>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Forms\FormExtraSettings.resx">
<DependentUpon>FormExtraSettings.cs</DependentUpon>
Expand Down

0 comments on commit 036e218

Please sign in to comment.