forked from lin-ycv/EverythingPowerToys
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNativeMethods.cs
368 lines (337 loc) · 14.4 KB
/
NativeMethods.cs
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.IO.Abstractions;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using Community.PowerToys.Run.Plugin.Everything.Properties;
using Microsoft.Win32;
using Wox.Plugin;
using Wox.Plugin.Logger;
namespace Community.PowerToys.Run.Plugin.Everything
{
internal static class NativeMethods
{
[Flags]
internal enum Request
{
FILE_NAME = 0x00000001,
PATH = 0x00000002,
FULL_PATH_AND_FILE_NAME = 0x00000004,
EXTENSION = 0x00000008,
SIZE = 0x00000010,
DATE_CREATED = 0x00000020,
DATE_MODIFIED = 0x00000040,
DATE_ACCESSED = 0x00000080,
ATTRIBUTES = 0x00000100,
FILE_LIST_FILE_NAME = 0x00000200,
RUN_COUNT = 0x00000400,
DATE_RUN = 0x00000800,
DATE_RECENTLY_CHANGED = 0x00001000,
HIGHLIGHTED_FILE_NAME = 0x00002000,
HIGHLIGHTED_PATH = 0x00004000,
HIGHLIGHTED_FULL_PATH_AND_FILE_NAME = 0x00008000,
}
internal enum Sort
{
NAME_ASCENDING = 1,
NAME_DESCENDING,
PATH_ASCENDING,
PATH_DESCENDING,
SIZE_ASCENDING,
SIZE_DESCENDING,
EXTENSION_ASCENDING,
EXTENSION_DESCENDING,
TYPE_NAME_ASCENDING,
TYPE_NAME_DESCENDING,
DATE_CREATED_ASCENDING,
DATE_CREATED_DESCENDING,
DATE_MODIFIED_ASCENDING,
DATE_MODIFIED_DESCENDING,
ATTRIBUTES_ASCENDING,
ATTRIBUTES_DESCENDING,
FILE_LIST_FILENAME_ASCENDING,
FILE_LIST_FILENAME_DESCENDING,
RUN_COUNT_ASCENDING,
RUN_COUNT_DESCENDING,
DATE_RECENTLY_CHANGED_ASCENDING,
DATE_RECENTLY_CHANGED_DESCENDING,
DATE_ACCESSED_ASCENDING,
DATE_ACCESSED_DESCENDING,
DATE_RUN_ASCENDING,
DATE_RUN_DESCENDING,
}
[Flags]
internal enum AssocF
{
NONE = 0x00000000,
INIT_NOREMAPCLSID = 0x00000001,
INIT_BYEXENAME = 0x00000002,
INIT_DEFAULTTOSTAR = 0x00000004,
INIT_DEFAULTTOFOLDER = 0x00000008,
NOUSERSETTINGS = 0x00000010,
NOTRUNCATE = 0x00000020,
VERIFY = 0x00000040,
REMAPRUNDLL = 0x00000080,
NOFIXUPS = 0x00000100,
IGNOREBASECLASS = 0x00000200,
INIT_IGNOREUNKNOWN = 0x00000400,
INIT_FIXED_PROGID = 0x00000800,
IS_PROTOCOL = 0x00001000,
INIT_FOR_FILE = 0x00002000,
}
internal enum AssocStr
{
COMMAND = 1,
EXECUTABLE,
FRIENDLYDOCNAME,
FRIENDLYAPPNAME,
NOOPEN,
SHELLNEWVALUE,
DDECOMMAND,
DDEIFEXEC,
DDEAPPLICATION,
DDETOPIC,
INFOTIP,
QUICKTIP,
TILEINFO,
CONTENTTYPE,
DEFAULTICON,
SHELLEXTENSION,
DROPTARGET,
DELEGATEEXECUTE,
SUPPORTED_URI_PROTOCOLS,
PROGID,
APPID,
APPPUBLISHER,
APPICONREFERENCE,
MAX,
}
internal const string dllName = "Everything64.dll";
[DllImport(dllName)]
internal static extern uint Everything_GetNumResults();
[DllImport(dllName, CharSet = CharSet.Unicode)]
internal static extern void Everything_GetResultFullPathName(uint nIndex, StringBuilder lpString, uint nMaxCount);
[DllImport(dllName)]
internal static extern bool Everything_IsFolderResult(uint index);
[DllImport(dllName)]
internal static extern bool Everything_QueryW(bool bWait);
[DllImport(dllName)]
internal static extern void Everything_SetMax(uint dwMax);
[DllImport(dllName)]
internal static extern void Everything_SetRegex(bool bEnable);
[DllImport(dllName)]
internal static extern void Everything_SetRequestFlags(Request RequestFlags);
[DllImport(dllName, CharSet = CharSet.Unicode)]
internal static extern uint Everything_SetSearchW(string lpSearchString);
[DllImport(dllName)]
internal static extern bool Everything_SetMatchPath(bool bEnable);
[DllImport(dllName)]
internal static extern void Everything_SetSort(Sort SortType);
[DllImport("Shlwapi.dll", SetLastError = true, CharSet = CharSet.Unicode)]
internal static extern uint AssocQueryString(AssocF flags, AssocStr str, string pszAssoc, string pszExtra, [Out] char[] pszOut, [In][Out] ref uint pcchOut);
private static uint max = 20;
private static Sort sort = Sort.DATE_MODIFIED_DESCENDING;
private static Dictionary<string, string> filters = new Dictionary<string, string>();
private static bool firstrun = true;
public static void EverythingSetup(bool debug)
{
Everything_SetRequestFlags(Request.FULL_PATH_AND_FILE_NAME);
GetCustomSettings(debug);
Everything_SetSort(sort);
}
private static void GetCustomSettings(bool debug)
{
string[] strArr;
try { strArr = File.ReadAllLines("modules\\launcher\\Plugins\\Everything\\settings.toml"); }
catch { return; }
var culture = new System.Globalization.CultureInfo("en-US");
foreach (string str in strArr)
{
if (str.Length == 0 || str[0] == '#') continue;
string[] kv = str.Split('=');
if (kv.Length != 2) continue;
string key = kv[0].Trim();
if (key == "max")
{
try { max = uint.Parse(kv[1].Trim(), culture.NumberFormat); }
catch { }
}
else if (key == "sort")
{
try { sort = (Sort)int.Parse(kv[1].Trim(), culture.NumberFormat); }
catch { }
}
else if (key.Contains(':'))
{
filters.TryAdd(key.Split(':')[0].ToLowerInvariant(), kv[1].Trim());
}
}
if (debug)
{
string msg = $"Max: {max}\nSort: {sort}\nFilters: {string.Join("\n - ", filters.Select(x => { return x.Key + "_" + x.Value; }))}";
Log.Info(msg, typeof(NativeMethods));
}
}
public static IEnumerable<Result> EverythingSearch(string qry, bool preview, bool legacy, bool debug)
{
string orgqry = qry;
if (!preview && legacy && firstrun)
Icons = GetFileTypeAndIcon();
Everything_SetMax(max);
if (qry.Contains(':'))
{
string[] nqry = qry.Split(':');
if (filters.ContainsKey(nqry[0].ToLowerInvariant()))
{
Everything_SetMax(0xffffffff);
qry = nqry[1].Trim() + " ext:" + filters[nqry[0].Trim()];
}
}
_ = Everything_SetSearchW(qry);
if (!Everything_QueryW(true))
{
throw new Win32Exception("Unable to Query");
}
uint resultCount = Everything_GetNumResults();
if (debug)
{
Log.Info(qry + " => " + resultCount, typeof(NativeMethods), "EverythingSearch.ResultCount", string.Empty, 217);
}
for (uint i = 0; i < resultCount; i++)
{
StringBuilder buffer = new StringBuilder(260);
Everything_GetResultFullPathName(i, buffer, 260);
string fullPath = buffer.ToString();
string name = Path.GetFileName(fullPath);
bool isFolder = Everything_IsFolderResult(i);
string path = isFolder ? fullPath : Path.GetDirectoryName(fullPath);
string ext = Path.GetExtension(fullPath.Replace(".lnk", string.Empty));
if (debug)
{
Log.Info(i + " : " + name + " = " + fullPath, typeof(NativeMethods), "EverythingSearch.Result", string.Empty, 229);
}
var r = new Result()
{
Title = name,
ToolTipData = debug ?
new ToolTipData(orgqry, qry) :
new ToolTipData("Name : " + name, fullPath),
SubTitle = Resources.plugin_name + ": " + fullPath,
IcoPath = isFolder ? "Images/folder.png" : (preview ?
fullPath :
(string)((legacy ? Icons[ext] : Icon(ext)) ??
"Images/file.png")),
ContextData = new SearchResult()
{
Path = fullPath,
Title = name,
},
Action = e =>
{
using (var process = new Process())
{
process.StartInfo.FileName = fullPath;
process.StartInfo.WorkingDirectory = path;
process.StartInfo.UseShellExecute = true;
try
{
process.Start();
return true;
}
catch (Win32Exception)
{
return false;
}
}
},
QueryTextDisplay = isFolder ? path : name,
};
yield return r;
}
}
#pragma warning disable CS8632 // The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
internal static string? Icon(string doctype)
#pragma warning restore CS8632 // The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
{
uint pcchOut = 0;
_ = AssocQueryString(AssocF.NONE, AssocStr.DEFAULTICON, doctype, null, null, ref pcchOut);
char[] pszOut = new char[pcchOut];
_ = AssocQueryString(AssocF.NONE, AssocStr.DEFAULTICON, doctype, null, pszOut, ref pcchOut);
string doc = Environment.ExpandEnvironmentVariables(new string(pszOut).Split(new char[] { '\"', ',' }, StringSplitOptions.RemoveEmptyEntries)[0].Replace("\"", string.Empty, StringComparison.CurrentCulture).Trim());
if (File.Exists(doc))
return doc;
return null;
}
//Manually traverse the registry
private static Hashtable Icons = new Hashtable();
internal static Hashtable GetFileTypeAndIcon()
{
Hashtable iconsInfo = new Hashtable(StringComparer.CurrentCultureIgnoreCase);
try
{
using (RegistryKey rkRoot = RegistryKey.OpenBaseKey(RegistryHive.ClassesRoot, RegistryView.Registry64))
{
FindExt(rkRoot);
}
using (RegistryKey rkRoot = RegistryKey.OpenBaseKey(RegistryHive.ClassesRoot, RegistryView.Registry32))
{
FindExt(rkRoot);
}
}
catch (Exception e)
{
Log.Exception(e.Message, e, typeof(NativeMethods));
}
firstrun = false;
return iconsInfo;
void FindExt(RegistryKey rkRoot)
{
foreach (string keyName in rkRoot.GetSubKeyNames())
{
if (string.IsNullOrWhiteSpace(keyName) || keyName[0] != '.' || iconsInfo.ContainsKey(keyName))
continue;
try
{
object defaultValue = null;
using (RegistryKey rkKey = rkRoot.OpenSubKey(keyName))
{
defaultValue = rkKey.GetValue(string.Empty);
if (defaultValue == null)
continue;
}
Log.Info((defaultValue == null) + string.Empty, typeof(NativeMethods));
object iconValue = null;
using (RegistryKey rkIcon = rkRoot.OpenSubKey(defaultValue.ToString() + "\\defaulticon"), rkOpen = rkRoot.OpenSubKey(defaultValue.ToString() + "\\shell\\Open\\command"))
{
iconValue = (rkIcon == null) ? rkOpen?.GetValue(string.Empty) : rkIcon.GetValue(string.Empty);
if (iconValue == null)
continue;
}
Log.Info((iconValue == null) + string.Empty, typeof(NativeMethods));
string[] path = iconValue.ToString().Split(new char[] { '\"', ',' }, StringSplitOptions.RemoveEmptyEntries);
if (path.Length > 0 && path[0].Contains('.'))
{
string fileParam = Environment.ExpandEnvironmentVariables(path[0].Replace("\"", string.Empty, StringComparison.CurrentCulture).Trim());
if (!fileParam.EndsWith("dll", StringComparison.CurrentCulture))
iconsInfo.Add(keyName, fileParam);
}
}
catch (Exception e)
{
Log.Exception(keyName + ":" + e.ToString(), e, typeof(NativeMethods));
continue; // something wrong with this key, move on
}
}
}
}
}
}