Programmatically change Windows system proxy settings issue

Zhaborovskyy Vitaliy 101 Reputation points
2021-09-03T14:09:04.857+00:00

Hi!

I use a custom HTTPS proxy server with my own root certificate. I add this certificate to the Windows certificates store as Trusted Root and everything works ok but only in the case when I configure system proxy via Windows "Proxy Settings" menu.

In the case when I try to configure system proxy by my custom application I got a certificate validation error. I have to enable system proxy "by hand" via Windows "Proxy Settings" menu to resolve this error.

For proxy configuration I use function like this:

BOOL EnableProxy(LPCSTR addr, UINT port)
{
    INTERNET_PER_CONN_OPTION_LIST option_list;
    INTERNET_PER_CONN_OPTION options[3];

    option_list.dwSize = sizeof(option_list);
    option_list.pszConnection = NULL;
    option_list.dwOptionCount = 3;
    option_list.pOptions = options;

    options[0].dwOption = INTERNET_PER_CONN_FLAGS;
    options[0].Value.dwValue = PROXY_TYPE_DIRECT | PROXY_TYPE_PROXY;

    char proxy_server[128];
    _snprintf_s(proxy_server, sizeof(proxy_server) - 1, "%s:%d", addr, port);

    options[1].dwOption = INTERNET_PER_CONN_PROXY_SERVER;
    options[1].Value.pszValue = proxy_server;

    options[2].dwOption = INTERNET_PER_CONN_PROXY_BYPASS;
    options[2].Value.pszValue = (LPSTR)"localhost";

    if (InternetSetOption(NULL, INTERNET_OPTION_PER_CONNECTION_OPTION, &option_list, option_list.dwSize) == TRUE)
    {
        InternetSetOption(NULL, INTERNET_OPTION_SETTINGS_CHANGED, NULL, 0);
        InternetSetOption(NULL, INTERNET_OPTION_REFRESH, NULL, 0);
        return TRUE;
    }
    else { return FALSE; }
}

So, looks like I missed something important in my application and it can't enable system proxy to work with proxy https server.
Could you help me with this issue?

OS: Windows 10

Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,689 questions
0 comments No comments
{count} votes

Accepted answer
  1. Zhaborovskyy Vitaliy 101 Reputation points
    2021-09-08T08:49:00.453+00:00

    The solution is to use 'http' as schema in proxy address. Windows set the proxy setting for all protocols if the address schema is 'http' and this is the case.

    for example EnableProxy('localhost', 8080) call sets system proxies as

    {'http': 'http://localhost:8080', 'https': 'https://localhost:8080', 'ftp': 'ftp://localhost:8080'}

    and EnableProxy('http://localhost', 8080) call sets system proxies as
    {'http': 'http://localhost:8080'}

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Xiaopo Yang - MSFT 12,726 Reputation points Microsoft Vendor
    2021-09-06T02:15:51.32+00:00

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.