-
Notifications
You must be signed in to change notification settings - Fork 2
/
MainDlg.cpp
356 lines (291 loc) · 12 KB
/
MainDlg.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
#include "stdafx.h"
#include "MainDlg.h"
#include "symbol_enum.h"
#include "symbols_from_binary.h"
#define WSH_VERSION L"1.0.5"
namespace {
struct InitialUIValues {
CString engineDir;
CString symbolsDir;
CString symbolServer;
CString targetExecutable;
};
InitialUIValues GetInitialUIValues() {
InitialUIValues values;
std::filesystem::path iniFilePath = wil::GetModuleFileName<std::wstring>();
iniFilePath.replace_filename(L"windhawk-symbol-helper.ini");
std::filesystem::path fallbackIniFilePath1 = iniFilePath;
fallbackIniFilePath1.replace_filename(L"windhawk.ini");
std::filesystem::path fallbackIniFilePath2 =
wil::ExpandEnvironmentStrings<std::wstring>(
LR"(%ProgramFiles%\Windhawk\windhawk.ini)");
WCHAR buffer[MAX_PATH];
constexpr DWORD bufferSize = static_cast<DWORD>(std::size(buffer));
if (GetPrivateProfileString(L"Config", L"EnginePath", L"", buffer,
bufferSize, iniFilePath.c_str())) {
values.engineDir =
wil::ExpandEnvironmentStrings<std::wstring>(buffer).c_str();
} else if (GetPrivateProfileString(L"Storage", L"EnginePath", L"", buffer,
bufferSize,
fallbackIniFilePath1.c_str())) {
auto expanded = wil::ExpandEnvironmentStrings<std::wstring>(buffer);
values.engineDir =
(fallbackIniFilePath1.parent_path() / expanded).c_str();
} else if (GetPrivateProfileString(L"Storage", L"EnginePath", L"", buffer,
bufferSize,
fallbackIniFilePath2.c_str())) {
auto expanded = wil::ExpandEnvironmentStrings<std::wstring>(buffer);
values.engineDir =
(fallbackIniFilePath2.parent_path() / expanded).c_str();
} else {
values.engineDir = LR"(C:\Program Files\Windhawk\Engine\1.4.1)";
}
if (GetPrivateProfileString(L"Config", L"SymbolsPath", L"", buffer,
bufferSize, iniFilePath.c_str())) {
values.symbolsDir =
wil::ExpandEnvironmentStrings<std::wstring>(buffer).c_str();
} else if (GetPrivateProfileString(L"Storage", L"AppDataPath", L"", buffer,
bufferSize,
fallbackIniFilePath1.c_str())) {
auto expanded = wil::ExpandEnvironmentStrings<std::wstring>(buffer);
values.symbolsDir = (fallbackIniFilePath1.parent_path() / expanded /
L"Engine" / L"Symbols")
.c_str();
} else if (GetPrivateProfileString(L"Storage", L"AppDataPath", L"", buffer,
bufferSize,
fallbackIniFilePath2.c_str())) {
auto expanded = wil::ExpandEnvironmentStrings<std::wstring>(buffer);
values.symbolsDir = (fallbackIniFilePath2.parent_path() / expanded /
L"Engine" / L"Symbols")
.c_str();
} else {
values.symbolsDir = LR"(C:\ProgramData\Windhawk\Engine\Symbols)";
}
if (GetPrivateProfileString(L"Config", L"SymbolServer", L"", buffer,
bufferSize, iniFilePath.c_str())) {
values.symbolServer = buffer;
} else {
values.symbolServer = L"https://msdl.microsoft.com/download/symbols";
}
if (GetPrivateProfileString(L"Config", L"TargetExecutable", L"", buffer,
bufferSize, iniFilePath.c_str())) {
values.targetExecutable =
wil::ExpandEnvironmentStrings<std::wstring>(buffer).c_str();
} else {
values.targetExecutable = LR"(C:\Windows\Explorer.exe)";
}
return values;
}
void OpenUrl(HWND hWnd, PCWSTR url) {
if ((INT_PTR)ShellExecute(hWnd, L"open", url, nullptr, nullptr,
SW_SHOWNORMAL) <= 32) {
CString errorMsg;
errorMsg.Format(
L"Failed to open link, you can copy it with Ctrl+C and open it in "
L"a browser manually:\n\n%s",
url);
MessageBox(hWnd, errorMsg, nullptr, MB_ICONHAND);
}
}
} // namespace
BOOL CMainDlg::PreTranslateMessage(MSG* pMsg) {
if (m_resultsEdit.PreTranslateMessage(pMsg)) {
return TRUE;
}
if (m_accelerator && ::TranslateAccelerator(m_hWnd, m_accelerator, pMsg)) {
return TRUE;
}
return CWindow::IsDialogMessage(pMsg);
}
BOOL CMainDlg::OnInitDialog(CWindow wndFocus, LPARAM lInitParam) {
// Center the dialog on the screen.
CenterWindow();
// Set icons.
HICON hIcon = AtlLoadIconImage(IDR_MAINFRAME, LR_DEFAULTCOLOR,
::GetSystemMetrics(SM_CXICON),
::GetSystemMetrics(SM_CYICON));
SetIcon(hIcon, TRUE);
HICON hIconSmall = AtlLoadIconImage(IDR_MAINFRAME, LR_DEFAULTCOLOR,
::GetSystemMetrics(SM_CXSMICON),
::GetSystemMetrics(SM_CYSMICON));
SetIcon(hIconSmall, FALSE);
// Bind keys...
m_accelerator = AtlLoadAccelerators(IDR_MAINFRAME);
// Register object for message filtering and idle updates.
CMessageLoop* pLoop = _Module.GetMessageLoop();
ATLASSERT(pLoop != nullptr);
pLoop->AddMessageFilter(this);
// Populate values.
auto initialUIValues = GetInitialUIValues();
CEdit(GetDlgItem(IDC_ENGINE_DIR)).SetWindowText(initialUIValues.engineDir);
CEdit(GetDlgItem(IDC_SYMBOLS_DIR))
.SetWindowText(initialUIValues.symbolsDir);
CEdit(GetDlgItem(IDC_SYMBOL_SERVER))
.SetWindowText(initialUIValues.symbolServer);
CEdit(GetDlgItem(IDC_TARGET_EXECUTABLE))
.SetWindowText(initialUIValues.targetExecutable);
CButton(GetDlgItem(IDC_UNDECORATED)).SetCheck(BST_CHECKED);
// Init edit control.
auto resultsPlaceholderControl = GetDlgItem(IDC_RESULTS_PLACEHOLDER);
CRect rc;
resultsPlaceholderControl.GetClientRect(&rc);
resultsPlaceholderControl.MapWindowPoints(m_hWnd, rc);
m_resultsEdit.Create(m_hWnd, rc, nullptr,
WS_CHILD | WS_VISIBLE | WS_TABSTOP | ES_MULTILINE |
ES_AUTOVSCROLL | ES_AUTOHSCROLL | ES_WANTRETURN |
ES_NOHIDESEL | WS_VSCROLL | WS_HSCROLL,
WS_EX_CLIENTEDGE, IDC_RESULTS);
CLogFont fontAttributes(AtlGetDefaultGuiFont());
wcscpy_s(fontAttributes.lfFaceName, L"Consolas");
m_resultsEditFont = fontAttributes.CreateFontIndirect();
m_resultsEdit.SetFont(m_resultsEditFont);
// Init resizing.
DlgResize_Init();
return TRUE;
}
void CMainDlg::OnDestroy() {
m_enumSymbolsThread.reset();
PostQuitMessage(0);
}
void CMainDlg::OnDropFiles(HDROP hDropInfo) {
if (DragQueryFile(hDropInfo, 0xFFFFFFFF, NULL, 0) == 1) {
WCHAR fileName[MAX_PATH];
DragQueryFile(hDropInfo, 0, fileName, MAX_PATH);
SetDlgItemText(IDC_TARGET_EXECUTABLE, fileName);
} else {
MessageBox(L"Please drop one file at a time", L"Unsupported",
MB_ICONINFORMATION);
}
DragFinish(hDropInfo);
}
void CMainDlg::OnPickFile(UINT uNotifyCode, int nID, CWindow wndCtl) {
// https://learn.microsoft.com/en-us/windows/win32/shell/common-file-dialog#basic-usage
CComPtr<IFileDialog> pfd;
HRESULT hr = pfd.CoCreateInstance(CLSID_FileOpenDialog, nullptr,
CLSCTX_INPROC_SERVER);
if (FAILED(hr)) {
return;
}
// Get options.
DWORD dwFlags;
hr = pfd->GetOptions(&dwFlags);
if (FAILED(hr)) {
return;
}
// Get shell items only for file system items.
hr = pfd->SetOptions(dwFlags | FOS_FORCEFILESYSTEM);
if (FAILED(hr)) {
return;
}
// Set the file types.
const COMDLG_FILTERSPEC fileTypes[] = {
{L"Executable files (*.dll;*.exe)", L"*.dll;*.exe"},
{L"All files (*.*)", L"*.*"},
};
hr = pfd->SetFileTypes(ARRAYSIZE(fileTypes), fileTypes);
if (FAILED(hr)) {
return;
}
hr = pfd->SetTitle(L"Browse");
if (FAILED(hr)) {
return;
}
// Show the dialog.
hr = pfd->Show(m_hWnd);
if (FAILED(hr)) {
return;
}
CComPtr<IShellItem> psiResult;
hr = pfd->GetResult(&psiResult);
if (SUCCEEDED(hr)) {
CComHeapPtr<WCHAR> pszFilePath;
hr = psiResult->GetDisplayName(SIGDN_FILESYSPATH, &pszFilePath);
if (SUCCEEDED(hr)) {
// Set the file name on the textbox.
SetDlgItemText(IDC_TARGET_EXECUTABLE, pszFilePath);
}
}
}
void CMainDlg::OnAppAbout(UINT uNotifyCode, int nID, CWindow wndCtl) {
PCWSTR content =
L"A tool to get symbols from executables the same way Windhawk mods do "
L"with the symbols API.\n\n"
L"The tool was created to help with Windhawk mod development.\n\n"
L"Windhawk can be downloaded at <A "
L"HREF=\"https://windhawk.net\">windhawk.net</A>.";
TASKDIALOGCONFIG taskDialogConfig{
.cbSize = sizeof(taskDialogConfig),
.hwndParent = m_hWnd,
.hInstance = _Module.GetModuleInstance(),
.dwFlags = TDF_ENABLE_HYPERLINKS | TDF_ALLOW_DIALOG_CANCELLATION |
TDF_POSITION_RELATIVE_TO_WINDOW,
.pszWindowTitle = L"About",
.pszMainIcon = MAKEINTRESOURCE(IDR_MAINFRAME),
.pszMainInstruction = L"Windhawk Symbol Helper v" WSH_VERSION,
.pszContent = content,
.pfCallback = [](HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam,
LONG_PTR lpRefData) -> HRESULT {
switch (msg) {
case TDN_HYPERLINK_CLICKED:
OpenUrl(hwnd, (PCWSTR)lParam);
break;
}
return S_OK;
},
};
::TaskDialogIndirect(&taskDialogConfig, nullptr, nullptr, nullptr);
}
void CMainDlg::OnOK(UINT uNotifyCode, int nID, CWindow wndCtl) {
if (m_enumSymbolsThread) {
m_enumSymbolsThread->request_stop();
GetDlgItem(IDOK).EnableWindow(FALSE);
return;
}
SymbolsFromBinaryOptions options;
GetDlgItemText(IDC_ENGINE_DIR, options.engineDir);
GetDlgItemText(IDC_SYMBOLS_DIR, options.symbolsDir);
GetDlgItemText(IDC_SYMBOL_SERVER, options.symbolServer);
GetDlgItemText(IDC_TARGET_EXECUTABLE, options.targetExecutable);
options.undecorated =
CButton(GetDlgItem(IDC_UNDECORATED)).GetCheck() != BST_UNCHECKED;
options.decorated =
CButton(GetDlgItem(IDC_DECORATED)).GetCheck() != BST_UNCHECKED;
bool log = CButton(GetDlgItem(IDC_LOG)).GetCheck() != BST_UNCHECKED;
SetDlgItemText(IDOK, L"Cancel");
m_enumSymbolsThread = std::jthread(
[options = std::move(options), &enumSymbolsResult = m_enumSymbolsResult,
hWnd = m_hWnd, log](std::stop_token stopToken) {
CStringA logOutput;
try {
enumSymbolsResult = SymbolsFromBinary(
std::move(options), log ? &logOutput : nullptr,
[hWnd](int progress) {
CWindow(hWnd).PostMessage(UWM_PROGRESS,
(WPARAM)progress);
},
&stopToken);
} catch (const std::exception& e) {
enumSymbolsResult.Format(L"Error: %S\r\n%S", e.what(),
logOutput.GetString());
}
CWindow(hWnd).PostMessage(UWM_ENUM_SYMBOLS_DONE);
});
}
void CMainDlg::OnCancel(UINT uNotifyCode, int nID, CWindow wndCtl) {
DestroyWindow();
}
LRESULT CMainDlg::OnProgress(UINT uMsg, WPARAM wParam, LPARAM lParam) {
int progress = (int)wParam;
CString text;
text.Format(L"[%d%%] Cancel", progress);
SetDlgItemText(IDOK, text);
return 0;
}
LRESULT CMainDlg::OnEnumSymbolsDone(UINT uMsg, WPARAM wParam, LPARAM lParam) {
m_enumSymbolsThread.reset();
SetDlgItemText(IDC_RESULTS, m_enumSymbolsResult);
m_enumSymbolsResult.Empty();
SetDlgItemText(IDOK, L"Get &symbols");
GetDlgItem(IDOK).EnableWindow(TRUE);
return 0;
}