-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdllmain.cpp
513 lines (378 loc) · 14.1 KB
/
dllmain.cpp
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
/** dllmain.cpp (2023.07.03) P. Stuer **/
#include "pch.h"
#include <CppCoreCheck/Warnings.h>
#pragma warning(disable: 4710 4711 4820 5045 ALL_CPPCORECHECK_WARNINGS)
#include "Resources.h"
#include "Control.h"
#include "Console.h"
#include "ShowSecurityDescriptor.h"
#include "ShowSID.h"
#include "ShowACL.h"
#include "Support.h"
HMODULE _hDLL = 0;
HFONT _hFontGUI = 0;
Console _Console;
Control _Control;
char IniFileName[MAX_PATH] = "lsplugin.ini";
static BOOL OnProcessAttach();
static BOOL OnProcessDetach();
static void ProcessItem(const wstring & filePath);
/// <summary>
/// Entry Point
/// </summary>
BOOL APIENTRY DllMain(HANDLE, DWORD reason, LPVOID)
{
switch (reason)
{
case DLL_PROCESS_ATTACH:
return OnProcessAttach();
case DLL_PROCESS_DETACH:
return OnProcessDetach();
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
}
return TRUE;
}
/// <summary>
/// Called when a user opens lister with F3 or the Quick View Panel with Ctrl+Q, and when the definition string either doesn't exist, or its evaluation returns true.
/// </summary>
HWND __stdcall ListLoad(HWND hWnd, char * filePath, int showFlags)
{
WCHAR FilePathW[MAX_PATH];
::MultiByteToWideChar(CP_ACP, 0, filePath, -1, FilePathW, _countof(FilePathW) - 1);
return ListLoadW(hWnd, FilePathW, showFlags);
}
/// <summary>
/// Called when a user opens lister with F3 or the Quick View Panel with Ctrl+Q, and when the definition string either doesn't exist, or its evaluation returns true.
/// </summary>
HWND __stdcall ListLoadW(HWND hWnd, WCHAR * filePath, int showFlags)
{
DWORD Attributes = ::GetFileAttributesW(filePath);
if ((Attributes == INVALID_FILE_ATTRIBUTES) || !(Attributes & FILE_ATTRIBUTE_DIRECTORY))
return 0;
_Console.Create(hWnd, showFlags & lcp_wraptext, showFlags & lcp_darkmode);
_Console.SetFont(_hFontGUI);
ProcessItem(filePath);
_Console.Show();
return _Console.Handle();
}
/// <summary>
/// Called when a user switches to the next or previous file in lister with 'n' or 'p' keys, or goes to the next/previous file in the Quick View Panel, and when the definition string either doesn't exist, or its evaluation returns true.
/// </summary>
int __stdcall ListLoadNext(HWND hParentWnd, HWND hWnd, char * filePath, int showFlags)
{
WCHAR FilePathW[MAX_PATH];
::MultiByteToWideChar(CP_ACP, 0, filePath, -1, FilePathW, _countof(FilePathW) - 1);
return ListLoadNextW(hParentWnd, hWnd, FilePathW, showFlags);
}
/// <summary>
/// Called when a user switches to the next or previous file in lister with 'n' or 'p' keys, or goes to the next/previous file in the Quick View Panel, and when the definition string either doesn't exist, or its evaluation returns true.
/// </summary>
int __stdcall ListLoadNextW(HWND hParentWnd, HWND, WCHAR * filePath, int)
{
::SendMessageA(hParentWnd, WM_SETREDRAW, FALSE, 0);
_Console.Hide();
ProcessItem(filePath);
_Console.Show();
::SendMessageA(hParentWnd, WM_SETREDRAW, TRUE, 0);
_Console.Redraw();
return LISTPLUGIN_OK;
}
/// <summary>
/// Called when a user closes lister, or loads a different file.
/// </summary>
void __stdcall ListCloseWindow(HWND hWnd)
{
::DestroyWindow(hWnd);
}
/// <summary>
/// Called when the user changes some options in Lister's menu.
/// </summary>
int __stdcall ListSendCommand(HWND hWnd, int command, int parameter)
{
switch (command)
{
// Copy current selection to the clipboard
case lc_copy:
{
::SendMessageW(hWnd, WM_COPY, 0, 0);
return LISTPLUGIN_OK;
}
// New parameters passed to plugin
case lc_newparams:
{
::PostMessageW(::GetParent(hWnd), WM_COMMAND, MAKEWPARAM(0, itm_next), (LPARAM) hWnd);
return LISTPLUGIN_OK;
}
// Select the whole contents.
case lc_selectall:
{
::SendMessageW(hWnd, EM_SETSEL, 0, -1);
return LISTPLUGIN_OK;
}
case lc_setpercent:
{
int LineCount = (int) ::SendMessageW(hWnd, EM_GETLINECOUNT, 0, 0);
if (LineCount <= 0)
return LISTPLUGIN_ERROR;
int Line = ::MulDiv(parameter, LineCount, 100);
int FirstVisibleLine = (int) ::SendMessageW(hWnd, EM_GETFIRSTVISIBLELINE, 0, 0);
::SendMessageW(hWnd, EM_LINESCROLL, 0, (LPARAM) Line - FirstVisibleLine);
FirstVisibleLine = (int) ::SendMessageW(hWnd, EM_GETFIRSTVISIBLELINE, 0, 0);
// Place caret on first visible line!
int FirstChar = (int) ::SendMessageW(hWnd, EM_LINEINDEX, (WPARAM) FirstVisibleLine, 0);
::SendMessageW(hWnd, EM_SETSEL, (WPARAM) FirstChar, FirstChar);
int Percentage = ::MulDiv(FirstVisibleLine, 100, LineCount);
// Update percentage display
::PostMessage(::GetParent(hWnd), WM_COMMAND, MAKEWPARAM(Percentage, itm_percent), (LPARAM) hWnd);
return LISTPLUGIN_OK;
}
}
return LISTPLUGIN_ERROR;
}
/// <summary>
/// Called when the user chooses the print function.
/// </summary>
int __stdcall ListPrint(HWND, char *, char *, int, RECT *)
{
return LISTPLUGIN_ERROR;
}
/// <summary>
/// Called when the user tries to find text in the plugin.
/// </summary>
int _stdcall ListSearchText(HWND hWnd, char * searchString, int searchParameter)
{
if (searchString == nullptr)
return LISTPLUGIN_ERROR;
if (searchString[0] == '\0')
return LISTPLUGIN_ERROR;
int FirstVisibleLine = (int) ::SendMessageW(hWnd, EM_GETFIRSTVISIBLELINE, 0, 0);
int StartPos;
if (searchParameter & lcs_findfirst)
{
//Find first: Start at top visible line
StartPos = (int) ::SendMessageW(hWnd, EM_LINEINDEX, (WPARAM) FirstVisibleLine, 0);
::SendMessageW(hWnd, EM_SETSEL, (WPARAM) StartPos, StartPos);
}
else
{
//Find next: Start at current selection + 1
::SendMessageW(hWnd, EM_GETSEL, (WPARAM) &StartPos, 0);
StartPos += 1;
}
FINDTEXTW ft = { { 0 }, 0 };
ft.chrg.cpMin = StartPos;
ft.chrg.cpMax = (LONG) ::SendMessageW(hWnd, WM_GETTEXTLENGTH, 0, 0);
int Flags = 0;
if (searchParameter & lcs_wholewords)
Flags |= FR_WHOLEWORD;
if (searchParameter & lcs_matchcase)
Flags |= FR_MATCHCASE;
if (!(searchParameter & lcs_backwards))
Flags |= FR_DOWN;
WCHAR SearchStringW[1024];
::MultiByteToWideChar(CP_ACP, 0, searchString, -1, SearchStringW, _countof(SearchStringW) - 1);
ft.lpstrText = SearchStringW;
int TextHead = (int) ::SendMessageW(hWnd, EM_FINDTEXT, (WPARAM) Flags, (LPARAM) &ft);
if (TextHead != -1)
{
int TextTail = TextHead + (int) ::strlen(searchString);
::SendMessageW(hWnd, EM_SETSEL, (WPARAM) TextHead, TextTail);
int LineIndex = (int) ::SendMessageW(hWnd, EM_LINEFROMCHAR, (WPARAM) TextHead, 0) - 3;
if (LineIndex < 0)
LineIndex = 0;
LineIndex -= FirstVisibleLine;
::SendMessageW(hWnd, EM_LINESCROLL, 0, LineIndex);
return LISTPLUGIN_OK;
}
else
::SendMessageW(hWnd, EM_SETSEL, (WPARAM) -1, -1); // Restart search at the beginning
return LISTPLUGIN_ERROR;
}
/// <summary>
/// Called when the user tries to find text in the plugin.
/// </summary>
int _stdcall ListSearchDialog(HWND, int)
{
return LISTPLUGIN_ERROR; // Total Commander should show its own text search dialog.
}
/// <summary>
/// Called when the parent window receives a notification message from the child window.
/// </summary>
int __stdcall ListNotificationReceived(HWND hWnd, int msg, WPARAM wParam, LPARAM)
{
if (msg != WM_COMMAND)
return 0;
if (HIWORD(wParam) != EN_UPDATE)
return 0;
int LineCount = (int) ::SendMessageW(hWnd, EM_GETLINECOUNT, 0, 0);
if (LineCount > 0)
{
int FirstVisibleLine = (int) ::SendMessageW(hWnd, EM_GETFIRSTVISIBLELINE, 0, 0);
int Percentage = ::MulDiv(FirstVisibleLine, 100, LineCount);
// Set the percentage in the Lister title bar.
::PostMessageW(::GetParent(hWnd), WM_COMMAND, MAKEWPARAM(Percentage, itm_percent), (LPARAM) hWnd);
}
return 0;
}
/// <summary>
/// Called immediately after loading the DLL, before ListLoad.
/// </summary>
void __stdcall ListSetDefaultParams(ListDefaultParamStruct * dps)
{
if (dps)
::StringCchCopy(IniFileName, _countof(IniFileName) - 1, dps->DefaultIniName);
}
/// <summary>
/// Called when the plugin gets loaded.
/// </summary>
static BOOL OnProcessAttach()
{
{
INITCOMMONCONTROLSEX icc = { sizeof(icc) };
icc.dwICC = ICC_STANDARD_CLASSES | ICC_TAB_CLASSES | ICC_UPDOWN_CLASS | ICC_USEREX_CLASSES;
::InitCommonControlsEx(&icc);
}
// Load the RichEdit library.
{
if (_hDLL == 0)
{
UINT OldErrorMode = ::SetErrorMode(SEM_NOOPENFILEERRORBOX);
_hDLL = ::LoadLibrary("msftedit.dll"); // 4.1
if (_hDLL < (HMODULE) HINSTANCE_ERROR)
_hDLL = ::LoadLibrary("riched20.dll"); // 3.0
if (_hDLL < (HMODULE) HINSTANCE_ERROR)
_hDLL = ::LoadLibrary("riched32.dll"); // 1.0
if (_hDLL < (HMODULE) HINSTANCE_ERROR)
_hDLL = 0;
::SetErrorMode(OldErrorMode);
}
}
// Create the GUI font (based on the menu font).
{
NONCLIENTMETRICS nm = { sizeof(nm) };
::SystemParametersInfo(SPI_GETNONCLIENTMETRICS, nm.cbSize, &nm, 0);
_hFontGUI = ::CreateFontIndirect(&nm.lfMenuFont);
}
return TRUE;
}
/// <summary>
/// Called when the plugin gets unloaded.
/// </summary>
static BOOL OnProcessDetach()
{
if (_hFontGUI)
{
::DeleteObject(_hFontGUI);
_hFontGUI = 0;
}
if (_hDLL)
{
::FreeLibrary(_hDLL);
_hDLL = 0;
}
return TRUE;
}
/// <summary>
/// Processes the specified item.
/// </summary>
static void ProcessItem(const wstring & pathName)
{
_Console.Clear();
_Console.SetDefaultTextColor(_Console.DarkMode() ? RGB(220, 220, 220) : ::GetSysColor(COLOR_WINDOWTEXT));
DWORD FileAttributes = ::GetFileAttributesW(pathName.c_str());
if (FileAttributes == INVALID_FILE_ATTRIBUTES)
{
ShowError(L"Unable to get file attributes. GetFileAttributesW() failed.");
return;
}
ObjectType ObjectType = ((FileAttributes & FILE_ATTRIBUTE_DIRECTORY) && !(FileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)) ? DirectoryObject : FileObject;
_Console.SetBold(true);
_Console.Write(L"Item: \"%s\"\n", pathName.c_str());
_Console.SetBold(false);
// Low-level Security Descriptor Functions, https://msdn.microsoft.com/en-us/library/aa379204(v=vs.85).aspx
const SECURITY_INFORMATION RequestedInformation = OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION;
DWORD sdSize = 0;
if (::GetFileSecurityW(pathName.c_str(), RequestedInformation, NULL, 0, &sdSize) && (::GetLastError() != ERROR_INSUFFICIENT_BUFFER))
{
ShowError(L"Unable to get security information. GetFileSecurityW() failed.");
return;
}
PSECURITY_DESCRIPTOR sd = (PSECURITY_DESCRIPTOR) ::LocalAlloc(LMEM_FIXED, sdSize);
if (sd == nullptr)
{
ShowError(L"Unable to allocated memory for security descriptor. LocalAlloc() failed.", ::GetLastError());
return;
}
if (::GetFileSecurityW(pathName.c_str(), RequestedInformation, sd, sdSize, &sdSize)) // Requires READ_CONTROL right.
{
{
_Console.SetBold(true);
_Console.Write(L"\nSecurity Descriptor:\n");
_Console.SetBold(false);
ShowSecurityDescriptor(sd);
}
{
_Console.SetBold(true);
_Console.Write(L"\nOwner:\n");
_Console.SetBold(false);
PSID OwnerSID;
BOOL IsOwnerDefaulted;
if (::GetSecurityDescriptorOwner(sd, &OwnerSID, &IsOwnerDefaulted))
ShowSID(OwnerSID);
else
ShowError(L"Unable to determine owner. GetSecurityDescriptorOwner() failed.");
}
{
_Console.SetBold(true);
_Console.Write(L"\nPrimary Group:\n");
_Console.SetBold(false);
PSID GroupSID;
BOOL IsGroupDefaulted;
if (::GetSecurityDescriptorGroup(sd, &GroupSID, &IsGroupDefaulted))
ShowSID(GroupSID);
else
ShowError(L"Unable to determine primary group. GetSecurityDescriptorGroup() failed.");
}
{
_Console.SetBold(true);
_Console.Write(L"\nDACL:\n");
_Console.SetBold(false);
PACL DACL;
BOOL IsDACLPresent;
BOOL IsDACLDefaulted;
if (::GetSecurityDescriptorDacl(sd, &IsDACLPresent, &DACL, &IsDACLDefaulted))
{
if (IsDACLPresent && ::IsValidAcl(DACL))
ShowACL(DACL, ObjectType);
}
else
ShowError(L"Unable to get DACL. GetSecurityDescriptorDacl() failed.");
}
}
else
ShowError(L"Unable to get security information. GetFileSecurityW() failed.");
if (sd)
::LocalFree(sd);
// Highlight any quoted text.
{
_Console.SetDefaultTextColor(::GetSysColor(COLOR_HIGHLIGHT));
FINDTEXTW ft = { { 0, (LONG) ::SendMessageW(_Console.Handle(), WM_GETTEXTLENGTH, 0, 0) }, L"\"" };
int OpeningQuote = (int) ::SendMessageW(_Console.Handle(), EM_FINDTEXT, (WPARAM) FR_DOWN, (LPARAM) &ft);
while (OpeningQuote != -1)
{
ft.chrg.cpMin = ++OpeningQuote;
int ClosingQuote = (int) ::SendMessageW(_Console.Handle(), EM_FINDTEXT, (WPARAM) FR_DOWN, (LPARAM) &ft);
if (ClosingQuote == -1)
break;
_Console.Select(OpeningQuote, ClosingQuote);
_Console.SetFormat();
ft.chrg.cpMin = ClosingQuote + 1;
OpeningQuote = (int) ::SendMessageW(_Console.Handle(), EM_FINDTEXT, (WPARAM) FR_DOWN, (LPARAM) &ft);
}
}
_Console.ScrollToTop();
_Console.Select(0, 0);
}