-
Notifications
You must be signed in to change notification settings - Fork 194
/
CHashCheck.cpp
321 lines (262 loc) · 8.38 KB
/
CHashCheck.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
/**
* HashCheck Shell Extension
* Copyright (C) Kai Liu. All rights reserved.
*
* Please refer to readme.txt for information about this source code.
* Please refer to license.txt for details about distribution and modification.
**/
#include "CHashCheck.hpp"
#include "HashCheckUI.h"
#include "HashCheckOptions.h"
CHashCheck::CHashCheck( )
{
InterlockedIncrement(&g_cRefThisDll);
m_cRef = 1;
m_hList = NULL;
m_hMenuBitmap = g_uWinVer >= 0x0600 ? // Vista+
(HBITMAP)LoadImage(g_hModThisDll, MAKEINTRESOURCE(IDI_MENUBITMAP), IMAGE_BITMAP, 0, 0, LR_DEFAULTSIZE | LR_CREATEDIBSECTION) :
NULL;
}
STDMETHODIMP CHashCheck::QueryInterface( REFIID riid, LPVOID *ppv )
{
if (IsEqualIID(riid, IID_IUnknown))
{
*ppv = this;
}
else if (IsEqualIID(riid, IID_IShellExtInit))
{
*ppv = (LPSHELLEXTINIT)this;
}
else if (IsEqualIID(riid, IID_IContextMenu))
{
*ppv = (LPCONTEXTMENU)this;
}
else if (IsEqualIID(riid, IID_IShellPropSheetExt))
{
*ppv = (LPSHELLPROPSHEETEXT)this;
}
else if (IsEqualIID(riid, IID_IDropTarget))
{
*ppv = (LPDROPTARGET)this;
}
else
{
*ppv = NULL;
return(E_NOINTERFACE);
}
AddRef();
return(S_OK);
}
STDMETHODIMP CHashCheck::Initialize( LPCITEMIDLIST pidlFolder, LPDATAOBJECT pdtobj, HKEY hkeyProgID )
{
// We'll be needing a buffer, and let's double it just to be safe
TCHAR szPath[MAX_PATH << 1];
// Make sure that we are working with a fresh list
SLRelease(m_hList);
m_hList = SLCreate();
// This indent exists to facilitate diffing against the CmdOpen source
{
FORMATETC format = { CF_HDROP, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
STGMEDIUM medium;
if (!pdtobj || pdtobj->GetData(&format, &medium) != S_OK)
return(E_INVALIDARG);
if (HDROP hDrop = (HDROP)GlobalLock(medium.hGlobal))
{
UINT uDrops = DragQueryFile(hDrop, -1, NULL, 0);
for (UINT uDrop = 0; uDrop < uDrops; ++uDrop)
{
if (DragQueryFile(hDrop, uDrop, szPath, countof(szPath)))
{
SLAddStringI(m_hList, szPath);
}
}
GlobalUnlock(medium.hGlobal);
}
ReleaseStgMedium(&medium);
}
// If there was any failure, the list would be empty...
return((SLCheck(m_hList)) ? S_OK : E_INVALIDARG);
}
STDMETHODIMP CHashCheck::QueryContextMenu( HMENU hmenu, UINT indexMenu, UINT idCmdFirst, UINT idCmdLast, UINT uFlags )
{
if (uFlags & (CMF_DEFAULTONLY | CMF_NOVERBS))
return(MAKE_HRESULT(SEVERITY_SUCCESS, FACILITY_NULL, 0));
// Ugly hack: work around a bug in Windows 5.x that causes a spurious
// separator to be added when invoking the context menu from the Start Menu
if (g_uWinVer < 0x0600 && !(uFlags & (0x20000 | CMF_EXPLORE)) && GetModuleHandleA("explorer.exe"))
return(MAKE_HRESULT(SEVERITY_SUCCESS, FACILITY_NULL, 0));
// Load the menu display settings
HASHCHECKOPTIONS opt;
opt.dwFlags = HCOF_MENUDISPLAY;
OptionsLoad(&opt);
// Do not show if the settings prohibit it
if (opt.dwMenuDisplay == 2 || (opt.dwMenuDisplay == 1 && !(uFlags & CMF_EXTENDEDVERBS)))
return(MAKE_HRESULT(SEVERITY_SUCCESS, FACILITY_NULL, 0));
if (! InsertMenu(hmenu, indexMenu, MF_SEPARATOR | MF_BYPOSITION, 0, NULL))
return(MAKE_HRESULT(SEVERITY_SUCCESS, FACILITY_NULL, 0));
// Load the localized menu text
TCHAR szMenuText[MAX_STRINGMSG];
LoadString(g_hModThisDll, IDS_HS_MENUTEXT, szMenuText, countof(szMenuText));
MENUITEMINFO mii;
mii.cbSize = sizeof(mii);
mii.fMask = MIIM_FTYPE | MIIM_ID | MIIM_STRING;
if (g_uWinVer >= 0x0600) // prior to Vista, 32-bit bitmaps w/alpha channels don't render correctly in menus
mii.fMask |= MIIM_BITMAP;
mii.fType = MFT_STRING;
mii.wID = idCmdFirst;
mii.dwTypeData = szMenuText;
mii.hbmpItem = m_hMenuBitmap;
if (! InsertMenuItem(hmenu, indexMenu + 1, TRUE, &mii))
return(MAKE_HRESULT(SEVERITY_SUCCESS, FACILITY_NULL, 0));
InsertMenu(hmenu, indexMenu + 2, MF_SEPARATOR | MF_BYPOSITION, 0, NULL);
return(MAKE_HRESULT(SEVERITY_SUCCESS, FACILITY_NULL, 1));
}
STDMETHODIMP CHashCheck::InvokeCommand( LPCMINVOKECOMMANDINFO pici )
{
// Ignore string verbs (high word must be zero)
// The only valid command index is 0 (low word must be zero)
if (pici->lpVerb)
return(E_INVALIDARG);
// Hand things over to HashSave, where all the work is done...
HashSaveStart(pici->hwnd, m_hList);
// HaveSave has AddRef'ed and now owns our list
SLRelease(m_hList);
m_hList = NULL;
return(S_OK);
}
STDMETHODIMP CHashCheck::GetCommandString( UINT_PTR idCmd, UINT uFlags, UINT *pwReserved, LPSTR pszName, UINT cchMax )
{
static const CHAR szVerbA[] = "cksum";
static const WCHAR szVerbW[] = L"cksum";
if (idCmd != 0 || cchMax < countof(szVerbW))
return(E_INVALIDARG);
switch (uFlags)
{
// The help text (status bar text) should not contain any of the
// characters added for the menu access keys.
case GCS_HELPTEXTA:
{
LoadStringA(g_hModThisDll, IDS_HS_MENUTEXT, (LPSTR)pszName, cchMax);
LPSTR lpszSrcA = (LPSTR)pszName;
LPSTR lpszDestA = (LPSTR)pszName;
while (*lpszSrcA && *lpszSrcA != '(' && *lpszSrcA != '.')
{
if (*lpszSrcA != '&')
{
*lpszDestA = *lpszSrcA;
++lpszDestA;
}
++lpszSrcA;
}
*lpszDestA = 0;
return(S_OK);
}
case GCS_HELPTEXTW:
{
LoadStringW(g_hModThisDll, IDS_HS_MENUTEXT, (LPWSTR)pszName, cchMax);
LPWSTR lpszSrcW = (LPWSTR)pszName;
LPWSTR lpszDestW = (LPWSTR)pszName;
while (*lpszSrcW && *lpszSrcW != L'(' && *lpszSrcW != L'.')
{
if (*lpszSrcW != L'&')
{
*lpszDestW = *lpszSrcW;
++lpszDestW;
}
++lpszSrcW;
}
*lpszDestW = 0;
return(S_OK);
}
case GCS_VERBA:
{
SSStaticCpyA((LPSTR)pszName, szVerbA);
return(S_OK);
}
case GCS_VERBW:
{
SSStaticCpyW((LPWSTR)pszName, szVerbW);
return(S_OK);
}
}
return(E_INVALIDARG);
}
STDMETHODIMP CHashCheck::AddPages( LPFNADDPROPSHEETPAGE pfnAddPage, LPARAM lParam )
{
PROPSHEETPAGE psp;
psp.dwSize = sizeof(psp);
psp.dwFlags = PSP_USECALLBACK | PSP_USEREFPARENT | PSP_USETITLE;
psp.hInstance = g_hModThisDll;
psp.pszTemplate = MAKEINTRESOURCE(IDD_HASHPROP);
psp.pszTitle = MAKEINTRESOURCE(IDS_HP_TITLE);
psp.pfnDlgProc = HashPropDlgProc;
psp.lParam = (LPARAM)m_hList;
psp.pfnCallback = HashPropCallback;
psp.pcRefParent = (PUINT)&g_cRefThisDll;
if (ActivateManifest(FALSE))
{
psp.dwFlags |= PSP_USEFUSIONCONTEXT;
psp.hActCtx = g_hActCtx;
}
HPROPSHEETPAGE hPage = CreatePropertySheetPage(&psp);
if (hPage && !pfnAddPage(hPage, lParam))
DestroyPropertySheetPage(hPage);
// HashProp has AddRef'ed and now owns our list
SLRelease(m_hList);
m_hList = NULL;
return(S_OK);
}
STDMETHODIMP CHashCheck::Drop( LPDATAOBJECT pdtobj, DWORD grfKeyState, POINTL pt, PDWORD pdwEffect )
{
FORMATETC format = { CF_HDROP, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
STGMEDIUM medium;
UINT uThreads = 0;
if (pdtobj && pdtobj->GetData(&format, &medium) == S_OK)
{
if (HDROP hDrop = (HDROP)GlobalLock(medium.hGlobal))
{
UINT uDrops = DragQueryFile(hDrop, -1, NULL, 0);
UINT cchPath;
LPTSTR lpszPath;
// Reduce the likelihood of a race condition when trying to create
// an activation context by creating it before creating threads
ActivateManifest(FALSE);
for (UINT uDrop = 0; uDrop < uDrops; ++uDrop)
{
if ( (cchPath = DragQueryFile(hDrop, uDrop, NULL, 0)) &&
(lpszPath = (LPTSTR)malloc((cchPath + 1) * sizeof(TCHAR))) )
{
InterlockedIncrement(&g_cRefThisDll);
HANDLE hThread;
if ( (DragQueryFile(hDrop, uDrop, lpszPath, cchPath + 1) == cchPath) &&
(!(GetFileAttributes(lpszPath) & FILE_ATTRIBUTE_DIRECTORY)) &&
(hThread = CreateThreadCRT(HashVerifyThread, lpszPath)) )
{
// The thread should free lpszPath, not us
CloseHandle(hThread);
++uThreads;
}
else
{
free(lpszPath);
InterlockedDecrement(&g_cRefThisDll);
}
}
}
GlobalUnlock(medium.hGlobal);
}
ReleaseStgMedium(&medium);
}
if (uThreads)
{
// DROPEFFECT_LINK would work here as well; it really doesn't matter
*pdwEffect = DROPEFFECT_COPY;
return(S_OK);
}
else
{
// We shouldn't ever be hitting this case
*pdwEffect = DROPEFFECT_NONE;
return(E_INVALIDARG);
}
}