-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExplorerExtension.cpp
281 lines (249 loc) · 7.02 KB
/
ExplorerExtension.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
#include "Dll.h"
#include "ExplorerExtension.h"
#include "FileItem.h"
#include "FileProcessor.h"
#include <list>
#include <cstdlib>
#include <fstream>
#include <codecvt>
static WCHAR const verbDisplayName[] = L"Calculate the Sum";
static WCHAR const c_szVerbName[] = L"Task.CalcTheSum";
ExplorerExtension::ExplorerExtension() : m_refCount(1), m_site(NULL), _hwnd(NULL), m_shellItemArray(NULL)
{
DllAddRef();
}
IFACEMETHODIMP ExplorerExtension::QueryInterface(REFIID riid, void **ppv)
{
static const QITAB qit[] =
{
QITABENT(ExplorerExtension, IExplorerCommand),
QITABENT(ExplorerExtension, IInitializeCommand),
QITABENT(ExplorerExtension, IObjectWithSite),
{ 0 },
};
return QISearch(this, qit, riid, ppv);
}
// Reference counting: increase reference count
IFACEMETHODIMP_(ULONG) ExplorerExtension::AddRef()
{
return ++m_refCount;
}
// Reference counting: decrease reference count
IFACEMETHODIMP_(ULONG) ExplorerExtension::Release()
{
long mRef = --m_refCount;
if (!mRef)
{
delete this;
}
return mRef;
}
// IExplorerCommand
// Get the title under which the item will be displayed in context menu
IFACEMETHODIMP ExplorerExtension::GetTitle(IShellItemArray * /* psiItemArray */, LPWSTR *ppszName)
{
return SHStrDup(verbDisplayName, ppszName);
}
// Get the icon. In our case, menu item has no icon
IFACEMETHODIMP ExplorerExtension::GetIcon(IShellItemArray * /* psiItemArray */, LPWSTR *ppszIcon)
{
*ppszIcon = NULL;
return E_NOTIMPL;
}
// Get tool tip. In our case, menu item has no tooltip
IFACEMETHODIMP ExplorerExtension::GetToolTip(IShellItemArray * /* psiItemArray */, LPWSTR *ppszInfotip)
{
// No tooltip
*ppszInfotip = NULL;
return E_NOTIMPL;
}
IFACEMETHODIMP ExplorerExtension::GetCanonicalName(GUID* pguidCommandName)
{
*pguidCommandName = __uuidof(this);
return S_OK;
}
// Get State (in our case, always enabled)
IFACEMETHODIMP ExplorerExtension::GetState(IShellItemArray * /* psiItemArray */, BOOL /*fOkToBeSlow*/, EXPCMDSTATE *pCmdState)
{
*pCmdState = ECS_ENABLED;
return S_OK;
}
// Get Flags (in our case, default)
IFACEMETHODIMP ExplorerExtension::GetFlags(EXPCMDFLAGS *pFlags)
{
*pFlags = ECF_DEFAULT;
return S_OK;
}
// Get subcommands (in our case, command has no subcommands
IFACEMETHODIMP ExplorerExtension::EnumSubCommands(IEnumExplorerCommand **ppEnum)
{
*ppEnum = NULL;
return E_NOTIMPL;
}
// IInitializeCommand
// This method is required, but in our case, does nothing
IFACEMETHODIMP ExplorerExtension::Initialize(PCWSTR, IPropertyBag *)
{
return S_OK;
}
// IObjectWithSite
IFACEMETHODIMP ExplorerExtension::SetSite(IUnknown *punkSite)
{
SetInterface(&m_site, punkSite);
return S_OK;
}
IFACEMETHODIMP ExplorerExtension::GetSite(REFIID riid, void **ppv)
{
*ppv = NULL;
return m_site ? m_site->QueryInterface(riid, ppv) : E_FAIL;
}
int64_t getFileSize(const wchar_t * name)
{
HANDLE hFile = CreateFileW(name, GENERIC_READ, FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE) return -1;
LARGE_INTEGER result;
BOOL succeed = GetFileSizeEx(hFile, &result);
CloseHandle(hFile);
if (succeed)
return result.QuadPart;
else
return -1;
}
DWORD ExplorerExtension::_ThreadProc()
{
using namespace std;
using namespace boost::posix_time;
using namespace boost::gregorian;
srand(static_cast<unsigned>(time(0)));
wstring file_name = OrderedLogger::generateLogName();
while (PathFileExists(file_name.c_str()))
file_name = OrderedLogger::generateLogName();
MessageBoxW(_hwnd, (L"Processing started. Result will be placed in the file \"" + file_name + L"\" in your user profile directory").c_str(), L"Calculate the Sum", MB_OK);
IShellItemArray *psia;
HRESULT hr = CoGetInterfaceAndReleaseStream(m_shellItemArray, IID_PPV_ARGS(&psia)); // Unmarshall data from Stream
m_shellItemArray = NULL;
if (SUCCEEDED(hr))
{
DWORD count;
psia->GetCount(&count);
list<FileItem> items;
IShellItem2 *psi;
for (DWORD i = 0; i < count && SUCCEEDED(hr); i++)
{
hr = GetItemAt(psia, i, IID_PPV_ARGS(&psi));
if (SUCCEEDED(hr))
{
PWSTR shortName;
hr = psi->GetDisplayName(SIGDN_PARENTRELATIVEPARSING, &shortName);
if (SUCCEEDED(hr))
{
PWSTR fullName;
hr = psi->GetDisplayName(SIGDN_FILESYSPATH, &fullName);
if (SUCCEEDED(hr))
{
int64_t size = getFileSize(fullName);
if (size != -1)
{
FILETIME ft;
hr = psi->GetFileTime(PKEY_DateCreated, &ft);
if (SUCCEEDED(hr))
{
SYSTEMTIME st;
if (FileTimeToSystemTime(&ft, &st))
{
items.push_back({ shortName, fullName, size,
ptime(date(st.wYear, st.wMonth, st.wDay),
time_duration(st.wHour, st.wMinute, st.wSecond)) });
}
else
hr = E_FAIL;
}
}
else
hr = E_FAIL;
CoTaskMemFree(fullName);
}
CoTaskMemFree(shortName);
}
psi->Release();
}
}
if (SUCCEEDED(hr))
{
wstring profile = _wgetenv(L"userprofile");
if (profile[profile.size() - 1] != L'\\')
profile += L'\\';
wofstream logfile(profile + file_name);
logfile.imbue(std::locale(logfile.getloc(), new std::codecvt_utf8_utf16<wchar_t>));
FileProcessor processor(logfile, true);
processor.processFileList(items);
}
psia->Release();
}
return 0;
}
IFACEMETHODIMP ExplorerExtension::Invoke(IShellItemArray *psia, IBindCtx * /* pbc */)
{
IUnknown_GetWindow(m_site, &_hwnd);
HRESULT hr = CoMarshalInterThreadInterfaceInStream(__uuidof(psia), psia, &m_shellItemArray);
if (SUCCEEDED(hr))
{
AddRef();
if (!SHCreateThread(s_ThreadProc, this, CTF_COINIT_STA | CTF_PROCESS_REF, NULL))
{
Release();
}
}
return S_OK;
}
static WCHAR const g_progId[] = L"*";
HRESULT ExplorerExtension::RegisterUnRegister(bool fRegister)
{
RegisterExtension re(__uuidof(ExplorerExtension));
HRESULT hr;
if (fRegister)
{
hr = re.RegisterInProcServer(verbDisplayName, L"Apartment");
if (SUCCEEDED(hr))
{
hr = re.RegisterExplorerCommandVerb(g_progId, c_szVerbName, verbDisplayName);
if (SUCCEEDED(hr))
{
hr = re.RegisterVerbAttribute(g_progId, c_szVerbName, L"NeverDefault");
}
}
}
else
{
// best effort
hr = re.UnRegisterVerb(g_progId, c_szVerbName);
hr = re.UnRegisterObject();
}
return hr;
}
HRESULT ExplorerExtension::CreateInstance(REFIID riid, void **ppv)
{
*ppv = NULL;
ExplorerExtension *pVerb = new (std::nothrow) ExplorerExtension();
HRESULT hr = pVerb ? S_OK : E_OUTOFMEMORY;
if (SUCCEEDED(hr))
{
pVerb->QueryInterface(riid, ppv);
pVerb->Release();
}
return hr;
}
ExplorerExtension::~ExplorerExtension()
{
SafeRelease(&m_site);
SafeRelease(&m_shellItemArray);
DllRelease();
}
DWORD __stdcall ExplorerExtension::s_ThreadProc(void *pv)
{
ExplorerExtension *pecv = reinterpret_cast<ExplorerExtension *>(pv);
const DWORD ret = pecv->_ThreadProc();
pecv->Release();
return ret;
}