C ++ / WinInet更改代理设置Windows 7 [英] C++/WinInet Change Proxy Settings Windows 7

查看:157
本文介绍了C ++ / WinInet更改代理设置Windows 7的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[免责声明:这是一个Windows 7的具体问题,就我所知]

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

我有一个代码块, Windows注册表,然后继续调用WinInet API与以下:

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);

这在XP和Vista中是完全正常的,但是在Windows 7中,显然有些改变了,因为以前的注册表项被注入,导致它无法正常工作。

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.

如果我注释掉这两行代码,注册表值,但显然IE和其他依赖该代理信息的应用程序不知道配置已更改。

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.

是否有更好的方法来通知系统选项已更改,需要重新加载?我已经搜索了这个问题的日子,切换编译器等,没有我做的使它的工作,我期望在Windows 7。

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我原来的问题是没有使用整个WinInet API来处理代理设置。答案从一开始就盯着我。最终的解决方案可能如下:

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);

这篇关于C ++ / WinInet更改代理设置Windows 7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆