C ++ / WinInet Windows 7 Proxy Settings

[Disclaimer: This is a specific Windows 7 issue, as far as I can tell)

I have a block of code that modifies the proxy settings in the Windows registry, and then proceeds to call the WinInet API with the following:

InternetSetOption(NULL, INTERNET_OPTION_SETTINGS_CHANGED, NULL, 0); InternetSetOption(NULL, INTERNET_OPTION_REFRESH , NULL, 0); 

This is perfectly normal in XP and Vista, however, something seems to have changed in Windows 7, and for some reason, the previous registry keys are injected back, making it not work as expected.

If I comment on these two lines of code, the registry values ​​will stand up, but, obviously, IE and other applications that rely on this proxy information do not know that the configuration has changed.

Is there a better way to deal with notifying the system that the settings have been changed and need to be reloaded? I searched for days on this issue, switched compilers, etc., And none of this does the job as I would expect in Windows 7.

+4
source share
2 answers

FWIW my initial problem was not using the whole WinInet API to process proxy settings. The answer looked right in the face from the very beginning ... The final solution may look something like this:

 LPWSTR proxyName; if (on) { proxyName = L"http=[IPADDRESS:PORT];https=[IPADDRESS:PORT]"; } else { proxyName = 0; } INTERNET_PER_CONN_OPTION_LIST OptionList; INTERNET_PER_CONN_OPTION Option[3]; unsigned long listSize = sizeof(INTERNET_PER_CONN_OPTION_LIST); Option[0].dwOption = INTERNET_PER_CONN_PROXY_SERVER; Option[1].dwOption = INTERNET_PER_CONN_FLAGS; Option[2].dwOption = INTERNET_PER_CONN_PROXY_BYPASS; OptionList.dwSize = sizeof(INTERNET_PER_CONN_OPTION_LIST); OptionList.pszConnection = NULL; OptionList.dwOptionCount = 3; OptionList.dwOptionError = 0; DWORD proxyType = PROXY_TYPE_DIRECT; // this proxy type disables any proxy server if (proxyName) { if (proxyName[0]) { proxyType = PROXY_TYPE_PROXY; // a name has been passed, so choose the correct proxy type for enabling the proxy server } } Option[0].Value.pszValue = (LPWSTR)proxyName; Option[1].Value.dwValue = proxyType; if (on) { Option[2].Value.pszValue = (LPWSTR)L""; } else { Option[2].Value.pszValue = (LPWSTR)L""; } OptionList.pOptions = Option; if (!InternetSetOption(0, INTERNET_OPTION_PER_CONNECTION_OPTION, &OptionList, listSize)) { // handle error } InternetSetOption(0, INTERNET_OPTION_REFRESH, NULL, NULL); 
+5
source

There is not much information, but you can try to set the keys in both HKEY_LOCAL_MACHINE and HKEY_CURRENT_USER if you have not already done so.

If you only set it to HKEY_CURRENT_USER, it is possible that it is copied from HKEY_LOCAL_MACHINE and overwritten.

0
source

All Articles