-
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathShellAssociationProvider.cs
More file actions
228 lines (181 loc) · 6.22 KB
/
Copy pathShellAssociationProvider.cs
File metadata and controls
228 lines (181 loc) · 6.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Windows.Win32;
using Windows.Win32.UI.Shell;
using Windows.Win32.UI.WindowsAndMessaging;
using WinQuickLook.Extensions;
namespace WinQuickLook.Providers;
public class ShellAssociationProvider
{
public class Entry
{
public required string Name { get; init; }
public required bool IsDefault { get; init; }
public ImageSource? Icon { get; init; }
}
public bool TryGetDefault(FileInfo fileInfo, [NotNullWhen(true)] out Entry? entry)
{
var pcchOut = 0u;
PInvoke.AssocQueryString(ASSOCF.ASSOCF_INIT_IGNOREUNKNOWN, ASSOCSTR.ASSOCSTR_FRIENDLYAPPNAME, fileInfo.Extension, null, Span<char>.Empty, ref pcchOut);
if (pcchOut == 0)
{
entry = default;
return false;
}
Span<char> pszOut = stackalloc char[(int)pcchOut];
PInvoke.AssocQueryString(ASSOCF.ASSOCF_INIT_IGNOREUNKNOWN, ASSOCSTR.ASSOCSTR_FRIENDLYAPPNAME, fileInfo.Extension, null, pszOut, ref pcchOut);
entry = new Entry { Name = new string(pszOut), IsDefault = true };
return true;
}
public IReadOnlyList<Entry> GetRecommends(FileInfo fileInfo)
{
if (PInvoke.SHAssocEnumHandlers(fileInfo.Extension, ASSOC_FILTER.ASSOC_FILTER_RECOMMENDED, out var enumAssocHandlers).Failed)
{
return [];
}
try
{
var recommends = new List<Entry>();
var assocHandlers = new IAssocHandler[8];
while (enumAssocHandlers.Next(assocHandlers, out var fetched).Succeeded)
{
if (fetched == 0)
{
break;
}
for (var i = 0; i < fetched; i++)
{
var assocHandler = assocHandlers[i];
try
{
assocHandler.GetUIName(out var pUiName);
assocHandler.GetIconLocation(out var pIconLocation, out var iconIndex);
var iconLocation = pIconLocation.ToString();
var icon = GetIconFromLocation(iconLocation, iconIndex);
recommends.Add(new Entry
{
Name = pUiName.ToString()!,
IsDefault = false,
Icon = icon
});
}
finally
{
Marshal.ReleaseComObject(assocHandler);
}
}
}
return recommends;
}
finally
{
Marshal.ReleaseComObject(enumAssocHandlers);
}
}
public void Invoke(string appName, FileInfo fileInfo)
{
if (PInvoke.SHAssocEnumHandlers(fileInfo.Extension, ASSOC_FILTER.ASSOC_FILTER_RECOMMENDED, out var enumAssocHandlers).Failed)
{
return;
}
try
{
var assocHandlers = new IAssocHandler[8];
while (enumAssocHandlers.Next(assocHandlers, out var fetched).Succeeded)
{
if (fetched == 0)
{
break;
}
for (var i = 0; i < fetched; i++)
{
var assocHandler = assocHandlers[i];
try
{
assocHandler.GetUIName(out var pUiName);
if (appName != pUiName.ToString())
{
continue;
}
PInvoke.SHCreateItemFromParsingName(fileInfo.FullName, null, out IShellItem shellItem);
shellItem.BindToHandler<Windows.Win32.System.Com.IDataObject>(null, PInvoke.BHID_DataObject, out var dataObject);
assocHandler.Invoke(dataObject);
Marshal.ReleaseComObject(dataObject);
Marshal.ReleaseComObject(shellItem);
return;
}
finally
{
Marshal.ReleaseComObject(assocHandler);
}
}
}
}
finally
{
Marshal.ReleaseComObject(enumAssocHandlers);
}
}
private static BitmapSource? GetIconFromLocation(string? iconLocation, int iconIndex)
{
if (iconLocation is null)
{
return null;
}
if (Path.IsPathFullyQualified(iconLocation))
{
return GetIconFromResource(iconLocation, iconIndex);
}
if (iconLocation.StartsWith('@'))
{
return GetIconFromIndirectString(iconLocation);
}
return null;
}
private static BitmapSource? GetIconFromResource(string path, int iconIndex)
{
Span<HICON> iconSmall = new HICON[1];
PInvoke.ExtractIconEx(path, iconIndex, phiconSmall: iconSmall);
try
{
return Imaging.CreateBitmapSourceFromHIcon(iconSmall[0], Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions())
.AsFreeze();
}
catch
{
return null;
}
finally
{
PInvoke.DestroyIcon(iconSmall[0]);
}
}
private static BitmapImage? GetIconFromIndirectString(string path)
{
Span<char> pszOut = new char[512];
if (PInvoke.SHLoadIndirectString(path, pszOut).Failed)
{
return null;
}
var iconImageLocation = new string(pszOut.TrimEnd('\0'));
if (!File.Exists(iconImageLocation))
{
return null;
}
var bitmap = new BitmapImage();
using (bitmap.Initialize())
{
bitmap.CreateOptions = BitmapCreateOptions.None;
bitmap.CacheOption = BitmapCacheOption.None;
bitmap.UriSource = new Uri(iconImageLocation);
}
return bitmap;
}
}