C++/WinInet 更改 Windows 7 代理设置

发布于 2024-08-11 23:38:18 字数 503 浏览 10 评论 0原文

[免责声明:据我所知,这是一个 Windows 7 特定问题]

我有一段代码可以更改 Windows 注册表中的代理设置,然后继续使用以下内容调用 WinInet API:

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

这完全没问题在 XP 和 Vista 中,但是在 Windows 7 中,某些内容显然发生了变化,并且由于某种原因,以前的注册表项被重新注入,导致它无法按预期工作。

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

是否有更好的方法来处理通知系统选项已更改并需要重新加载?我已经在这个问题上搜索了好几天,切换了编译器等,但我所做的一切都没有使它像我在 Windows 7 中所期望的那样工作。

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

阿楠 2024-08-18 23:38:18

FWIW 我最初的问题是没有使用整个 WinInet API 来处理代理设置。答案从一开始就摆在我面前......最终的解决方案可能看起来像这样:

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

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);
流年里的时光 2024-08-18 23:38:18

没有太多信息可供参考,但如果您还没有这样做,您可能想尝试在 HKEY_LOCAL_MACHINE 和 HKEY_CURRENT_USER 中设置键。

如果您仅在 HKEY_CURRENT_USER 中设置它,则它可能是从 HKEY_LOCAL_MACHINE 复制并覆盖的。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文