<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆-88  評論-77  文章-48  trackbacks-0

    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:

    Collapse
    				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);
            constchar* 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;
            constchar* 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);
            constchar* 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 :

    Collapse
    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

    posted on 2007-01-25 20:13 崛起的程序員 閱讀(887) 評論(0)  編輯  收藏 所屬分類: 載選文章
    主站蜘蛛池模板: 国产成人亚洲影院在线观看| 成年女人免费v片| 中文字幕一精品亚洲无线一区| 中文有码亚洲制服av片| 久久精品女人天堂AV免费观看| 亚洲综合色7777情网站777| 一本岛高清v不卡免费一三区| 亚洲精品在线免费看| h视频在线观看免费完整版| 亚洲欧洲日产韩国在线| 99久久免费国产精品特黄| 亚洲精品蜜夜内射| 国产免费69成人精品视频| 免费精品久久久久久中文字幕| 全部免费毛片免费播放| 久久一区二区三区免费| 亚洲国产综合专区电影在线| 中国xxxxx高清免费看视频| 国产成人精品日本亚洲直接| 成熟女人特级毛片www免费| 国产成人亚洲综合无| 亚洲女同成av人片在线观看| 无码日韩精品一区二区免费暖暖| 亚洲美女激情视频| 日韩中文无码有码免费视频 | 日本亚洲精品色婷婷在线影院| 成人无遮挡裸免费视频在线观看 | 亚洲高清最新av网站| 一区二区三区无码视频免费福利| 色播亚洲视频在线观看| 好男人视频在线观看免费看片| 男人和女人高潮免费网站| 亚洲精品私拍国产福利在线| 国产精品无码素人福利免费| 成人片黄网站色大片免费观看APP| 亚洲宅男天堂a在线| 免费在线视频一区| 蜜臀AV免费一区二区三区| 亚洲乱妇老熟女爽到高潮的片| 中文字幕专区在线亚洲| 中文字幕av无码无卡免费|