8083

What initialization should be made prior to calling InternetGetProxyInfo()?

Question:

I configured internet explorer to use a local PAC file: <img alt="ie proxy settings" class="b-lazy" data-src="https://i.stack.imgur.com/eBkAY.png" data-original="https://i.stack.imgur.com/eBkAY.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" />

It works just fine. But when I try to call InternetGetProxyInfo(), it fails with ERROR_CAN_NOT_COMPLETE. What can be the problem?

#ifndef WINVER // Allow use of features specific to Windows XP or later. #define WINVER 0x0501 // Change this to the appropriate value to target other versions of Windows. #endif #ifndef _WIN32_WINNT // Allow use of features specific to Windows XP or later. #define _WIN32_WINNT 0x0501 // Change this to the appropriate value to target other versions of Windows. #endif #include <winsock2.h> #include <windows.h> #include <wininet.h> #include <tchar.h> #define URL "http://www.yandex.ru/" #define HOST "www.yandex.ru" int _tmain(int argc, _TCHAR* argv[]) { char proxyBuffer[1024]; char *str = proxyBuffer; DWORD nb = 1024; DWORD dw; BOOL b; pfnInternetGetProxyInfo pIGPI; // Function-pointer instance /* code from MSDN: */ { HMODULE hModJS; // Handle for loading the DLL hModJS = LoadLibrary( TEXT("jsproxy.dll") ); if (!hModJS) { _tprintf( TEXT("\nLoadLibrary failed to load jsproxy.dll with error: %d\n"), GetLastError( ) ); return( FALSE ); } pIGPI = (pfnInternetGetProxyInfo) GetProcAddress( hModJS, "InternetGetProxyInfo" ); if (!pIGPI) { _tprintf( TEXT("\nGetProcAddress failed to find InternetGetProxyInfo, error: %d\n"), GetLastError( ) ); return( FALSE ); } // The pIGPI function pointer can now be used to call InternetGetProxyInfo. } InternetInitializeAutoProxyDll(0); /* wininet.dll version of this function */ SetLastError(0); b = pIGPI(URL,sizeof(URL),HOST,sizeof(HOST), &str, &nb); dw = GetLastError(); SetLastError(0); b = pIGPI(URL,sizeof(URL)-1,HOST,sizeof(HOST)-1, &str, &nb); dw = GetLastError(); return 0; }

Please don't tell me to use other API, it's an educational, not practical question.

Answer1:

There are 2 ways:

<ul><li>

easy: call InternetOpenUrl() with a dummy URL to let wininet initialize jsproxy:

#define URL "https://yandex.ru:777" #define HOST "yandex.ru" HINTERNET hInternet = InternetOpen(_T("try-wininet"), INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0); HINTERNET hUrl = InternetOpenUrl(hInternet, _T("http://0.0.0.0"), NULL, 0, 0, 20); InternetCloseHandle(hUrl); InternetCloseHandle(hInternet); InternetGetProxyInfo(URL,sizeof(URL)-1,HOST,sizeof(HOST)-1, &str, &nb); GlobalFree(str); </li> <li>

hard: call InternetInitializeAutoProxyDll() in jsproxy. But to prepare the arguments, you have to do everything yourself: read the settings from the registry, parse them, detect and download the PAC file and provide an implementation of AutoProxyHelperVtbl that contains such functions as GetIPAddress(), IsInNet(), etc.

</li> </ul>

InternetGetProxyInfo() fails when there's no PAC script configured, although some proxy server is set in the bottom of the dialog. In this case you should call InternetQueryOption(NULL, INTERNET_OPTION_PROXY,...)

Recommend

  • Switch case menu
  • unique_ptr with an API that expects raw pointers?
  • Why does referencing break when done through pointers
  • Pantheios write to extenal file
  • Converting _TCHAR* to char*
  • WTSQuerySessionInformation returning empty strings
  • Does _control87() also set the SSE MXCSR Control Register?
  • std::list erase incompatible iterator
  • WinCE: How can I determine the module that contains a code address?
  • How to Fire Windows Service stop event on System restart?
  • x86: Count transitions from 1 to 0 in 32 bit number
  • What does the lower left pane in OllyDbg displays?
  • What is lua_len() alternative in Lua 5.1?
  • Implicit cast from const string to bool [duplicate]
  • std::system Exception when instantiating a singleton object
  • Call a macro with parameters : Python win32com API
  • Get the App path without the app name at the end of the app
  • Getting short path in python
  • How to read piped content in C?
  • Parse a date string in a specific locale (not timezone!)
  • C: Incompatible pointer type initializing
  • Jetty 9 HashLoginService
  • Python urlparse: small issue
  • PostgreSQL Query without WHERE only ORDER BY and LIMIT doesn't use index
  • Read a local file using javascript
  • Syntax for setting draggablecursor property in google maps api
  • Seeking advice on Jetty HttpClient Hang
  • does jqgrid support a multiple checkbox list for editing
  • Optimizing database types to compact database (SQLite)
  • AES padding and writing the ciphertext to a disk file
  • Cross-Platform Protobuf Serialization
  • How to handle AllServersUnavailable Exception
  • Convert array of 8 bytes to signed long in C++
  • Do I've to free mysql result after storing it?
  • 0x202A in filename: Why?
  • Linker errors when using intrinsic function via function pointer
  • Turn off referential integrity in Derby? is it possible?
  • LevelDB C iterator
  • JaxB to read class hierarchy
  • Python/Django TangoWithDjango Models and Databases