摘选
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),
(LPTSTR) &lpMsgBuf,
0,
NULL ))
{
return;
}
MessageBox( NULL, (LPCTSTR)lpMsgBuf, "Error", MB_OK | MB_ICONINFORMATION );
LocalFree( lpMsgBuf );
}
void CieproxyDlg::OnBnClickedOk()
{
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;
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;
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)
{
INTERNET_PER_CONN_OPTION_LIST list;
BOOL bReturn;
DWORD dwBufSize = sizeof(list);
list.dwSize = sizeof(list);
list.pszConnection = conn_name;
list.dwOptionCount = 3;
list.pOptions = new INTERNET_PER_CONN_OPTION[3];
if(NULL == list.pOptions)
{
OutputDebugString("failed to allocat memory in SetConnectionOptions()");
return FALSE;
}
list.pOptions[0].dwOption = INTERNET_PER_CONN_FLAGS;
list.pOptions[0].Value.dwValue = PROXY_TYPE_DIRECT |
PROXY_TYPE_PROXY;
list.pOptions[1].dwOption = INTERNET_PER_CONN_PROXY_SERVER;
list.pOptions[1].Value.pszValue = proxy_full_addr;
list.pOptions[2].dwOption = INTERNET_PER_CONN_PROXY_BYPASS;
list.pOptions[2].Value.pszValue = "local";
bReturn = InternetSetOption(NULL,
INTERNET_OPTION_PER_CONNECTION_OPTION, &list, dwBufSize);
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)
{
INTERNET_PER_CONN_OPTION_LIST list;
BOOL bReturn;
DWORD dwBufSize = sizeof(list);
list.dwSize = sizeof(list);
list.pszConnection = conn_name;
list.dwOptionCount = 1;
list.pOptions = new INTERNET_PER_CONN_OPTION[list.dwOptionCount];
if(NULL == list.pOptions)
{
OutputDebugString("failed to allocat memory in DisableConnectionProxy()");
return FALSE;
}
list.pOptions[0].dwOption = INTERNET_PER_CONN_FLAGS;
list.pOptions[0].Value.dwValue = PROXY_TYPE_DIRECT ;
bReturn = InternetSetOption(NULL,
INTERNET_OPTION_PER_CONNECTION_OPTION, &list, dwBufSize);
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:
const
char* connection_name="Connection to adsl3";
SetConnectionOptions(connection_name,"62.81.236.23:80");
DisableConnectionProxy(connection_name);
Existing Internet Explorer instances are notified by INTERNET_OPTION_SETTINGS_CHANGED
and INTERNET_OPTION_REFRESH
posted on 2007-01-25 20:06
崛起的程序员 阅读(1276)
评论(0) 编辑 收藏 所属分类:
c/c++