Select the product you need help with
- Internet Explorer
- Windows Phone
- More products
How to programmatically query and set proxy settings under Internet Explorer
Article ID: 226473 - View products that this article applies to.
This article was previously published under Q226473
The article explains the steps neccessary to programmatically query and set the proxy setting information for Internet Explorer.
Under Internet Explorer 4.x and earlier, the InternetSetOption and InternetQueryOption APIs are used with the INTERNET_OPTION_PROXY flag. While this option will still work under Internet Explorer 5, multiple connection options have been introduced in the new version. Given this, the INTERNET_OPTION_PROXY flag will return only the "static" proxy server setting. The static option is the proxy server information stored under the HKEY_CURRENT_USER hive much the same way it was under Internet Explorer 4.0
Note INTERNET_OPTION_PROXY does not permanently change the settings. It does this for the current process only when a NULL handle is used. However, it can also change the settings on a per session basis if a valid session handle is sent in (session handles are obtained using the InternetOpen() API).
If under Internet Explorer 5, you specified a different connection option (such as a dial up connection) as the default, it possible that the proxy information you obtain using the INTERNET_OPTION_PROXY flag may be incorrect for the current Internet Explorer session. For this reason, under Internet Explorer 5, it is recommended that the INTERNET_OPTION_PER_CONNECTION_OPTION be used instead.
Note INTERNET_OPTION_PER_CONNECTION_OPTION causes the settings to be changed on a system-wide basis when a NULL handle is used. To correctly reflect global proxy settings, you must call the InternetSetOption function with the INTERNET_OPTION_REFRESH option flag. Or, to set the settings on a per session basis, a valid session handle can be used.
Note It is still possible to change proxy information for the whgke process without affecting global machine settings in Internet Explorer 5 and later versions. To do this, you must call the InternetSetOption function on the handle that is returned by the InternetOpen function by using the INTERNET_OPTION_PER_CONNECTION_OPTION option. The following code example changes the proxy for the whole process even though the hInternet handle is closed and is not used for the actual request.
Under Internet Explorer 4.x, a typical mechanism to query the proxy information would look something like this:
Under Internet Explorer 5, the recommended way is to use code similar to below:
The code above specifies the connection by setting the pszConnection string in the INTERNET_PER_CONN_OPTION_LIST structure. By setting this string to NULL, the configuration information will be retrieved for the default (or LAN) settings.
The first option (Option INTERNET_PER_CONN_AUTOCONFIG_URL) will return the URL specified for auto configuration of the proxy server. The second option (Option INTERNET_PER_CONN_AUTODISCOVERY_FLAG) will detect whether the auto detect option is enabled or not for the connection specified. The third option will determine what combination of flags have been set for this particular connection. The last two options correspond to the same information as retrieved whe INTERNET_OPTION_PROXY was used in Internet Explorer 4.x.
As you also see, the options that can potentially return string values are freed using GlobalFree(). This is because the string buffers are allocated for you by the WININET library, and it's up to the programmer to free up the buffer after using it.
To obtain information for a different connection, simply change the List.pszConnection string to point to the Dial-Up setting entry you're interested it.
Similarly, to set proxy information, you would use the same technique but with InternetSetOption() instead, for example:
Once again, the above sample will change the default (or LAN) settings (List.pszConnection == NULL). In the sample, the "static" proxy server information is changed to "http://myproxy" at port 8080. Similarly you can also change the auto configuration URL:
When you configure Internet Explorer to use a proxy server for HTTP and go direct for other protocols, use the code to follow.
Note The buffer that is passed to INTERNET_PER_CONN_PROXY_SERVER has the settings for each protocol (this code sets only HTTP, and all other protocols use direct connection).
Note The INTERNET_PER_CONN_FLAGS sets the value for both Proxy types (direct and through proxy) in one element of the Option List Array, as shown below in the code.
Note You have to OR these flags in one place. If you place these flags in another List element, they will display correctly (Internet Explorer OPTIONS), but only one of the flags (the first that is set) will work.
In the sample above (again the default or LAN setting), you have to specify the auto proxy configuration URL and set the option flag to enable the auto proxy configuration.
Article ID: 226473 - Last Review: April 10, 2007 - Revision: 4.0