C++/WinInet Change Proxy Settings Windows 7

后端 未结 2 465
执笔经年
执笔经年 2020-12-30 16:27

[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 proceed

相关标签:
2条回答
  • 2020-12-30 17:13

    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);
    
    0 讨论(0)
  • 2020-12-30 17:29

    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.

    0 讨论(0)
提交回复
热议问题