Finally found good config for removing IWshRuntimeLibrary dependency

Related to issue #151.
This commit is contained in:
Travis Nickles 2018-01-26 00:41:20 -06:00
parent dc18449179
commit b3c9796fbb
2 changed files with 37 additions and 23 deletions

View File

@ -7,6 +7,9 @@ using System.IO;
using System.Xml; using System.Xml;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Threading;
using System.Diagnostics;
using System.Threading.Tasks;
namespace DS4Windows namespace DS4Windows
{ {
@ -384,7 +387,7 @@ namespace DS4Windows
bnAddPrograms.Text = Properties.Resources.Loading; bnAddPrograms.Text = Properties.Resources.Loading;
bnAddPrograms.Enabled = false; bnAddPrograms.Enabled = false;
cMSPrograms.Items.Remove(addSteamGamesToolStripMenuItem); cMSPrograms.Items.Remove(addSteamGamesToolStripMenuItem);
await System.Threading.Tasks.Task.Run(() => GetApps(steamgamesdir)); await Task.Run(() => GetApps(steamgamesdir));
addLoadedApps(); addLoadedApps();
} }
@ -395,7 +398,7 @@ namespace DS4Windows
{ {
bnAddPrograms.Text = Properties.Resources.Loading; bnAddPrograms.Text = Properties.Resources.Loading;
bnAddPrograms.Enabled = false; bnAddPrograms.Enabled = false;
await System.Threading.Tasks.Task.Run(() => GetApps(fbd.SelectedPath)); await Task.Run(() => GetApps(fbd.SelectedPath));
addLoadedApps(); addLoadedApps();
} }
} }
@ -423,7 +426,7 @@ namespace DS4Windows
bnAddPrograms.Text = Properties.Resources.Loading; bnAddPrograms.Text = Properties.Resources.Loading;
bnAddPrograms.Enabled = false; bnAddPrograms.Enabled = false;
cMSPrograms.Items.Remove(addOriginGamesToolStripMenuItem); cMSPrograms.Items.Remove(addOriginGamesToolStripMenuItem);
await System.Threading.Tasks.Task.Run(() => GetApps(origingamesdir)); await Task.Run(() => GetApps(origingamesdir));
addLoadedApps(); addLoadedApps();
} }
@ -432,7 +435,7 @@ namespace DS4Windows
bnAddPrograms.Text = Properties.Resources.Loading; bnAddPrograms.Text = Properties.Resources.Loading;
bnAddPrograms.Enabled = false; bnAddPrograms.Enabled = false;
cMSPrograms.Items.Remove(addProgramsFromStartMenuToolStripMenuItem); cMSPrograms.Items.Remove(addProgramsFromStartMenuToolStripMenuItem);
await System.Threading.Tasks.Task.Run(() => GetShortcuts(Environment.GetFolderPath(Environment.SpecialFolder.StartMenu) + "\\Programs")); await Task.Run(() => GetShortcuts(Environment.GetFolderPath(Environment.SpecialFolder.StartMenu) + "\\Programs"));
addLoadedApps(); addLoadedApps();
} }
@ -474,35 +477,57 @@ namespace DS4Windows
public static string ResolveShortcut(string filePath) public static string ResolveShortcut(string filePath)
{ {
// IWshRuntimeLibrary is in the COM library "Windows Script Host Object Model" // IWshRuntimeLibrary is in the COM library "Windows Script Host Object Model"
IWshRuntimeLibrary.WshShell shell = new IWshRuntimeLibrary.WshShell(); //IWshRuntimeLibrary.WshShell shell = new IWshRuntimeLibrary.WshShell();
Type t = Type.GetTypeFromCLSID(new Guid("72C24DD5-D70A-438B-8A42-98424B88AFB8")); // Windows Script Host Shell Object
dynamic shell = Activator.CreateInstance(t);
string result;
try try
{ {
IWshRuntimeLibrary.IWshShortcut shortcut = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(filePath); //IWshRuntimeLibrary.IWshShortcut shortcut = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(filePath);
return shortcut.TargetPath; var shortcut = shell.CreateShortcut(filePath);
result = shortcut.TargetPath;
Marshal.FinalReleaseComObject(shortcut);
} }
catch (COMException) catch (COMException)
{ {
// A COMException is thrown if the file is not a valid shortcut (.lnk) file // A COMException is thrown if the file is not a valid shortcut (.lnk) file
return null; result = null;
} }
finally
{
Marshal.FinalReleaseComObject(shell);
}
return result;
} }
public static string ResolveShortcutAndArgument(string filePath) public static string ResolveShortcutAndArgument(string filePath)
{ {
// IWshRuntimeLibrary is in the COM library "Windows Script Host Object Model" // IWshRuntimeLibrary is in the COM library "Windows Script Host Object Model"
IWshRuntimeLibrary.WshShell shell = new IWshRuntimeLibrary.WshShell(); //IWshRuntimeLibrary.WshShell shell = new IWshRuntimeLibrary.WshShell();
Type t = Type.GetTypeFromCLSID(new Guid("72C24DD5-D70A-438B-8A42-98424B88AFB8")); // Windows Script Host Shell Object
dynamic shell = Activator.CreateInstance(t);
string result;
try try
{ {
IWshRuntimeLibrary.IWshShortcut shortcut = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(filePath); //IWshRuntimeLibrary.IWshShortcut shortcut = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(filePath);
return shortcut.TargetPath + " " + shortcut.Arguments; var shortcut = shell.CreateShortcut(filePath);
result = shortcut.TargetPath + " " + shortcut.Arguments;
Marshal.FinalReleaseComObject(shortcut);
} }
catch (COMException) catch (COMException)
{ {
// A COMException is thrown if the file is not a valid shortcut (.lnk) file // A COMException is thrown if the file is not a valid shortcut (.lnk) file
return null; result = null;
} }
finally
{
Marshal.FinalReleaseComObject(shell);
}
return result;
} }
private void cBTurnOffDS4W_CheckedChanged(object sender, EventArgs e) private void cBTurnOffDS4W_CheckedChanged(object sender, EventArgs e)

View File

@ -1116,17 +1116,6 @@
<ItemGroup> <ItemGroup>
<None Include="App.config" /> <None Include="App.config" />
</ItemGroup> </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> <ItemGroup>
<Content Include="HidLibrary\LICENSE" /> <Content Include="HidLibrary\LICENSE" />
<Content Include="Resources\360 fades.png" /> <Content Include="Resources\360 fades.png" />