0

I need a code to turn Windows Proxy on or off. I tried:

WINHTTP_PROXY_INFO proxyInfo;
proxyInfo.dwAccessType = WINHTTP_ACCESS_TYPE_NO_PROXY;
proxyInfo.lpszProxy = new WCHAR[25] { 0 };
proxyInfo.lpszProxyBypass = new WCHAR[25] { 0 };
WinHttpSetDefaultProxyConfiguration(&proxyInfo)

but it does not turn the proxy off even with administrator privileges.

Is there a winapi code that can do that?

asked Jan 3, 2021 at 6:07

1 Answer 1

0

Turn Windows Proxy on,

#include <Windows.h>
#include <wininet.h>
#pragma comment(lib,"Wininet.lib")
void main()
{
 INTERNET_PER_CONN_OPTION options[2];
 INTERNET_PER_CONN_OPTION_LIST list{};
 list.dwSize = sizeof(list);
 list.dwOptionCount = 1;
 list.pOptions = options;
 options[0].dwOption = INTERNET_PER_CONN_FLAGS_UI;
 // Query the current value
 DWORD listSize = sizeof(list);
 InternetQueryOption(nullptr, INTERNET_OPTION_PER_CONNECTION_OPTION, &list, &listSize);
 options[0].Value.dwValue |= PROXY_TYPE_PROXY; 
 list.dwOptionCount = 2;
 options[1].dwOption = INTERNET_PER_CONN_PROXY_SERVER;
 WCHAR proxy[] = L"192.3.3.6";
 options[1].Value.pszValue = proxy;
 InternetSetOption(nullptr, INTERNET_OPTION_PER_CONNECTION_OPTION, &list, sizeof(list));
 InternetSetOption(nullptr, INTERNET_OPTION_SETTINGS_CHANGED, nullptr, 0);
}

Turn Windows Proxy off,

Modify this line:

options[0].Value.dwValue |= PROXY_TYPE_DIRECT; 
answered Jan 6, 2021 at 2:09
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. options[0].Value.dwValue = PROXY_TYPE_DIRECT; works for turning off. And L"192.168.49.1:9099" can be used to set the port. But still I have problem. I don't want to change the default value of proxy when I turn it on?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.