27 #include "precompiled.h"
42 #if CONFIG_ENABLE_BOOST
43 # include <boost/algorithm/string.hpp>
48 #pragma comment(lib, "shell32.lib") // for sys_pick_directory SH* calls
49 #pragma comment(lib, "winhttp.lib")
55 return (IsDebuggerPresent() != 0);
65 const UINT cp = CP_ACP;
66 const DWORD flags = MB_PRECOMPOSED|MB_ERR_INVALID_CHARS;
67 const int inputSize = -1;
68 std::vector<wchar_t> buf(strlen(argv_i)+1);
70 const int ret = MultiByteToWideChar(cp, flags, argv_i, (
int)inputSize, &buf[0], (
int)buf.size());
72 return std::wstring(&buf[0]);
78 MessageBoxW(0, msg, caption, MB_ICONEXCLAMATION|MB_TASKMODAL|MB_SETFOREGROUND);
106 HWND hControl = GetDlgItem(hDlg, dlgItem);
108 GetWindowRect(hControl, &r);
113 if(anchors & ANCHOR_RIGHT)
116 if(!(anchors & ANCHOR_LEFT))
123 if(anchors & ANCHOR_BOTTOM)
126 if(!(anchors & ANCHOR_TOP))
133 SetWindowPos(hControl, 0, x,y, w,h, SWP_NOZORDER);
143 if(state == SIZE_MINIMIZED)
172 mmi->ptMinTrackSize.x = 407;
173 mmi->ptMinTrackSize.y = 159;
195 EnableWindow(hWnd, FALSE);
200 HGDIOBJ hObj = (HGDIOBJ)GetStockObject(SYSTEM_FIXED_FONT);
201 LPARAM redraw = FALSE;
202 SendMessage(hWnd, WM_SETFONT, (WPARAM)hObj, redraw);
215 std::vector<wchar_t> buf(128*
KiB);
216 GetDlgItemTextW(hDlg,
IDC_EDIT1, &buf[0], (
int)buf.size());
266 return HANDLE_WM_COMMAND(hDlg, wParam, lParam,
dlg_OnCommand);
269 return HANDLE_WM_MOVE(hDlg, wParam, lParam,
dlg_OnMove);
271 case WM_GETMINMAXINFO:
275 return HANDLE_WM_SIZE(hDlg, wParam, lParam,
dlg_OnSize);
304 const BOOL isQuitPending = PeekMessage(&msg, 0, WM_QUIT, WM_QUIT, PM_REMOVE);
307 LPCWSTR lpTemplateName = MAKEINTRESOURCEW(
IDD_DIALOG1);
315 INT_PTR ret = DialogBoxParamW(hInstance, lpTemplateName, hWndParent,
dlg_OnMessage, (LPARAM)¶ms);
318 PostQuitMessage((
int)msg.wParam);
321 if(ret == 0 || ret == -1)
340 const DWORD err = user_err? (
DWORD)user_err : GetLastError();
346 wcscpy_s(buf, max_chars, L
"0 (no error code was set)");
350 wchar_t message[400];
352 const LPCVOID source = 0;
353 const DWORD lang_id = 0;
355 const DWORD charsWritten = FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM, source, err, lang_id, message, (
DWORD)
ARRAY_SIZE(message), args);
358 ENSURE(charsWritten < max_chars);
359 if(message[charsWritten-1] ==
'\n')
360 message[charsWritten-1] =
'\0';
361 if(message[charsWritten-2] ==
'\r')
362 message[charsWritten-2] =
'\0';
365 const int charsWritten =
swprintf_s(buf, max_chars, L
"%d (%ls)", err, message);
366 ENSURE(charsWritten != -1);
373 wchar_t pathnameBuf[32768];
375 const DWORD charsWritten = GetModuleFileNameW(hModule, pathnameBuf, length);
376 if(charsWritten == 0)
378 ENSURE(charsWritten < length);
379 pathname = pathnameBuf;
386 MEMORY_BASIC_INFORMATION mbi;
387 const SIZE_T bytesWritten = VirtualQuery(addr, &mbi,
sizeof(mbi));
390 ENSURE(bytesWritten >=
sizeof(mbi));
406 wchar_t usernameBuf[256];
408 if(!GetUserNameW(usernameBuf, &size))
418 if(msg == BFFM_INITIALIZED)
420 const WPARAM wParam = TRUE;
422 (void)SendMessage(hWnd, BFFM_SETSELECTIONW, wParam, lpData);
431 const HRESULT hr = CoInitialize(0);
432 ENSURE(hr == S_OK || hr == S_FALSE);
436 memset(&bi, 0,
sizeof(bi));
437 bi.ulFlags = BIF_RETURNONLYFSDIRS|BIF_NEWDIALOGSTYLE|BIF_NONEWFOLDERBUTTON;
441 bi.lParam = (LPARAM)initialPath.c_str();
442 const LPITEMIDLIST pidl = SHBrowseForFolderW(&bi);
447 wchar_t pathBuf[MAX_PATH];
448 const BOOL ok = SHGetPathFromIDListW(pidl, pathBuf);
468 HINSTANCE r = ShellExecuteA(NULL,
"open", url.c_str(), NULL, NULL, SW_SHOWNORMAL);
469 if ((
int)(intptr_t)r > 32)
478 HCRYPTPROV hCryptProv = 0;
479 if(!CryptAcquireContext(&hCryptProv, 0, 0, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
482 memset(buffer, 0, size);
483 if(!CryptGenRandom(hCryptProv, (
DWORD)size, (BYTE*)buffer))
486 if(!CryptReleaseContext(hCryptProv, 0))
493 #if CONFIG_ENABLE_BOOST
503 static std::wstring parse_proxy(
const std::wstring&
input)
505 if(input.find(
'=') == input.npos)
508 std::vector<std::wstring> parts;
509 split(parts, input, boost::algorithm::is_any_of(
"; \t\r\n"), boost::algorithm::token_compress_on);
511 for(
size_t i = 0; i < parts.size(); ++i)
512 if(boost::algorithm::starts_with(parts[i],
"http="))
513 return parts[i].substr(5);
521 WINHTTP_AUTOPROXY_OPTIONS autoProxyOptions;
522 memset(&autoProxyOptions, 0,
sizeof(autoProxyOptions));
523 autoProxyOptions.dwFlags = WINHTTP_AUTOPROXY_AUTO_DETECT;
524 autoProxyOptions.dwAutoDetectFlags = WINHTTP_AUTO_DETECT_TYPE_DHCP | WINHTTP_AUTO_DETECT_TYPE_DNS_A;
525 autoProxyOptions.fAutoLogonIfChallenged = TRUE;
527 WINHTTP_PROXY_INFO proxyInfo;
528 memset(&proxyInfo, 0,
sizeof(proxyInfo));
530 WINHTTP_CURRENT_USER_IE_PROXY_CONFIG ieConfig;
531 memset(&ieConfig, 0,
sizeof(ieConfig));
533 HINTERNET hSession = NULL;
539 if(WinHttpGetIEProxyConfigForCurrentUser(&ieConfig))
541 if(ieConfig.lpszAutoConfigUrl)
544 useAutoDetect =
true;
545 autoProxyOptions.dwFlags |= WINHTTP_AUTOPROXY_CONFIG_URL;
546 autoProxyOptions.lpszAutoConfigUrl = ieConfig.lpszAutoConfigUrl;
551 useAutoDetect = (ieConfig.fAutoDetect == TRUE);
557 useAutoDetect =
true;
562 hSession = WinHttpOpen(NULL, WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
563 WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);
565 if(hSession && WinHttpGetProxyForUrl(hSession, url.c_str(), &autoProxyOptions, &proxyInfo) && proxyInfo.lpszProxy)
567 proxy = parse_proxy(proxyInfo.lpszProxy);
577 if(ieConfig.lpszProxy)
579 proxy = parse_proxy(ieConfig.lpszProxy);
588 if(ieConfig.lpszProxy)
589 GlobalFree(ieConfig.lpszProxy);
590 if(ieConfig.lpszProxyBypass)
591 GlobalFree(ieConfig.lpszProxyBypass);
592 if(ieConfig.lpszAutoConfigUrl)
593 GlobalFree(ieConfig.lpszAutoConfigUrl);
594 if(proxyInfo.lpszProxy)
595 GlobalFree(proxyInfo.lpszProxy);
596 if(proxyInfo.lpszProxyBypass)
597 GlobalFree(proxyInfo.lpszProxyBypass);
599 WinHttpCloseHandle(hSession);
609 const std::wstring wmode(mode, mode+strlen(mode));
610 (void)_wfopen_s(&f,
OsString(pathname).c_str(), wmode.c_str());
std::wstring sys_get_user_name()
Get the current user's login name.
static const size_t ANCHOR_LEFT
#define UNUSED(param)
mark a function parameter as unused and avoid the corresponding compiler warning. ...
static int CALLBACK BrowseCallback(HWND hWnd, unsigned int msg, LPARAM lParam, LPARAM lpData)
Status sys_clipboard_set(const wchar_t *text)
some WinAPI functions SetLastError(0) on success, which is bad because it can hide previous errors...
HMODULE wutil_LibModuleHandle()
static void dlg_ResizeControl(HWND hDlg, int dlgItem, int dx, int dy, size_t anchors)
enable the Suppress button.
static void dlg_OnSize(HWND hDlg, UINT state, int clientSizeX, int clientSizeY)
static void dlg_OnSysCommand(HWND hDlg, UINT cmd, int x, int y)
static void dlg_OnMove(HWND hDlg, int x, int y)
ErrorReactionInternal sys_display_error(const wchar_t *text, size_t flags)
show the error dialog.
Status sys_get_proxy_config(const std::wstring &url, std::wstring &proxy)
get the proxy address for accessing the given HTTP URL.
static const size_t ANCHOR_ALL
int swprintf_s(wchar_t *buf, size_t max_chars, const wchar_t *fmt,...) WPRINTF_ARGS(3)
static INT_PTR CALLBACK dlg_OnMessage(HWND hDlg, unsigned int msg, WPARAM wParam, LPARAM lParam)
int wcscpy_s(wchar_t *dst, size_t max_dst_chars, const wchar_t *src)
static POINTS dlg_prevClientSize
FILE * sys_OpenFile(const OsPath &pathname, const char *mode)
open a file like with fopen (but taking an OsPath argument).
#define ENSURE(expr)
ensure the expression <expr> evaluates to non-zero.
exit the program immediately.
Status sys_StatusDescription(int err, wchar_t *buf, size_t max_chars)
describe the current OS error state.
static Status GetModulePathname(HMODULE hModule, OsPath &pathname)
Status sys_open_url(const std::string &url)
Open the user's default web browser to the given URL.
static POINTS dlg_clientOrigin
void sys_display_msg(const wchar_t *caption, const wchar_t *msg)
display a message.
void debug_DisplayMessage(const wchar_t *caption, const wchar_t *msg)
translates and displays the given strings in a dialog.
Status sys_pick_directory(OsPath &path)
Have the user choose a directory via OS dialog.
Status sys_get_module_filename(void *addr, OsPath &pathname)
determine filename of the module to whom an address belongs.
static void dlg_OnGetMinMaxInfo(HWND hDlg, LPMINMAXINFO mmi)
std::wstring sys_WideFromArgv(const char *argv_i)
i64 Status
Error handling system.
Status sys_generate_random_bytes(u8 *buf, size_t count)
generate high-quality random bytes.
static const size_t ANCHOR_RIGHT
bool sys_IsDebuggerPresent()
ignore and do not report again.
OsPath sys_ExecutablePathname()
#define WARN_RETURN(status)
static void dlg_OnCommand(HWND hDlg, int id, HWND hWndCtl, UINT codeNotify)
static BOOL dlg_OnInitDialog(HWND hDlg, HWND hWndFocus, LPARAM lParam)
static const size_t ANCHOR_BOTTOM
static const size_t ANCHOR_TOP
static std::string OsString(const OsPath &path)
ErrorReactionInternal
all choices offered by the error dialog.