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
|
// DropDlg.cpp : Implementation of CDropDlg
#include "stdafx.h"
#include "DropDlg.h"
/////////////////////////////////////////////////////////////////////////////
// CDropDlg
const TCHAR* kLastBatch = _T("Batch");
CDropDlg::CDropDlg() :
m_settings(HKEY_CURRENT_USER, _T("SOFTWARE\\WS\\Dropper\\")),
CPersistPosWindow<CDropDlg>("Window")
{
m_isRunning = false;
m_isCancelled = false;
}
void AddBatches(const string& wildcard, CComboBox& ctlBatch)
{
WIN32_FIND_DATA findData;
HANDLE hFindFile = FindFirstFile(wildcard, &findData);
if(hFindFile != INVALID_HANDLE_VALUE)
{
do
{
// Get first part of the file
string name = findData.cFileName;
name = name.substr(0, name.find_last_of('.'));
ctlBatch.AddString(name);
}
while(FindNextFile(hFindFile, &findData));
}
FindClose(hFindFile);
}
LRESULT CDropDlg::OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
m_ctlLog = GetDlgItem(IDC_LOG);
m_baseDir = GetModuleFolder(_Module.GetModuleInstance());
CleanFolder(m_baseDir);
CComboBox ctlBatch = GetDlgItem(IDC_BATCH);
AddBatches(m_baseDir + _T("*.cmd"), ctlBatch);
AddBatches(m_baseDir + _T("*.bat"), ctlBatch);
SetMin(IDC_MINSIZE);
SetControlSizing(IDC_LOG, SD_HORZ | SD_VERT | SD_SIZING);
SetControlSizing(IDC_RUN, SD_HORZ | SD_MOVING);
SetControlSizing(IDC_BATCH, SD_HORZ | SD_SIZING);
// Select last used batch
ctlBatch.SelectString(-1, m_settings.GetString(kLastBatch));
SetRunMode(false);
// Center it
CenterWindow();
LoadPosition(m_settings);
SetIcon((HICON)LoadImage(_Module.GetResourceInstance(), MAKEINTRESOURCE(IDI_MAIN), IMAGE_ICON, 32, 32, LR_SHARED), TRUE);
SetIcon((HICON)LoadImage(_Module.GetResourceInstance(), MAKEINTRESOURCE(IDI_MAIN), IMAGE_ICON, 16, 16, LR_SHARED), FALSE);
return 1; // Let the system set the focus
}
LRESULT CDropDlg::OnDestroy(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
SavePosition(m_settings);
return 1;
}
void CDropDlg::SetRunMode(bool isRunning)
{
m_isRunning = isRunning;
if(isRunning)
m_isCancelled = false;
DragAcceptFiles(isRunning ? FALSE : TRUE);
::EnableWindow(GetDlgItem(IDC_BATCH), isRunning ? FALSE : TRUE);
::EnableWindow(GetDlgItem(IDC_RUN), TRUE);
SetDlgItemText(IDC_RUN, isRunning ? _T("Cancel") : _T("Run"));
}
void CDropDlg::ReportError(HRESULT hr)
{
MessageBox(FormatHR(hr), _T("Dropper"), MB_ICONSTOP | MB_OK);
}
string CDropDlg::CleanFolder(const string& folderName)
{
if(folderName.at(folderName.length() - 1) != _T('\\'))
return folderName + _T("\\");
return folderName;
}
bool CDropDlg::IsDots(const string& fileName)
{
return fileName == _T(".") || fileName == _T("..");
}
LRESULT CDropDlg::OnDropFiles(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
HDROP hDrop = (HDROP)wParam;
HRESULT hr = S_OK;
string fileName;
string batchName = GetCurBatch();
SetRunMode(true);
SetDlgItemText(IDC_LOG, _T(""));
UINT numFiles = DragQueryFile(hDrop, 0xFFFFFFFF, NULL, 0);
for(int i = 0; i < numFiles; i++)
{
if(DragQueryFile(hDrop, i, fileName.get_buffer(MAX_PATH), MAX_PATH))
{
fileName.release_buffer();
hr = RunWithFile(batchName, fileName);
if(FAILED(hr))
{
ReportError(hr);
break;
}
}
}
DragFinish(hDrop);
SetRunMode(false);
return 0;
}
LRESULT CDropDlg::OnClose(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
if(!m_isRunning)
{
string batchName;
GetDlgItemText(IDC_BATCH, batchName.get_buffer(MAX_PATH), MAX_PATH);
batchName.release_buffer();
m_settings.WriteString(kLastBatch, batchName);
EndDialog(wID);
}
else
{
m_isCancelled = true;
}
return 0;
}
LRESULT CDropDlg::OnRun(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
if(m_isRunning)
{
// It's a cancel button now
m_isCancelled = true;
::EnableWindow(GetDlgItem(IDC_RUN), FALSE);
}
else
{
// It's a run button
SetRunMode(true);
SetDlgItemText(IDC_LOG, _T(""));
HRESULT hr = RunWithFile(GetCurBatch(), _T(""));
if(FAILED(hr))
ReportError(hr);
SetRunMode(false);
}
return 0;
}
string CDropDlg::GetCurBatch()
{
string sBatch;
GetDlgItemText(IDC_BATCH, sBatch.get_buffer(MAX_PATH), MAX_PATH);
sBatch.release_buffer();
return sBatch;
}
HRESULT CDropDlg::RunWithFolder(const string& batch, const string& folderName)
{
string wildcard = folderName + _T("*.*");
WIN32_FIND_DATA findData;
HRESULT hr = S_OK;
HANDLE hFindFile = FindFirstFile(wildcard, &findData);
if(hFindFile != INVALID_HANDLE_VALUE)
{
do
{
if(!IsDots(findData.cFileName))
{
string fileName = folderName + findData.cFileName;
hr = RunWithFile(batch, fileName);
if(FAILED(hr)) return hr;
}
if(m_isCancelled)
{
::SetLastError(ERROR_SUCCESS);
break;
}
}
while(FindNextFile(hFindFile, &findData));
}
if(GetLastError() != 0 && GetLastError() != ERROR_NO_MORE_FILES)
return HRESULT_FROM_WIN32(::GetLastError());
return S_OK;
}
HRESULT CDropDlg::RunWithFile(const string& batch, const string& path)
{
if(path.size() > 0)
{
DWORD fileAttrib = GetFileAttributes(path);
if(fileAttrib & FILE_ATTRIBUTE_DIRECTORY)
{
return RunWithFolder(batch, CleanFolder(path));
}
}
string cmdLine = _T("\"") + m_baseDir + batch +
_T(".bat\"");
HRESULT hr = S_OK;
if(path.size() > 0)
{
cmdLine += _T(" \"") + path + _T("\"");
size_t pos = path.find_last_of(_T("\\/"));
string file, dir;
if(pos != string::npos)
{
pos++;
file = path.substr(pos);
dir = path.substr(0, pos);
}
else
{
file = path;
}
SetEnvironmentVariable(_T("DROPPED_FILE"), file);
SetEnvironmentVariable(_T("DROPPED_DIR"), dir);
SetEnvironmentVariable(_T("DROPPED_PATH"), path);
}
if(!CreateProcessOutput(NULL, (LPTSTR)(LPCTSTR)cmdLine, NULL, NULL, TRUE,
0, NULL, m_baseDir, OutputString, (LPVOID)this))
{
hr = HRESULT_FROM_WIN32(::GetLastError());
}
// Do an extra line
OutputString(_T("\r\n"), (LPVOID)this);
return S_OK;
}
BOOL CDropDlg::OutputString(LPCTSTR data, LPVOID param)
{
CDropDlg* pDlg = (CDropDlg*)param;
pDlg->m_ctlLog.AppendText(data);
// TODO: Provide some way to cancel
return TRUE;
}
|