Skip to content

Commit 114de45

Browse files
committed
Remove dependency on IWshRuntimeLibrary by using reflection instead
1 parent 8799b9c commit 114de45

File tree

3 files changed

+75
-25
lines changed

3 files changed

+75
-25
lines changed

Caffeinated.csproj

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
<Compile Include="BaseForm.cs">
6161
<SubType>Form</SubType>
6262
</Compile>
63+
<Compile Include="ReflectedShell.cs" />
6364
<Compile Include="Settings.cs" />
6465
<Compile Include="SettingsForm.cs">
6566
<SubType>Form</SubType>
@@ -98,29 +99,12 @@
9899
<DesignTimeSharedInput>True</DesignTimeSharedInput>
99100
</Compile>
100101
</ItemGroup>
101-
<ItemGroup>
102-
<COMReference Include="IWshRuntimeLibrary">
103-
<Guid>{F935DC20-1CF0-11D0-ADB9-00C04FD58A0B}</Guid>
104-
<VersionMajor>1</VersionMajor>
105-
<VersionMinor>0</VersionMinor>
106-
<Lcid>0</Lcid>
107-
<WrapperTool>tlbimp</WrapperTool>
108-
<Isolated>False</Isolated>
109-
<EmbedInteropTypes>True</EmbedInteropTypes>
110-
</COMReference>
111-
</ItemGroup>
112102
<ItemGroup>
113103
<None Include="Resources\cup-coffee-icon.ico" />
114104
</ItemGroup>
115105
<ItemGroup>
116106
<None Include="Resources\cup-coffee-icon-bw.ico" />
117107
</ItemGroup>
118-
<ItemGroup>
119-
<None Include="Resources\cup-coffee-icon-128.png" />
120-
</ItemGroup>
121-
<ItemGroup>
122-
<None Include="Resources\cup-coffee-icon-48.png" />
123-
</ItemGroup>
124108
<ItemGroup>
125109
<Content Include="Resources\cup-coffee-icon-64.png" />
126110
<Content Include="Resources\cup-coffee-icon-96.png" />

ReflectedShell.cs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Reflection;
6+
using System.IO;
7+
8+
namespace Caffeinated {
9+
class ReflectedShell {
10+
private const BindingFlags PublicInstance =
11+
BindingFlags.Public | BindingFlags.Instance;
12+
13+
private Type type;
14+
private object shell;
15+
16+
public ReflectedShell() {
17+
this.type = Type.GetTypeFromProgID("WScript.Shell");
18+
this.shell = Activator.CreateInstance(type);
19+
}
20+
21+
public object CreateShortcut(
22+
string linkFileName,
23+
string targetPath,
24+
string workingDir = null
25+
26+
) {
27+
object shortcut = type.InvokeMember(
28+
"CreateShortcut", PublicInstance | BindingFlags.InvokeMethod,
29+
null, shell, new object[] { linkFileName }
30+
);
31+
32+
Type shortcutType = shortcut.GetType();
33+
shortcutType.InvokeMember(
34+
"TargetPath", PublicInstance | BindingFlags.SetProperty,
35+
null, shortcut, new object[] { targetPath }
36+
);
37+
38+
if (workingDir != null) {
39+
shortcutType.InvokeMember(
40+
"WorkingDirectory",
41+
PublicInstance | BindingFlags.SetProperty,
42+
null, shortcut, new object[] { workingDir }
43+
);
44+
}
45+
46+
shortcutType.InvokeMember(
47+
"Save", PublicInstance | BindingFlags.InvokeMethod,
48+
null, shortcut, null
49+
);
50+
51+
return shortcut;
52+
}
53+
54+
public string GetSpecialFolder(string item) {
55+
object specFolders = type.InvokeMember(
56+
"SpecialFolders", PublicInstance | BindingFlags.GetProperty,
57+
null, shell, null
58+
);
59+
60+
Type specFoldersType = specFolders.GetType();
61+
object path = specFoldersType.InvokeMember(
62+
"Item", PublicInstance | BindingFlags.InvokeMethod,
63+
null, specFolders, new object[] { item }
64+
);
65+
66+
return path as string;
67+
}
68+
}
69+
}

SettingsForm.cs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,25 +46,22 @@ EventArgs e
4646
}
4747
}
4848

49-
string GetShortcutPath(IWshRuntimeLibrary.WshShell shell = null) {
49+
static string GetShortcutPath(ReflectedShell shell = null) {
5050
if (shell == null) {
51-
shell = new IWshRuntimeLibrary.WshShell();
51+
shell = new ReflectedShell();
5252
}
53-
var startup = (string)shell.SpecialFolders.Item("Startup");
53+
var startup = shell.GetSpecialFolder("Startup");
5454
return Path.Combine(startup, "Caffeinated.lnk");
5555
}
5656

5757
private void StartupChkBox_CheckedChanged(object sender, EventArgs e) {
58-
var shell = new IWshRuntimeLibrary.WshShell();
58+
var shell = new ReflectedShell();
5959
var shortcut = GetShortcutPath(shell);
6060
var executable = Assembly.GetExecutingAssembly()
6161
.GetName().CodeBase;
6262
if (StartupChkBox.Checked) {
6363
// create shortcut in startup items folder in start menu
64-
object objLink = shell.CreateShortcut(shortcut);
65-
var link = (IWshRuntimeLibrary.IWshShortcut)objLink;
66-
link.TargetPath = executable;
67-
link.Save();
64+
shell.CreateShortcut(shortcut, executable);
6865
}
6966
else {
7067
// remove shortcut if it exists

0 commit comments

Comments
 (0)