Click here to Skip to main content
Licence 
First Posted 11 Feb 2003
Views 302,018
Bookmarked 51 times

Change Internet Proxy settings

By onega | 11 Feb 2003
Shows code snippets to programmatically change IE proxy settings
4 votes, 22.2%
1

2
2 votes, 11.1%
3
1 vote, 5.6%
4
11 votes, 61.1%
5
4.19/5 - 18 votes
μ 3.89, σa 2.96 [?]

Introduction

Changing proxy settings of IE is a frequent requirement of mine. Then I got the idea of writing a tool by myself, at last. I have not found clear instructions on this. Many articles recommend to modify registry directly, but unfortunately their instruction is not enough. Most of them direct me to modify the following values in registry :-

[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings]
"ProxyEnable"=dword:00000001
"ProxyServer"=":"
"ProxyOverride"=""
"DisablePasswordCaching"=dword:00000001

Details

I tested it and find that it does not work at least on my computer.( I access internet by ADSL connection.) So I backed up registry and modified proxy settings via Internet Explorer, finding that [HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Connections] is also changed. So I wrote the following code snippet to change proxy settings:

void ShowError(long lerr)
{
    LPVOID lpMsgBuf;
    if (!FormatMessage( 
        FORMAT_MESSAGE_ALLOCATE_BUFFER | 
        FORMAT_MESSAGE_FROM_SYSTEM | 
        FORMAT_MESSAGE_IGNORE_INSERTS,
        NULL,
        lerr,
        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
        (LPTSTR) &lpMsgBuf,
        0,
        NULL ))
    {
        return;
    }
    MessageBox( NULL, (LPCTSTR)lpMsgBuf, "Error", MB_OK | MB_ICONINFORMATION );
    LocalFree( lpMsgBuf );
}
void CieproxyDlg::OnBnClickedOk()
{//set proxy server
    UpdateData();
    GetDlgItemText(IDC_EDIT1,m_sIEProxy);
    HKEY hk;
    LONG lret=RegOpenKeyEx(HKEY_CURRENT_USER,
        "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings",
        NULL,KEY_WRITE|KEY_SET_VALUE,&hk);
    if(lret==ERROR_SUCCESS&&NULL!=hk)
    {
        TCHAR* pbuf=m_sIEProxy.GetBuffer(1);
        lret=RegSetValueEx( hk,"ProxyServer",NULL,REG_SZ,pbuf,m_sIEProxy.GetLength());
        DWORD dwenable=1;
        lret=RegSetValueEx(hk,"ProxyEnable",NULL,REG_DWORD,
           (LPBYTE)&dwenable,sizeof(dwenable));
        RegCloseKey(hk);
    }
    const TCHAR* keyname3=_T(
      "software\\Microsoft\\windows\\currentversion\\Internet Settings\\Connections");
    lret=RegOpenKeyEx(HKEY_CURRENT_USER,keyname3,NULL,
        KEY_READ|KEY_WRITE|KEY_SET_VALUE,&hk);
    if(lret==ERROR_SUCCESS&&NULL!=hk)
    {
        DWORD dwtype;
        char pbuf[256];
        DWORD dwlen=sizeof(pbuf);
        const char* valname="Connection to adsl3";
        lret=RegQueryValueEx(hk,valname,NULL,&dwtype,pbuf,&dwlen);
        if(lret!=ERROR_SUCCESS)
        {
            ShowError(lret);
        }
        pbuf[8] = 3;//enable proxy
        pbuf[4]=pbuf[4]+1;
        const char* p=m_sIEProxy.GetBuffer(1);
        memcpy(pbuf+16,p,m_sIEProxy.GetLength());
        char c=0;
        for(int i=m_sIEProxy.GetLength();i<20;i++)
            pbuf[16+i]=c;
        m_sIEProxy.ReleaseBuffer();
        lret=RegSetValueEx(hk,valname,NULL,REG_BINARY,pbuf,dwlen);
        RegCloseKey(hk);
    }
    DWORD dwret;
    SendMessageTimeout(HWND_BROADCAST,WM_SETTINGCHANGE,NULL,NULL,
        SMTO_NORMAL,1000,&dwret);
}

void CieproxyDlg::OnBnClickedDisableProxy()
{
    UpdateData();
    GetDlgItemText(IDC_EDIT1,m_sIEProxy);
    HKEY hk;
    LONG lret=RegOpenKeyEx(HKEY_CURRENT_USER,
        "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings",
        NULL,KEY_WRITE|KEY_SET_VALUE,&hk);
    if(lret==ERROR_SUCCESS&&NULL!=hk)
    {
        DWORD dwenable=0;
        lret=RegSetValueEx(hk,"ProxyEnable",NULL,REG_DWORD,
            (LPBYTE)&dwenable,sizeof(dwenable));
        RegCloseKey(hk);
    }
    const TCHAR* keyname3=_T(
      "software\\Microsoft\\windows\\currentversion\\Internet Settings\\Connections");
    lret=RegOpenKeyEx(HKEY_CURRENT_USER,keyname3,
        NULL,KEY_READ|KEY_WRITE|KEY_SET_VALUE,&hk);
    if(lret==ERROR_SUCCESS&&NULL!=hk)
    {
        DWORD dwtype;
        char pbuf[256];
        DWORD dwlen=sizeof(pbuf);
        const char* valname="Connection to adsl3";
        lret=RegQueryValueEx(hk,valname,NULL,&dwtype,pbuf,&dwlen);
        if(lret!=ERROR_SUCCESS)
        {
            ShowError(lret);
        }
        pbuf[8] = 1;//enable proxy
        pbuf[4]=pbuf[4]+1;
        lret=RegSetValueEx(hk,valname,NULL,REG_BINARY,pbuf,dwlen);
        RegCloseKey(hk);
    }
    DWORD dwret;
    SendMessageTimeout(HWND_BROADCAST,WM_SETTINGCHANGE,NULL,NULL,SMTO_NORMAL,
        1000,&dwret);
}

Problem with above code is that existing Internet Explorer instances don't know the change of settings. What is more, changing registry directly is not an elegant method. Then the following must be more attractive :

BOOL SetConnectionOptions(LPCTSTR conn_name,LPCTSTR proxy_full_addr)
{
    //conn_name: active connection name. 
    //proxy_full_addr : eg "210.78.22.87:8000"
    INTERNET_PER_CONN_OPTION_LIST list;
    BOOL    bReturn;
    DWORD   dwBufSize = sizeof(list);
    // Fill out list struct.
    list.dwSize = sizeof(list);
    // NULL == LAN, otherwise connectoid name.
    list.pszConnection = conn_name;
    // Set three options.
    list.dwOptionCount = 3;
    list.pOptions = new INTERNET_PER_CONN_OPTION[3];
    // Make sure the memory was allocated.
    if(NULL == list.pOptions)
    {
        // Return FALSE if the memory wasn't allocated.
        OutputDebugString("failed to allocat memory in SetConnectionOptions()");
        return FALSE;
    }
    // Set flags.
    list.pOptions[0].dwOption = INTERNET_PER_CONN_FLAGS;
    list.pOptions[0].Value.dwValue = PROXY_TYPE_DIRECT |
        PROXY_TYPE_PROXY;

    // Set proxy name.
    list.pOptions[1].dwOption = INTERNET_PER_CONN_PROXY_SERVER;
    list.pOptions[1].Value.pszValue = proxy_full_addr;//"http://proxy:80";

    // Set proxy override.
    list.pOptions[2].dwOption = INTERNET_PER_CONN_PROXY_BYPASS;
    list.pOptions[2].Value.pszValue = "local";

    // Set the options on the connection.
    bReturn = InternetSetOption(NULL,
        INTERNET_OPTION_PER_CONNECTION_OPTION, &list, dwBufSize);

    // Free the allocated memory.
    delete [] list.pOptions;
    InternetSetOption(NULL, INTERNET_OPTION_SETTINGS_CHANGED, NULL, 0);
    InternetSetOption(NULL, INTERNET_OPTION_REFRESH , NULL, 0);
    return bReturn;
}
BOOL DisableConnectionProxy(LPCTSTR conn_name)
{
    //conn_name: active connection name. 
    INTERNET_PER_CONN_OPTION_LIST list;
    BOOL    bReturn;
    DWORD   dwBufSize = sizeof(list);
    // Fill out list struct.
    list.dwSize = sizeof(list);
    // NULL == LAN, otherwise connectoid name.
    list.pszConnection = conn_name;
    // Set three options.
    list.dwOptionCount = 1;
    list.pOptions = new INTERNET_PER_CONN_OPTION[list.dwOptionCount];
    // Make sure the memory was allocated.
    if(NULL == list.pOptions)
    {
        // Return FALSE if the memory wasn't allocated.
        OutputDebugString("failed to allocat memory in DisableConnectionProxy()");
        return FALSE;
    }
    // Set flags.
    list.pOptions[0].dwOption = INTERNET_PER_CONN_FLAGS;
    list.pOptions[0].Value.dwValue = PROXY_TYPE_DIRECT  ;
    // Set the options on the connection.
    bReturn = InternetSetOption(NULL,
        INTERNET_OPTION_PER_CONNECTION_OPTION, &list, dwBufSize);
    // Free the allocated memory.
    delete [] list.pOptions;
    InternetSetOption(NULL, INTERNET_OPTION_SETTINGS_CHANGED, NULL, 0);
    InternetSetOption(NULL, INTERNET_OPTION_REFRESH , NULL, 0);
    return bReturn;
}

The usage is very straightforward:

//set proxy
    const char* connection_name="Connection to adsl3";
    SetConnectionOptions(connection_name,"62.81.236.23:80");
//disable proxy 
    DisableConnectionProxy(connection_name);
    

Existing Internet Explorer instances are notified by INTERNET_OPTION_SETTINGS_CHANGED and INTERNET_OPTION_REFRESH

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

onega

Web Developer

China China

Member


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralCool ! And a helpfull tip. PinmemberETA9:44 11 Sep '08  
QuestionHow to set "Enable integrated windows authentication" Pinmembervishnuteja3:03 18 Apr '07  
GeneralHelp needed PinmemberKrishhh2:43 16 Apr '07  
GeneralINTERNET_PER_CONN_OPTION' : undeclared identifier PinmemberShashikant_200623:09 27 Apr '06  
GeneralRe: INTERNET_PER_CONN_OPTION' : undeclared identifier Pinmemberchristsiav6:50 26 Jun '06  
GeneralRe: INTERNET_PER_CONN_OPTION' : undeclared identifier Pinmemberchristsiav7:12 26 Jun '06  
GeneralRe: INTERNET_PER_CONN_OPTION' : undeclared identifier Pinmemberalboori2:49 27 Jul '06  
GeneralRe: INTERNET_PER_CONN_OPTION' : undeclared identifier Pinmemberdekdimaya22:49 10 Sep '06  
GeneralRe: INTERNET_PER_CONN_OPTION' : undeclared identifier Pinmemberaren37216:24 16 Sep '06  
QuestionHello, can someone help me here? PinmemberShimshonthegever3:02 4 Apr '06  
QuestionProblem with ProxyServer value Pinsussexpander2k1:49 25 Sep '05  
AnswerRe: Problem with ProxyServer value PinmemberOZambrano13:21 17 Dec '08  
Generalschool blockers Pinsussshwoggie3:58 16 Sep '05  
QuestionHow to get the default (active) connection name directly from the registry? PinmemberAirstriker2:59 18 May '05  
AnswerRe: How to get the default (active) connection name directly from the registry? PinmemberAirstriker3:25 18 May '05  
GeneralRe: How to get the default (active) connection name directly from the registry? Pinmemberchristsiav6:25 26 Jun '06  
GeneralRe: How to get the default (active) connection name directly from the registry? PinmemberAirstriker4:06 11 Sep '06  
GeneralRe: How to get the default (active) connection name directly from the registry? Pinmembertaxtdod21:02 2 Dec '06  
Generalyou went a little overboard PinmemberPyro Joe5:01 22 Apr '05  
GeneralGet it to work in vc++ PinmemberDimitris Vassiliades23:26 26 Mar '05  
GeneralSolution to get it to work in vc++ Pinmemberspdr8708:54 27 Aug '04  
Generalspecific rules for ProxyOvveride Pinmembertyounsi9:13 11 Jul '04  
Generalcompiling problem!! PinsussMayrit10:46 31 May '04  
Generalcompiling problem!! PinsussAnonymous10:37 31 May '04  
QuestionWhat about different browsers? PinsussAnonymous10:36 28 Apr '04  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120209.1 | Last Updated 12 Feb 2003
Article Copyright 2003 by onega
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid