1 /* Copyright (C) 2011 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 <shlobj.h> // pick_dir
33 #include <shellapi.h> // open_url
34 #include <Wincrypt.h>
35 #include <WindowsX.h> // message crackers
36 #include <winhttp.h>
37
38 #include "lib/sysdep/clipboard.h"
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
sys_IsDebuggerPresent()53 bool sys_IsDebuggerPresent()
54 {
55 return (IsDebuggerPresent() != 0);
56 }
57
58
sys_WideFromArgv(const char * argv_i)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
sys_display_msg(const wchar_t * caption,const wchar_t * msg)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
dlg_OnMove(HWND UNUSED (hDlg),int x,int y)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
dlg_ResizeControl(HWND hDlg,int dlgItem,int dx,int dy,size_t anchors)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
dlg_OnSize(HWND hDlg,UINT state,int clientSizeX,int clientSizeY)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
dlg_OnGetMinMaxInfo(HWND UNUSED (hDlg),LPMINMAXINFO mmi)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
dlg_OnInitDialog(HWND hDlg,HWND UNUSED (hWndFocus),LPARAM lParam)183 static BOOL dlg_OnInitDialog(HWND hDlg, HWND UNUSED(hWndFocus), LPARAM lParam)
184 {
185 const DialogParams* params = (const DialogParams*)lParam;
186 HWND hWnd;
187
188 // need to reset for new instance of dialog
189 dlg_clientOrigin.x = dlg_clientOrigin.y = 0;
190 dlg_prevClientSize.x = dlg_prevClientSize.y = 0;
191
192 if(!(params->flags & DE_ALLOW_SUPPRESS))
193 {
194 hWnd = GetDlgItem(hDlg, IDC_SUPPRESS);
195 EnableWindow(hWnd, FALSE);
196 }
197
198 // set fixed font for readability
199 hWnd = GetDlgItem(hDlg, IDC_EDIT1);
200 HGDIOBJ hObj = (HGDIOBJ)GetStockObject(SYSTEM_FIXED_FONT);
201 LPARAM redraw = FALSE;
202 SendMessage(hWnd, WM_SETFONT, (WPARAM)hObj, redraw);
203
204 SetDlgItemTextW(hDlg, IDC_EDIT1, params->text);
205 return TRUE; // set default keyboard focus
206 }
207
208
dlg_OnCommand(HWND hDlg,int id,HWND UNUSED (hWndCtl),UINT UNUSED (codeNotify))209 static void dlg_OnCommand(HWND hDlg, int id, HWND UNUSED(hWndCtl), UINT UNUSED(codeNotify))
210 {
211 switch(id)
212 {
213 case IDC_COPY:
214 {
215 std::vector<wchar_t> buf(128*KiB); // (too big for stack)
216 GetDlgItemTextW(hDlg, IDC_EDIT1, &buf[0], (int)buf.size());
217 sys_clipboard_set(&buf[0]);
218 break;
219 }
220
221 case IDC_CONTINUE:
222 EndDialog(hDlg, ERI_CONTINUE);
223 break;
224 case IDC_SUPPRESS:
225 EndDialog(hDlg, ERI_SUPPRESS);
226 break;
227 case IDC_BREAK:
228 EndDialog(hDlg, ERI_BREAK);
229 break;
230 case IDC_EXIT:
231 EndDialog(hDlg, ERI_EXIT);
232 break;
233
234 default:
235 break;
236 }
237 }
238
239
dlg_OnSysCommand(HWND hDlg,UINT cmd,int UNUSED (x),int UNUSED (y))240 static void dlg_OnSysCommand(HWND hDlg, UINT cmd, int UNUSED(x), int UNUSED(y))
241 {
242 switch(cmd & 0xFFF0) // NB: lower 4 bits are reserved
243 {
244 // [X] clicked -> close dialog (doesn't happen automatically)
245 case SC_CLOSE:
246 EndDialog(hDlg, 0);
247 break;
248
249 default:
250 break;
251 }
252 }
253
254
dlg_OnMessage(HWND hDlg,unsigned int msg,WPARAM wParam,LPARAM lParam)255 static INT_PTR CALLBACK dlg_OnMessage(HWND hDlg, unsigned int msg, WPARAM wParam, LPARAM lParam)
256 {
257 switch(msg)
258 {
259 case WM_INITDIALOG:
260 return HANDLE_WM_INITDIALOG(hDlg, wParam, lParam, dlg_OnInitDialog);
261
262 case WM_SYSCOMMAND:
263 return HANDLE_WM_SYSCOMMAND(hDlg, wParam, lParam, dlg_OnSysCommand);
264
265 case WM_COMMAND:
266 return HANDLE_WM_COMMAND(hDlg, wParam, lParam, dlg_OnCommand);
267
268 case WM_MOVE:
269 return HANDLE_WM_MOVE(hDlg, wParam, lParam, dlg_OnMove);
270
271 case WM_GETMINMAXINFO:
272 return HANDLE_WM_GETMINMAXINFO(hDlg, wParam, lParam, dlg_OnGetMinMaxInfo);
273
274 case WM_SIZE:
275 return HANDLE_WM_SIZE(hDlg, wParam, lParam, dlg_OnSize);
276
277 default:
278 // we didn't process the message; caller will perform default action.
279 return FALSE;
280 }
281 }
282
283
sys_display_error(const wchar_t * text,size_t flags)284 ErrorReactionInternal sys_display_error(const wchar_t* text, size_t flags)
285 {
286 // note: other threads might still be running, crash and take down the
287 // process before we have a chance to display this error message.
288 // ideally we would suspend them all and resume when finished; however,
289 // they may be holding system-wide locks (e.g. heap or loader) that
290 // are potentially needed by DialogBoxParam. in that case, deadlock
291 // would result; this is much worse than a crash because no error
292 // at all is displayed to the end-user. therefore, do nothing here.
293
294 // temporarily remove any pending quit message from the queue because
295 // it would prevent the dialog from being displayed (DialogBoxParam
296 // returns IDOK without doing anything). will be restored below.
297 // notes:
298 // - this isn't only relevant at exit - Windows also posts one if
299 // window init fails. therefore, it is important that errors can be
300 // displayed regardless.
301 // - by passing hWnd=0, we check all windows belonging to the current
302 // thread. there is no reason to use hWndParent below.
303 MSG msg;
304 const BOOL isQuitPending = PeekMessage(&msg, 0, WM_QUIT, WM_QUIT, PM_REMOVE);
305
306 const HINSTANCE hInstance = wutil_LibModuleHandle();
307 LPCWSTR lpTemplateName = MAKEINTRESOURCEW(IDD_DIALOG1);
308 const DialogParams params = { text, flags };
309 // get the enclosing app's window handle. we can't just pass 0 or
310 // the desktop window because the dialog must be modal (if the app
311 // continues running, it may crash and take down the process before
312 // we've managed to show the dialog).
313 const HWND hWndParent = wutil_AppWindow();
314
315 INT_PTR ret = DialogBoxParamW(hInstance, lpTemplateName, hWndParent, dlg_OnMessage, (LPARAM)¶ms);
316
317 if(isQuitPending)
318 PostQuitMessage((int)msg.wParam);
319
320 // failed; warn user and make sure we return an ErrorReactionInternal.
321 if(ret == 0 || ret == -1)
322 {
323 debug_DisplayMessage(L"Error", L"Unable to display detailed error dialog.");
324 return ERI_CONTINUE;
325 }
326 return (ErrorReactionInternal)ret;
327 }
328
329
330 //-----------------------------------------------------------------------------
331 // misc
332 //-----------------------------------------------------------------------------
333
sys_StatusDescription(int user_err,wchar_t * buf,size_t max_chars)334 Status sys_StatusDescription(int user_err, wchar_t* buf, size_t max_chars)
335 {
336 // validate user_err - Win32 doesn't have negative error numbers
337 if(user_err < 0)
338 return ERR::FAIL; // NOWARN
339
340 const DWORD err = user_err? (DWORD)user_err : GetLastError();
341
342 // no one likes to see "The operation completed successfully" in
343 // error messages, so return more descriptive text instead.
344 if(err == 0)
345 {
346 wcscpy_s(buf, max_chars, L"0 (no error code was set)");
347 return INFO::OK;
348 }
349
350 wchar_t message[400];
351 {
352 const LPCVOID source = 0; // ignored (we're not using FROM_HMODULE etc.)
353 const DWORD lang_id = 0; // look for neutral, then current locale
354 va_list* args = 0; // we don't care about "inserts"
355 const DWORD charsWritten = FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM, source, err, lang_id, message, (DWORD)ARRAY_SIZE(message), args);
356 if(!charsWritten)
357 WARN_RETURN(ERR::FAIL);
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';
363 }
364
365 const int charsWritten = swprintf_s(buf, max_chars, L"%d (%ls)", err, message);
366 ENSURE(charsWritten != -1);
367 return INFO::OK;
368 }
369
370
GetModulePathname(HMODULE hModule,OsPath & pathname)371 static Status GetModulePathname(HMODULE hModule, OsPath& pathname)
372 {
373 wchar_t pathnameBuf[32768]; // NTFS limit
374 const DWORD length = (DWORD)ARRAY_SIZE(pathnameBuf);
375 const DWORD charsWritten = GetModuleFileNameW(hModule, pathnameBuf, length);
376 if(charsWritten == 0) // failed
377 WARN_RETURN(StatusFromWin());
378 ENSURE(charsWritten < length); // why would the above buffer ever be exceeded?
379 pathname = pathnameBuf;
380 return INFO::OK;
381 }
382
383
sys_get_module_filename(void * addr,OsPath & pathname)384 Status sys_get_module_filename(void* addr, OsPath& pathname)
385 {
386 MEMORY_BASIC_INFORMATION mbi;
387 const SIZE_T bytesWritten = VirtualQuery(addr, &mbi, sizeof(mbi));
388 if(!bytesWritten)
389 WARN_RETURN(StatusFromWin());
390 ENSURE(bytesWritten >= sizeof(mbi));
391 return GetModulePathname((HMODULE)mbi.AllocationBase, pathname);
392 }
393
394
sys_ExecutablePathname()395 OsPath sys_ExecutablePathname()
396 {
397 WinScopedPreserveLastError s;
398 OsPath pathname;
399 ENSURE(GetModulePathname(0, pathname) == INFO::OK);
400 return pathname;
401 }
402
403
sys_get_user_name()404 std::wstring sys_get_user_name()
405 {
406 wchar_t usernameBuf[256];
407 DWORD size = ARRAY_SIZE(usernameBuf);
408 if(!GetUserNameW(usernameBuf, &size))
409 return L"";
410 return usernameBuf;
411 }
412
413
414 // callback for shell directory picker: used to set starting directory
415 // (for user convenience).
BrowseCallback(HWND hWnd,unsigned int msg,LPARAM UNUSED (lParam),LPARAM lpData)416 static int CALLBACK BrowseCallback(HWND hWnd, unsigned int msg, LPARAM UNUSED(lParam), LPARAM lpData)
417 {
418 if(msg == BFFM_INITIALIZED)
419 {
420 const WPARAM wParam = TRUE; // lpData is a Unicode string, not PIDL.
421 // (MSDN: the return values for both of these BFFM_ notifications are ignored)
422 (void)SendMessage(hWnd, BFFM_SETSELECTIONW, wParam, lpData);
423 }
424
425 return 0;
426 }
427
sys_pick_directory(OsPath & path)428 Status sys_pick_directory(OsPath& path)
429 {
430 // (must not use multi-threaded apartment due to BIF_NEWDIALOGSTYLE)
431 const HRESULT hr = CoInitialize(0);
432 ENSURE(hr == S_OK || hr == S_FALSE); // S_FALSE == already initialized
433
434 // note: bi.pszDisplayName isn't the full path, so it isn't of any use.
435 BROWSEINFOW bi;
436 memset(&bi, 0, sizeof(bi));
437 bi.ulFlags = BIF_RETURNONLYFSDIRS|BIF_NEWDIALOGSTYLE|BIF_NONEWFOLDERBUTTON;
438 // for setting starting directory:
439 bi.lpfn = (BFFCALLBACK)BrowseCallback;
440 const Path::String initialPath = OsString(path); // NB: BFFM_SETSELECTIONW can't deal with '/' separators
441 bi.lParam = (LPARAM)initialPath.c_str();
442 const LPITEMIDLIST pidl = SHBrowseForFolderW(&bi);
443 if(!pidl) // user canceled
444 return INFO::SKIPPED;
445
446 // translate ITEMIDLIST to string
447 wchar_t pathBuf[MAX_PATH]; // mandated by SHGetPathFromIDListW
448 const BOOL ok = SHGetPathFromIDListW(pidl, pathBuf);
449
450 // free the ITEMIDLIST
451 CoTaskMemFree(pidl);
452
453 if(ok == TRUE)
454 {
455 path = pathBuf;
456 return INFO::OK;
457 }
458
459 // Balance call to CoInitialize, which must have been successful
460 CoUninitialize();
461
462 WARN_RETURN(StatusFromWin());
463 }
464
465
sys_open_url(const std::string & url)466 Status sys_open_url(const std::string& url)
467 {
468 HINSTANCE r = ShellExecuteA(NULL, "open", url.c_str(), NULL, NULL, SW_SHOWNORMAL);
469 if ((int)(intptr_t)r > 32)
470 return INFO::OK;
471
472 WARN_RETURN(ERR::FAIL);
473 }
474
475
sys_generate_random_bytes(u8 * buffer,size_t size)476 Status sys_generate_random_bytes(u8* buffer, size_t size)
477 {
478 HCRYPTPROV hCryptProv = 0;
479 if(!CryptAcquireContext(&hCryptProv, 0, 0, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
480 WARN_RETURN(StatusFromWin());
481
482 memset(buffer, 0, size);
483 if(!CryptGenRandom(hCryptProv, (DWORD)size, (BYTE*)buffer))
484 WARN_RETURN(StatusFromWin());
485
486 if(!CryptReleaseContext(hCryptProv, 0))
487 WARN_RETURN(StatusFromWin());
488
489 return INFO::OK;
490 }
491
492
493 #if CONFIG_ENABLE_BOOST
494
495 /*
496 * Given a string of the form
497 * "example.com:80"
498 * or
499 * "ftp=ftp.example.com:80;http=example.com:80;https=example.com:80"
500 * separated by semicolons or whitespace,
501 * return the string "example.com:80".
502 */
parse_proxy(const std::wstring & input)503 static std::wstring parse_proxy(const std::wstring& input)
504 {
505 if(input.find('=') == input.npos)
506 return input;
507
508 std::vector<std::wstring> parts;
509 split(parts, input, boost::algorithm::is_any_of("; \t\r\n"), boost::algorithm::token_compress_on);
510
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);
514
515 // If we got this far, proxies were only set for non-HTTP protocols
516 return L"";
517 }
518
sys_get_proxy_config(const std::wstring & url,std::wstring & proxy)519 Status sys_get_proxy_config(const std::wstring& url, std::wstring& proxy)
520 {
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;
526
527 WINHTTP_PROXY_INFO proxyInfo;
528 memset(&proxyInfo, 0, sizeof(proxyInfo));
529
530 WINHTTP_CURRENT_USER_IE_PROXY_CONFIG ieConfig;
531 memset(&ieConfig, 0, sizeof(ieConfig));
532
533 HINTERNET hSession = NULL;
534
535 Status err = INFO::SKIPPED;
536
537 bool useAutoDetect;
538
539 if(WinHttpGetIEProxyConfigForCurrentUser(&ieConfig))
540 {
541 if(ieConfig.lpszAutoConfigUrl)
542 {
543 // Use explicit auto-config script if specified
544 useAutoDetect = true;
545 autoProxyOptions.dwFlags |= WINHTTP_AUTOPROXY_CONFIG_URL;
546 autoProxyOptions.lpszAutoConfigUrl = ieConfig.lpszAutoConfigUrl;
547 }
548 else
549 {
550 // Use auto-discovery if enabled
551 useAutoDetect = (ieConfig.fAutoDetect == TRUE);
552 }
553 }
554 else
555 {
556 // Can't find IE config settings - fall back to auto-discovery
557 useAutoDetect = true;
558 }
559
560 if(useAutoDetect)
561 {
562 hSession = WinHttpOpen(NULL, WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
563 WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);
564
565 if(hSession && WinHttpGetProxyForUrl(hSession, url.c_str(), &autoProxyOptions, &proxyInfo) && proxyInfo.lpszProxy)
566 {
567 proxy = parse_proxy(proxyInfo.lpszProxy);
568 if(!proxy.empty())
569 {
570 err = INFO::OK;
571 goto done;
572 }
573 }
574 }
575
576 // No valid auto-config; try explicit proxy instead
577 if(ieConfig.lpszProxy)
578 {
579 proxy = parse_proxy(ieConfig.lpszProxy);
580 if(!proxy.empty())
581 {
582 err = INFO::OK;
583 goto done;
584 }
585 }
586
587 done:
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);
598 if(hSession)
599 WinHttpCloseHandle(hSession);
600
601 return err;
602 }
603
604 #endif
605
sys_OpenFile(const OsPath & pathname,const char * mode)606 FILE* sys_OpenFile(const OsPath& pathname, const char* mode)
607 {
608 FILE* f = 0;
609 const std::wstring wmode(mode, mode+strlen(mode));
610 (void)_wfopen_s(&f, OsString(pathname).c_str(), wmode.c_str());
611 return f;
612 }
613