| 1 | /* Copyright (C) 2020 Wildfire Games.
|
|---|
| 2 | *
|
|---|
| 3 | * Permission is hereby granted, free of charge, to any person obtaining
|
|---|
| 4 | * a copy of this software and associated documentation files (the
|
|---|
| 5 | * "Software"), to deal in the Software without restriction, including
|
|---|
| 6 | * without limitation the rights to use, copy, modify, merge, publish,
|
|---|
| 7 | * distribute, sublicense, and/or sell copies of the Software, and to
|
|---|
| 8 | * permit persons to whom the Software is furnished to do so, subject to
|
|---|
| 9 | * the following conditions:
|
|---|
| 10 | *
|
|---|
| 11 | * The above copyright notice and this permission notice shall be included
|
|---|
| 12 | * in all copies or substantial portions of the Software.
|
|---|
| 13 | *
|
|---|
| 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|---|
| 15 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|---|
| 16 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|---|
| 17 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|---|
| 18 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|---|
| 19 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|---|
| 20 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|---|
| 21 | */
|
|---|
| 22 |
|
|---|
| 23 | /*
|
|---|
| 24 | * Windows backend of the sysdep interface
|
|---|
| 25 | */
|
|---|
| 26 |
|
|---|
| 27 | #include "precompiled.h"
|
|---|
| 28 | #include "lib/sysdep/sysdep.h"
|
|---|
| 29 |
|
|---|
| 30 | #include "lib/alignment.h"
|
|---|
| 31 | #include "lib/sysdep/os/win/win.h" // includes windows.h; must come before shlobj
|
|---|
| 32 | #include <SDL_clipboard.h> // SDL_SetClipboardText
|
|---|
| 33 | #include <shlobj.h> // pick_dir
|
|---|
| 34 | #include <shellapi.h> // open_url
|
|---|
| 35 | #include <Wincrypt.h>
|
|---|
| 36 | #include <WindowsX.h> // message crackers
|
|---|
| 37 | #include <winhttp.h>
|
|---|
| 38 |
|
|---|
| 39 | #include "lib/sysdep/os/win/error_dialog.h"
|
|---|
| 40 | #include "lib/sysdep/os/win/wutil.h"
|
|---|
| 41 |
|
|---|
| 42 | #if CONFIG_ENABLE_BOOST
|
|---|
| 43 | # include <boost/algorithm/string.hpp>
|
|---|
| 44 | #endif
|
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 | #if MSC_VERSION
|
|---|
| 48 | #pragma comment(lib, "shell32.lib") // for sys_pick_directory SH* calls
|
|---|
| 49 | #pragma comment(lib, "winhttp.lib")
|
|---|
| 50 | #endif
|
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 | bool sys_IsDebuggerPresent()
|
|---|
| 54 | {
|
|---|
| 55 | return (IsDebuggerPresent() != 0);
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 |
|
|---|
| 59 | std::wstring sys_WideFromArgv(const char* argv_i)
|
|---|
| 60 | {
|
|---|
| 61 | // NB: despite http://cbloomrants.blogspot.com/2008/06/06-14-08-1.html,
|
|---|
| 62 | // WinXP x64 EN cmd.exe (chcp reports 437) encodes argv u-umlaut
|
|---|
| 63 | // (entered manually or via auto-complete) via cp1252. the same applies
|
|---|
| 64 | // to WinXP SP2 DE (where chcp reports 850).
|
|---|
| 65 | const UINT cp = CP_ACP;
|
|---|
| 66 | const DWORD flags = MB_PRECOMPOSED|MB_ERR_INVALID_CHARS;
|
|---|
| 67 | const int inputSize = -1; // null-terminated
|
|---|
| 68 | std::vector<wchar_t> buf(strlen(argv_i)+1); // (upper bound on number of characters)
|
|---|
| 69 | // NB: avoid mbstowcs because it may specify another locale
|
|---|
| 70 | const int ret = MultiByteToWideChar(cp, flags, argv_i, (int)inputSize, &buf[0], (int)buf.size());
|
|---|
| 71 | ENSURE(ret != 0);
|
|---|
| 72 | return std::wstring(&buf[0]);
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 |
|
|---|
| 76 | void sys_display_msg(const wchar_t* caption, const wchar_t* msg)
|
|---|
| 77 | {
|
|---|
| 78 | MessageBoxW(0, msg, caption, MB_ICONEXCLAMATION|MB_TASKMODAL|MB_SETFOREGROUND);
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 |
|
|---|
| 82 | //-----------------------------------------------------------------------------
|
|---|
| 83 | // "program error" dialog (triggered by ENSURE and exception)
|
|---|
| 84 | //-----------------------------------------------------------------------------
|
|---|
| 85 |
|
|---|
| 86 | // support for resizing the dialog / its controls (must be done manually)
|
|---|
| 87 |
|
|---|
| 88 | static POINTS dlg_clientOrigin;
|
|---|
| 89 | static POINTS dlg_prevClientSize;
|
|---|
| 90 |
|
|---|
| 91 | static void dlg_OnMove(HWND UNUSED(hDlg), int x, int y)
|
|---|
| 92 | {
|
|---|
| 93 | dlg_clientOrigin.x = (short)x;
|
|---|
| 94 | dlg_clientOrigin.y = (short)y;
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 |
|
|---|
| 98 | static const size_t ANCHOR_LEFT = 0x01;
|
|---|
| 99 | static const size_t ANCHOR_RIGHT = 0x02;
|
|---|
| 100 | static const size_t ANCHOR_TOP = 0x04;
|
|---|
| 101 | static const size_t ANCHOR_BOTTOM = 0x08;
|
|---|
| 102 | static const size_t ANCHOR_ALL = 0x0F;
|
|---|
| 103 |
|
|---|
| 104 | static void dlg_ResizeControl(HWND hDlg, int dlgItem, int dx, int dy, size_t anchors)
|
|---|
| 105 | {
|
|---|
| 106 | HWND hControl = GetDlgItem(hDlg, dlgItem);
|
|---|
| 107 | RECT r;
|
|---|
| 108 | GetWindowRect(hControl, &r);
|
|---|
| 109 |
|
|---|
| 110 | int w = r.right - r.left, h = r.bottom - r.top;
|
|---|
| 111 | int x = r.left - dlg_clientOrigin.x, y = r.top - dlg_clientOrigin.y;
|
|---|
| 112 |
|
|---|
| 113 | if(anchors & ANCHOR_RIGHT)
|
|---|
| 114 | {
|
|---|
| 115 | // right only
|
|---|
| 116 | if(!(anchors & ANCHOR_LEFT))
|
|---|
| 117 | x += dx;
|
|---|
| 118 | // horizontal (stretch width)
|
|---|
| 119 | else
|
|---|
| 120 | w += dx;
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | if(anchors & ANCHOR_BOTTOM)
|
|---|
| 124 | {
|
|---|
| 125 | // bottom only
|
|---|
| 126 | if(!(anchors & ANCHOR_TOP))
|
|---|
| 127 | y += dy;
|
|---|
| 128 | // vertical (stretch height)
|
|---|
| 129 | else
|
|---|
| 130 | h += dy;
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | SetWindowPos(hControl, 0, x,y, w,h, SWP_NOZORDER);
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 |
|
|---|
| 137 | static void dlg_OnSize(HWND hDlg, UINT state, int clientSizeX, int clientSizeY)
|
|---|
| 138 | {
|
|---|
| 139 | // 'minimize' was clicked. we need to ignore this, otherwise
|
|---|
| 140 | // dx/dy would reduce some control positions to less than 0.
|
|---|
| 141 | // since Windows clips them, we wouldn't later be able to
|
|---|
| 142 | // reconstruct the previous values when 'restoring'.
|
|---|
| 143 | if(state == SIZE_MINIMIZED)
|
|---|
| 144 | return;
|
|---|
| 145 |
|
|---|
| 146 | // NB: origin might legitimately be 0, but we know it is invalid
|
|---|
| 147 | // on the first call to this function, where dlg_prevClientSize is 0.
|
|---|
| 148 | const bool isOriginValid = (dlg_prevClientSize.y != 0);
|
|---|
| 149 |
|
|---|
| 150 | const int dx = clientSizeX - dlg_prevClientSize.x;
|
|---|
| 151 | const int dy = clientSizeY - dlg_prevClientSize.y;
|
|---|
| 152 | dlg_prevClientSize.x = (short)clientSizeX;
|
|---|
| 153 | dlg_prevClientSize.y = (short)clientSizeY;
|
|---|
| 154 |
|
|---|
| 155 | if(!isOriginValid) // must not call dlg_ResizeControl
|
|---|
| 156 | return;
|
|---|
| 157 |
|
|---|
| 158 | dlg_ResizeControl(hDlg, IDC_CONTINUE, dx,dy, ANCHOR_LEFT|ANCHOR_BOTTOM);
|
|---|
| 159 | dlg_ResizeControl(hDlg, IDC_SUPPRESS, dx,dy, ANCHOR_LEFT|ANCHOR_BOTTOM);
|
|---|
| 160 | dlg_ResizeControl(hDlg, IDC_BREAK , dx,dy, ANCHOR_LEFT|ANCHOR_BOTTOM);
|
|---|
| 161 | dlg_ResizeControl(hDlg, IDC_EXIT , dx,dy, ANCHOR_LEFT|ANCHOR_BOTTOM);
|
|---|
| 162 | dlg_ResizeControl(hDlg, IDC_COPY , dx,dy, ANCHOR_RIGHT|ANCHOR_BOTTOM);
|
|---|
| 163 | dlg_ResizeControl(hDlg, IDC_EDIT1 , dx,dy, ANCHOR_ALL);
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 |
|
|---|
| 167 | static void dlg_OnGetMinMaxInfo(HWND UNUSED(hDlg), LPMINMAXINFO mmi)
|
|---|
| 168 | {
|
|---|
| 169 | // we must make sure resize_control will never set negative coords -
|
|---|
| 170 | // Windows would clip them, and its real position would be lost.
|
|---|
| 171 | // restrict to a reasonable and good looking minimum size [pixels].
|
|---|
| 172 | mmi->ptMinTrackSize.x = 407;
|
|---|
| 173 | mmi->ptMinTrackSize.y = 159; // determined experimentally
|
|---|
| 174 | }
|
|---|
| 175 |
|
|---|
| 176 |
|
|---|
| 177 | struct DialogParams
|
|---|
| 178 | {
|
|---|
| 179 | const wchar_t* text;
|
|---|
| 180 | size_t flags;
|
|---|
| 181 | };
|
|---|
| 182 |
|
|---|
| 183 | static BOOL dlg_OnInitDialog(HWND hDlg, HWND UNUSED(hWndFocus), LPARAM lParam)
|
|---|
| 184 | {
|
|---|
| 185 | const DialogParams* params = reinterpret_cast<const DialogParams*>(lParam);
|
|---|
| 186 | SetWindowLongPtr(hDlg, DWLP_USER, lParam);
|
|---|
| 187 | HWND hWnd;
|
|---|
| 188 |
|
|---|
| 189 | // need to reset for new instance of dialog
|
|---|
| 190 | dlg_clientOrigin.x = dlg_clientOrigin.y = 0;
|
|---|
| 191 | dlg_prevClientSize.x = dlg_prevClientSize.y = 0;
|
|---|
| 192 |
|
|---|
| 193 | if(!(params->flags & DE_ALLOW_SUPPRESS))
|
|---|
| 194 | {
|
|---|
| 195 | hWnd = GetDlgItem(hDlg, IDC_SUPPRESS);
|
|---|
| 196 | EnableWindow(hWnd, FALSE);
|
|---|
| 197 | }
|
|---|
| 198 |
|
|---|
| 199 | if(params->flags & DE_NO_CONTINUE)
|
|---|
| 200 | {
|
|---|
| 201 | hWnd = GetDlgItem(hDlg, IDC_CONTINUE);
|
|---|
| 202 | EnableWindow(hWnd, FALSE);
|
|---|
| 203 | }
|
|---|
| 204 |
|
|---|
| 205 | // set fixed font for readability
|
|---|
| 206 | hWnd = GetDlgItem(hDlg, IDC_EDIT1);
|
|---|
| 207 | HGDIOBJ hObj = (HGDIOBJ)GetStockObject(SYSTEM_FIXED_FONT);
|
|---|
| 208 | LPARAM redraw = FALSE;
|
|---|
| 209 | SendMessage(hWnd, WM_SETFONT, (WPARAM)hObj, redraw);
|
|---|
| 210 |
|
|---|
| 211 | SetDlgItemTextW(hDlg, IDC_EDIT1, params->text);
|
|---|
| 212 | return TRUE; // set default keyboard focus
|
|---|
| 213 | }
|
|---|
| 214 |
|
|---|
| 215 |
|
|---|
| 216 | static void dlg_OnCommand(HWND hDlg, int id, HWND UNUSED(hWndCtl), UINT UNUSED(codeNotify))
|
|---|
| 217 | {
|
|---|
| 218 | switch(id)
|
|---|
| 219 | {
|
|---|
| 220 | case IDC_COPY:
|
|---|
| 221 | {
|
|---|
| 222 | std::vector<wchar_t> buf(128*KiB); // (too big for stack)
|
|---|
| 223 | GetDlgItemTextW(hDlg, IDC_EDIT1, &buf[0], (int)buf.size());
|
|---|
| 224 | std::string string = utf8_from_wstring(&buf[0]);
|
|---|
| 225 | SDL_SetClipboardText(string.c_str());
|
|---|
| 226 | break;
|
|---|
| 227 | }
|
|---|
| 228 |
|
|---|
| 229 | case IDC_CONTINUE:
|
|---|
| 230 | EndDialog(hDlg, ERI_CONTINUE);
|
|---|
| 231 | break;
|
|---|
| 232 | case IDC_SUPPRESS:
|
|---|
| 233 | EndDialog(hDlg, ERI_SUPPRESS);
|
|---|
| 234 | break;
|
|---|
| 235 | case IDC_BREAK:
|
|---|
| 236 | EndDialog(hDlg, ERI_BREAK);
|
|---|
| 237 | break;
|
|---|
| 238 | case IDC_EXIT:
|
|---|
| 239 | EndDialog(hDlg, ERI_EXIT);
|
|---|
| 240 | break;
|
|---|
| 241 |
|
|---|
| 242 | default:
|
|---|
| 243 | break;
|
|---|
| 244 | }
|
|---|
| 245 | }
|
|---|
| 246 |
|
|---|
| 247 |
|
|---|
| 248 | static void dlg_OnClose(HWND hDlg)
|
|---|
| 249 | {
|
|---|
| 250 | const DialogParams* params = reinterpret_cast<const DialogParams*>(
|
|---|
| 251 | GetWindowLongPtr(hDlg, DWLP_USER));
|
|---|
| 252 | if (!params)
|
|---|
| 253 | return;
|
|---|
| 254 |
|
|---|
| 255 | // Interpret close as exit in case we can't continue.
|
|---|
| 256 | if(params->flags & DE_NO_CONTINUE)
|
|---|
| 257 | EndDialog(hDlg, ERI_EXIT);
|
|---|
| 258 | }
|
|---|
| 259 |
|
|---|
| 260 |
|
|---|
| 261 | static void dlg_OnSysCommand(HWND hDlg, UINT cmd, int UNUSED(x), int UNUSED(y))
|
|---|
| 262 | {
|
|---|
| 263 | switch(cmd & 0xFFF0) // NB: lower 4 bits are reserved
|
|---|
| 264 | {
|
|---|
| 265 | // [X] clicked -> close dialog (doesn't happen automatically)
|
|---|
| 266 | case SC_CLOSE:
|
|---|
| 267 | EndDialog(hDlg, 0);
|
|---|
| 268 | break;
|
|---|
| 269 |
|
|---|
| 270 | default:
|
|---|
| 271 | break;
|
|---|
| 272 | }
|
|---|
| 273 | }
|
|---|
| 274 |
|
|---|
| 275 |
|
|---|
| 276 | static INT_PTR CALLBACK dlg_OnMessage(HWND hDlg, unsigned int msg, WPARAM wParam, LPARAM lParam)
|
|---|
| 277 | {
|
|---|
| 278 | switch(msg)
|
|---|
| 279 | {
|
|---|
| 280 | case WM_INITDIALOG:
|
|---|
| 281 | return HANDLE_WM_INITDIALOG(hDlg, wParam, lParam, dlg_OnInitDialog);
|
|---|
| 282 |
|
|---|
| 283 | case WM_SYSCOMMAND:
|
|---|
| 284 | return HANDLE_WM_SYSCOMMAND(hDlg, wParam, lParam, dlg_OnSysCommand);
|
|---|
| 285 |
|
|---|
| 286 | case WM_COMMAND:
|
|---|
| 287 | return HANDLE_WM_COMMAND(hDlg, wParam, lParam, dlg_OnCommand);
|
|---|
| 288 |
|
|---|
| 289 | case WM_MOVE:
|
|---|
| 290 | return HANDLE_WM_MOVE(hDlg, wParam, lParam, dlg_OnMove);
|
|---|
| 291 |
|
|---|
| 292 | case WM_GETMINMAXINFO:
|
|---|
| 293 | return HANDLE_WM_GETMINMAXINFO(hDlg, wParam, lParam, dlg_OnGetMinMaxInfo);
|
|---|
| 294 |
|
|---|
| 295 | case WM_SIZE:
|
|---|
| 296 | return HANDLE_WM_SIZE(hDlg, wParam, lParam, dlg_OnSize);
|
|---|
| 297 |
|
|---|
| 298 | case WM_CLOSE:
|
|---|
| 299 | return HANDLE_WM_CLOSE(hDlg, wParam, lParam, dlg_OnClose);
|
|---|
| 300 |
|
|---|
| 301 | default:
|
|---|
| 302 | // we didn't process the message; caller will perform default action.
|
|---|
| 303 | return FALSE;
|
|---|
| 304 | }
|
|---|
| 305 | }
|
|---|
| 306 |
|
|---|
| 307 |
|
|---|
| 308 | ErrorReactionInternal sys_display_error(const wchar_t* text, size_t flags)
|
|---|
| 309 | {
|
|---|
| 310 | // note: other threads might still be running, crash and take down the
|
|---|
| 311 | // process before we have a chance to display this error message.
|
|---|
| 312 | // ideally we would suspend them all and resume when finished; however,
|
|---|
| 313 | // they may be holding system-wide locks (e.g. heap or loader) that
|
|---|
| 314 | // are potentially needed by DialogBoxParam. in that case, deadlock
|
|---|
| 315 | // would result; this is much worse than a crash because no error
|
|---|
| 316 | // at all is displayed to the end-user. therefore, do nothing here.
|
|---|
| 317 |
|
|---|
| 318 | // temporarily remove any pending quit message from the queue because
|
|---|
| 319 | // it would prevent the dialog from being displayed (DialogBoxParam
|
|---|
| 320 | // returns IDOK without doing anything). will be restored below.
|
|---|
| 321 | // notes:
|
|---|
| 322 | // - this isn't only relevant at exit - Windows also posts one if
|
|---|
| 323 | // window init fails. therefore, it is important that errors can be
|
|---|
| 324 | // displayed regardless.
|
|---|
| 325 | // - by passing hWnd=0, we check all windows belonging to the current
|
|---|
| 326 | // thread. there is no reason to use hWndParent below.
|
|---|
| 327 | MSG msg;
|
|---|
| 328 | const BOOL isQuitPending = PeekMessage(&msg, 0, WM_QUIT, WM_QUIT, PM_REMOVE);
|
|---|
| 329 |
|
|---|
| 330 | const HINSTANCE hInstance = wutil_LibModuleHandle();
|
|---|
| 331 | LPCWSTR lpTemplateName = MAKEINTRESOURCEW(IDD_DIALOG1);
|
|---|
| 332 | const DialogParams params = { text, flags };
|
|---|
| 333 | // get the enclosing app's window handle. we can't just pass 0 or
|
|---|
| 334 | // the desktop window because the dialog must be modal (if the app
|
|---|
| 335 | // continues running, it may crash and take down the process before
|
|---|
| 336 | // we've managed to show the dialog).
|
|---|
| 337 | const HWND hWndParent = wutil_AppWindow();
|
|---|
| 338 |
|
|---|
| 339 | INT_PTR ret = DialogBoxParamW(hInstance, lpTemplateName, hWndParent, dlg_OnMessage, (LPARAM)¶ms);
|
|---|
| 340 |
|
|---|
| 341 | if(isQuitPending)
|
|---|
| 342 | PostQuitMessage((int)msg.wParam);
|
|---|
| 343 |
|
|---|
| 344 | // failed; warn user and make sure we return an ErrorReactionInternal.
|
|---|
| 345 | if(ret == 0 || ret == -1)
|
|---|
| 346 | {
|
|---|
| 347 | debug_DisplayMessage(L"Error", L"Unable to display detailed error dialog.");
|
|---|
| 348 | return ERI_CONTINUE;
|
|---|
| 349 | }
|
|---|
| 350 | return (ErrorReactionInternal)ret;
|
|---|
| 351 | }
|
|---|
| 352 |
|
|---|
| 353 |
|
|---|
| 354 | //-----------------------------------------------------------------------------
|
|---|
| 355 | // misc
|
|---|
| 356 | //-----------------------------------------------------------------------------
|
|---|
| 357 |
|
|---|
| 358 | Status sys_StatusDescription(int user_err, wchar_t* buf, size_t max_chars)
|
|---|
| 359 | {
|
|---|
| 360 | // validate user_err - Win32 doesn't have negative error numbers
|
|---|
| 361 | if(user_err < 0)
|
|---|
| 362 | return ERR::FAIL; // NOWARN
|
|---|
| 363 |
|
|---|
| 364 | const DWORD errorCode = user_err? (DWORD)user_err : GetLastError();
|
|---|
| 365 |
|
|---|
| 366 | // no one likes to see "The operation completed successfully" in
|
|---|
| 367 | // error messages, so return more descriptive text instead.
|
|---|
| 368 | if(errorCode == 0)
|
|---|
| 369 | {
|
|---|
| 370 | wcscpy_s(buf, max_chars, L"0 (no error code was set)");
|
|---|
| 371 | return INFO::OK;
|
|---|
| 372 | }
|
|---|
| 373 |
|
|---|
| 374 | wchar_t message[400];
|
|---|
| 375 | if(errorCode == ERROR_NOT_ENOUGH_MEMORY)
|
|---|
| 376 | {
|
|---|
| 377 | // We handle out of memory separately to prevent possible subsequent/nested allocations.
|
|---|
| 378 | swprintf_s(message, ARRAY_SIZE(message), L"Not enough memory resources are available to process this command.");
|
|---|
| 379 | }
|
|---|
| 380 | else
|
|---|
| 381 | {
|
|---|
| 382 | const LPCVOID source = 0; // ignored (we're not using FROM_HMODULE etc.)
|
|---|
| 383 | const DWORD lang_id = 0; // look for neutral, then current locale
|
|---|
| 384 | va_list* args = 0; // we don't care about "inserts"
|
|---|
| 385 | const DWORD charsWritten = FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM, source, errorCode, lang_id, message, (DWORD)ARRAY_SIZE(message), args);
|
|---|
| 386 | if(!charsWritten)
|
|---|
| 387 | WARN_RETURN(ERR::FAIL);
|
|---|
| 388 | ENSURE(charsWritten < max_chars);
|
|---|
| 389 | if(message[charsWritten-1] == '\n')
|
|---|
| 390 | message[charsWritten-1] = '\0';
|
|---|
| 391 | if(message[charsWritten-2] == '\r')
|
|---|
| 392 | message[charsWritten-2] = '\0';
|
|---|
| 393 | }
|
|---|
| 394 |
|
|---|
| 395 | const int charsWritten = swprintf_s(buf, max_chars, L"%d (%ls)", errorCode, message);
|
|---|
| 396 | ENSURE(charsWritten != -1);
|
|---|
| 397 | return INFO::OK;
|
|---|
| 398 | }
|
|---|
| 399 |
|
|---|
| 400 |
|
|---|
| 401 | static Status GetModulePathname(HMODULE hModule, OsPath& pathname)
|
|---|
| 402 | {
|
|---|
| 403 | wchar_t pathnameBuf[32768]; // NTFS limit
|
|---|
| 404 | const DWORD length = (DWORD)ARRAY_SIZE(pathnameBuf);
|
|---|
| 405 | const DWORD charsWritten = GetModuleFileNameW(hModule, pathnameBuf, length);
|
|---|
| 406 | if(charsWritten == 0) // failed
|
|---|
| 407 | WARN_RETURN(StatusFromWin());
|
|---|
| 408 | ENSURE(charsWritten < length); // why would the above buffer ever be exceeded?
|
|---|
| 409 | pathname = pathnameBuf;
|
|---|
| 410 | return INFO::OK;
|
|---|
| 411 | }
|
|---|
| 412 |
|
|---|
| 413 |
|
|---|
| 414 | Status sys_get_module_filename(void* addr, OsPath& pathname)
|
|---|
| 415 | {
|
|---|
| 416 | MEMORY_BASIC_INFORMATION mbi;
|
|---|
| 417 | const SIZE_T bytesWritten = VirtualQuery(addr, &mbi, sizeof(mbi));
|
|---|
| 418 | if(!bytesWritten)
|
|---|
| 419 | WARN_RETURN(StatusFromWin());
|
|---|
| 420 | ENSURE(bytesWritten >= sizeof(mbi));
|
|---|
| 421 | return GetModulePathname((HMODULE)mbi.AllocationBase, pathname);
|
|---|
| 422 | }
|
|---|
| 423 |
|
|---|
| 424 |
|
|---|
| 425 | OsPath sys_ExecutablePathname()
|
|---|
| 426 | {
|
|---|
| 427 | WinScopedPreserveLastError s;
|
|---|
| 428 | OsPath pathname;
|
|---|
| 429 | ENSURE(GetModulePathname(0, pathname) == INFO::OK);
|
|---|
| 430 | return pathname;
|
|---|
| 431 | }
|
|---|
| 432 |
|
|---|
| 433 |
|
|---|
| 434 | std::wstring sys_get_user_name()
|
|---|
| 435 | {
|
|---|
| 436 | wchar_t usernameBuf[256];
|
|---|
| 437 | DWORD size = ARRAY_SIZE(usernameBuf);
|
|---|
| 438 | if(!GetUserNameW(usernameBuf, &size))
|
|---|
| 439 | return L"";
|
|---|
| 440 | return usernameBuf;
|
|---|
| 441 | }
|
|---|
| 442 |
|
|---|
| 443 |
|
|---|
| 444 | // callback for shell directory picker: used to set starting directory
|
|---|
| 445 | // (for user convenience).
|
|---|
| 446 | static int CALLBACK BrowseCallback(HWND hWnd, unsigned int msg, LPARAM UNUSED(lParam), LPARAM lpData)
|
|---|
| 447 | {
|
|---|
| 448 | if(msg == BFFM_INITIALIZED)
|
|---|
| 449 | {
|
|---|
| 450 | const WPARAM wParam = TRUE; // lpData is a Unicode string, not PIDL.
|
|---|
| 451 | // (MSDN: the return values for both of these BFFM_ notifications are ignored)
|
|---|
| 452 | (void)SendMessage(hWnd, BFFM_SETSELECTIONW, wParam, lpData);
|
|---|
| 453 | }
|
|---|
| 454 |
|
|---|
| 455 | return 0;
|
|---|
| 456 | }
|
|---|
| 457 |
|
|---|
| 458 | Status sys_pick_directory(OsPath& path)
|
|---|
| 459 | {
|
|---|
| 460 | // (must not use multi-threaded apartment due to BIF_NEWDIALOGSTYLE)
|
|---|
| 461 | const HRESULT hr = CoInitialize(0);
|
|---|
| 462 | ENSURE(hr == S_OK || hr == S_FALSE); // S_FALSE == already initialized
|
|---|
| 463 |
|
|---|
| 464 | // note: bi.pszDisplayName isn't the full path, so it isn't of any use.
|
|---|
| 465 | BROWSEINFOW bi;
|
|---|
| 466 | memset(&bi, 0, sizeof(bi));
|
|---|
| 467 | bi.ulFlags = BIF_RETURNONLYFSDIRS|BIF_NEWDIALOGSTYLE|BIF_NONEWFOLDERBUTTON;
|
|---|
| 468 | // for setting starting directory:
|
|---|
| 469 | bi.lpfn = (BFFCALLBACK)BrowseCallback;
|
|---|
| 470 | const Path::String initialPath = OsString(path); // NB: BFFM_SETSELECTIONW can't deal with '/' separators
|
|---|
| 471 | bi.lParam = (LPARAM)initialPath.c_str();
|
|---|
| 472 | const LPITEMIDLIST pidl = SHBrowseForFolderW(&bi);
|
|---|
| 473 | if(!pidl) // user canceled
|
|---|
| 474 | return INFO::SKIPPED;
|
|---|
| 475 |
|
|---|
| 476 | // translate ITEMIDLIST to string
|
|---|
| 477 | wchar_t pathBuf[MAX_PATH]; // mandated by SHGetPathFromIDListW
|
|---|
| 478 | const BOOL ok = SHGetPathFromIDListW(pidl, pathBuf);
|
|---|
| 479 |
|
|---|
| 480 | // free the ITEMIDLIST
|
|---|
| 481 | CoTaskMemFree(pidl);
|
|---|
| 482 |
|
|---|
| 483 | if(ok == TRUE)
|
|---|
| 484 | {
|
|---|
| 485 | path = pathBuf;
|
|---|
| 486 | return INFO::OK;
|
|---|
| 487 | }
|
|---|
| 488 |
|
|---|
| 489 | // Balance call to CoInitialize, which must have been successful
|
|---|
| 490 | CoUninitialize();
|
|---|
| 491 |
|
|---|
| 492 | WARN_RETURN(StatusFromWin());
|
|---|
| 493 | }
|
|---|
| 494 |
|
|---|
| 495 |
|
|---|
| 496 | Status sys_open_url(const std::string& url)
|
|---|
| 497 | {
|
|---|
| 498 | HINSTANCE r = ShellExecuteA(NULL, "open", url.c_str(), NULL, NULL, SW_SHOWNORMAL);
|
|---|
| 499 | if ((int)(intptr_t)r > 32)
|
|---|
| 500 | return INFO::OK;
|
|---|
| 501 |
|
|---|
| 502 | WARN_RETURN(ERR::FAIL);
|
|---|
| 503 | }
|
|---|
| 504 |
|
|---|
| 505 |
|
|---|
| 506 | Status sys_generate_random_bytes(u8* buffer, size_t size)
|
|---|
| 507 | {
|
|---|
| 508 | HCRYPTPROV hCryptProv = 0;
|
|---|
| 509 | if(!CryptAcquireContext(&hCryptProv, 0, 0, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
|
|---|
| 510 | WARN_RETURN(StatusFromWin());
|
|---|
| 511 |
|
|---|
| 512 | memset(buffer, 0, size);
|
|---|
| 513 | if(!CryptGenRandom(hCryptProv, (DWORD)size, (BYTE*)buffer))
|
|---|
| 514 | WARN_RETURN(StatusFromWin());
|
|---|
| 515 |
|
|---|
| 516 | if(!CryptReleaseContext(hCryptProv, 0))
|
|---|
| 517 | WARN_RETURN(StatusFromWin());
|
|---|
| 518 |
|
|---|
| 519 | return INFO::OK;
|
|---|
| 520 | }
|
|---|
| 521 |
|
|---|
| 522 |
|
|---|
| 523 | #if CONFIG_ENABLE_BOOST
|
|---|
| 524 |
|
|---|
| 525 | /*
|
|---|
| 526 | * Given a string of the form
|
|---|
| 527 | * "example.com:80"
|
|---|
| 528 | * or
|
|---|
| 529 | * "ftp=ftp.example.com:80;http=example.com:80;https=example.com:80"
|
|---|
| 530 | * separated by semicolons or whitespace,
|
|---|
| 531 | * return the string "example.com:80".
|
|---|
| 532 | */
|
|---|
| 533 | static std::wstring parse_proxy(const std::wstring& input)
|
|---|
| 534 | {
|
|---|
| 535 | if(input.find('=') == input.npos)
|
|---|
| 536 | return input;
|
|---|
| 537 |
|
|---|
| 538 | std::vector<std::wstring> parts;
|
|---|
| 539 | split(parts, input, boost::algorithm::is_any_of("; \t\r\n"), boost::algorithm::token_compress_on);
|
|---|
| 540 |
|
|---|
| 541 | for(size_t i = 0; i < parts.size(); ++i)
|
|---|
| 542 | if(boost::algorithm::starts_with(parts[i], "http="))
|
|---|
| 543 | return parts[i].substr(5);
|
|---|
| 544 |
|
|---|
| 545 | // If we got this far, proxies were only set for non-HTTP protocols
|
|---|
| 546 | return L"";
|
|---|
| 547 | }
|
|---|
| 548 |
|
|---|
| 549 | Status sys_get_proxy_config(const std::wstring& url, std::wstring& proxy)
|
|---|
| 550 | {
|
|---|
| 551 | WINHTTP_AUTOPROXY_OPTIONS autoProxyOptions;
|
|---|
| 552 | memset(&autoProxyOptions, 0, sizeof(autoProxyOptions));
|
|---|
| 553 | autoProxyOptions.dwFlags = WINHTTP_AUTOPROXY_AUTO_DETECT;
|
|---|
| 554 | autoProxyOptions.dwAutoDetectFlags = WINHTTP_AUTO_DETECT_TYPE_DHCP | WINHTTP_AUTO_DETECT_TYPE_DNS_A;
|
|---|
| 555 | autoProxyOptions.fAutoLogonIfChallenged = TRUE;
|
|---|
| 556 |
|
|---|
| 557 | WINHTTP_PROXY_INFO proxyInfo;
|
|---|
| 558 | memset(&proxyInfo, 0, sizeof(proxyInfo));
|
|---|
| 559 |
|
|---|
| 560 | WINHTTP_CURRENT_USER_IE_PROXY_CONFIG ieConfig;
|
|---|
| 561 | memset(&ieConfig, 0, sizeof(ieConfig));
|
|---|
| 562 |
|
|---|
| 563 | HINTERNET hSession = NULL;
|
|---|
| 564 |
|
|---|
| 565 | Status err = INFO::SKIPPED;
|
|---|
| 566 |
|
|---|
| 567 | bool useAutoDetect;
|
|---|
| 568 |
|
|---|
| 569 | if(WinHttpGetIEProxyConfigForCurrentUser(&ieConfig))
|
|---|
| 570 | {
|
|---|
| 571 | if(ieConfig.lpszAutoConfigUrl)
|
|---|
| 572 | {
|
|---|
| 573 | // Use explicit auto-config script if specified
|
|---|
| 574 | useAutoDetect = true;
|
|---|
| 575 | autoProxyOptions.dwFlags |= WINHTTP_AUTOPROXY_CONFIG_URL;
|
|---|
| 576 | autoProxyOptions.lpszAutoConfigUrl = ieConfig.lpszAutoConfigUrl;
|
|---|
| 577 | }
|
|---|
| 578 | else
|
|---|
| 579 | {
|
|---|
| 580 | // Use auto-discovery if enabled
|
|---|
| 581 | useAutoDetect = (ieConfig.fAutoDetect == TRUE);
|
|---|
| 582 | }
|
|---|
| 583 | }
|
|---|
| 584 | else
|
|---|
| 585 | {
|
|---|
| 586 | // Can't find IE config settings - fall back to auto-discovery
|
|---|
| 587 | useAutoDetect = true;
|
|---|
| 588 | }
|
|---|
| 589 |
|
|---|
| 590 | if(useAutoDetect)
|
|---|
| 591 | {
|
|---|
| 592 | hSession = WinHttpOpen(NULL, WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
|
|---|
| 593 | WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);
|
|---|
| 594 |
|
|---|
| 595 | if(hSession && WinHttpGetProxyForUrl(hSession, url.c_str(), &autoProxyOptions, &proxyInfo) && proxyInfo.lpszProxy)
|
|---|
| 596 | {
|
|---|
| 597 | proxy = parse_proxy(proxyInfo.lpszProxy);
|
|---|
| 598 | if(!proxy.empty())
|
|---|
| 599 | {
|
|---|
| 600 | err = INFO::OK;
|
|---|
| 601 | goto done;
|
|---|
| 602 | }
|
|---|
| 603 | }
|
|---|
| 604 | }
|
|---|
| 605 |
|
|---|
| 606 | // No valid auto-config; try explicit proxy instead
|
|---|
| 607 | if(ieConfig.lpszProxy)
|
|---|
| 608 | {
|
|---|
| 609 | proxy = parse_proxy(ieConfig.lpszProxy);
|
|---|
| 610 | if(!proxy.empty())
|
|---|
| 611 | {
|
|---|
| 612 | err = INFO::OK;
|
|---|
| 613 | goto done;
|
|---|
| 614 | }
|
|---|
| 615 | }
|
|---|
| 616 |
|
|---|
| 617 | done:
|
|---|
| 618 | if(ieConfig.lpszProxy)
|
|---|
| 619 | GlobalFree(ieConfig.lpszProxy);
|
|---|
| 620 | if(ieConfig.lpszProxyBypass)
|
|---|
| 621 | GlobalFree(ieConfig.lpszProxyBypass);
|
|---|
| 622 | if(ieConfig.lpszAutoConfigUrl)
|
|---|
| 623 | GlobalFree(ieConfig.lpszAutoConfigUrl);
|
|---|
| 624 | if(proxyInfo.lpszProxy)
|
|---|
| 625 | GlobalFree(proxyInfo.lpszProxy);
|
|---|
| 626 | if(proxyInfo.lpszProxyBypass)
|
|---|
| 627 | GlobalFree(proxyInfo.lpszProxyBypass);
|
|---|
| 628 | if(hSession)
|
|---|
| 629 | WinHttpCloseHandle(hSession);
|
|---|
| 630 |
|
|---|
| 631 | return err;
|
|---|
| 632 | }
|
|---|
| 633 |
|
|---|
| 634 | #endif
|
|---|
| 635 |
|
|---|
| 636 | FILE* sys_OpenFile(const OsPath& pathname, const char* mode)
|
|---|
| 637 | {
|
|---|
| 638 | FILE* f = 0;
|
|---|
| 639 | const std::wstring wmode(mode, mode+strlen(mode));
|
|---|
| 640 | (void)_wfopen_s(&f, OsString(pathname).c_str(), wmode.c_str());
|
|---|
| 641 | return f;
|
|---|
| 642 | }
|
|---|