添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

[Disclaimer: this is a Windows 7 specific issue as far as I can tell]

I've got a block of code that changes the proxy settings in the Windows registry, 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 completely fine in XP and Vista, however in Windows 7 something has apparently changed, and for some reason the previous registry keys get injected back in causing it to not work as expected.

If I comment out those two lines of code, the registry values stick, but obviously IE and other applications relying on that proxy information have no idea that the configuration has changed.

Is there a better way to handle notifying the system that the options have changed and need to be reloaded? I have searched for days on this issue, switched compilers, etc., and nothing I do makes it work as I would expect in Windows 7.

FWIW my original problem was not using the entire WinInet API to handle the proxy settings. The answer has been staring me in the face from the beginning... A final solution might look something like:

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);
                Bingo-- poking at the registry directly is unsupported.  One point: You should set the INTERNET_OPTION_PROXY_SETTINGS_CHANGED option instead of INTERNET_OPTION_REFRESH for proper functioning.  You should also OR the PROXY_TYPE_DIRECT in even when you have a fixed proxy server (e.g. use proxyType |= PROXY_TYPE_PROXY instead of what you've got)-- your comment "disables any proxy server" is wrong.  You also probably ought not blindly clear the bypass list like that...
– EricLaw
                Feb 22, 2010 at 2:19
                MSDN on "Setting and Retrieving Internet Options": msdn.microsoft.com/en-us/library/windows/desktop/… and some old KB: support.microsoft.com/kb/226473/en-us
– jave.web
                Nov 27, 2014 at 16:16
                I added proxyOverride settings like Option[2].Value.pszValue = (LPWSTR)L"xx.xx.xx.xx";  However, it didn't work until I wrote "proxyType |= PROXY_TYPE_PROXY;" as @EricLaw wrote. Then proxyOverrides started work properly and proxy start ignoring "xx.xx.xx.xx" ip address, without restarting IE. Thanks guys!
– Artem Zh.
                Oct 10, 2018 at 8:53

There's not much information to go by, but you may want to attempt to set the keys in both HKEY_LOCAL_MACHINE and HKEY_CURRENT_USER if you aren't already doing so.

If you're only setting it in HKEY_CURRENT_USER, it's possible that it's being copied from HKEY_LOCAL_MACHINE and overwritten.

I tried that a while ago, and still came out with the same results. By "not much information to go by" are you implying I should provide more details? I can't really think of much else to provide, besides maybe a copy/paste sample script? – Michael Nov 23, 2009 at 16:04 WinINET will use the settings from HKCU unless you have a group policy specifying that machine settings (HKLM) should be used. That's very rare. – EricLaw Feb 22, 2010 at 2:20

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.