现在需要通过一个文本框要别人输入代理地址127.0.0.1:80 然后按确定按钮即可实现改变void CIeproxyDlg::OnButtonSet() {
UpdateData(FALSE);
CString str;
this->GetDlgItemText(IDPROXY,str);//获得用户输入的代理 unsigned long nSize = 4096;
char szBuf[4096] = { 0 };
INTERNET_PROXY_INFO* pInfo = (INTERNET_PROXY_INFO*)szBuf;
pInfo->lpszProxy = str;
if(!InternetSetOption(NULL, INTERNET_OPTION_PROXY, pInfo, nSize))
AfxMessageBox(_T("设置代理失败"));}老是设置失败 不知道为什么哦
UpdateData(FALSE);
CString str;
this->GetDlgItemText(IDPROXY,str);//获得用户输入的代理 unsigned long nSize = 4096;
char szBuf[4096] = { 0 };
INTERNET_PROXY_INFO* pInfo = (INTERNET_PROXY_INFO*)szBuf;
pInfo->lpszProxy = str;
if(!InternetSetOption(NULL, INTERNET_OPTION_PROXY, pInfo, nSize))
AfxMessageBox(_T("设置代理失败"));}老是设置失败 不知道为什么哦
{ CString l_just;
l_just.Format("http=%s:%d", ip.LockBuffer(), port);
//下面的代码将注册表项HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\ProxyServer
//的内容取出来
HKEY hKeyIn = HKEY_CURRENT_USER, hKeyOut;
if( ERROR_SUCCESS != RegOpenKeyEx(hKeyIn, "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", 0, KEY_CREATE_LINK | KEY_WRITE | KEY_READ | KEY_NOTIFY, &hKeyOut))
{
return FALSE;
}
ULONG regsize = 0;
if(ERROR_SUCCESS != RegQueryValueEx(hKeyOut, "ProxyServer", NULL, NULL, NULL, ®size))
{
return FALSE;
} LPBYTE pValue = new BYTE[regsize];
memset(pValue, 0x00, regsize); if(ERROR_SUCCESS != RegQueryValueEx(hKeyOut, "ProxyServer", NULL, NULL, pValue, ®size))
{
return FALSE;
} CString oldproxy((char *)pValue);
delete [] pValue;
pValue = NULL; //从注册表中读出来的数据格式为:http=111.111.111.111:80;ftp=222.222.222.222:21;......,
//如果你只想改变http的代理的话, 就只要把其中的111.111.111.111:80换成你想要的代理就行了,
//类似的你可以改变其他的代理. //下面的代码就替换http代理成为参数所指定的代理.
int pos = 0;
//如果没有字符串中没有找到"http="说明用户没有设置http代理,这时候直接加在最前面.
if(-1 == (pos = oldproxy.Find("http=")))
{
pos = 0;
} int pos1 = 0;
if(-1 == (pos1 = oldproxy.Find(";", pos)))
{
pos1 = oldproxy.GetLength();
} oldproxy.Delete(pos, pos1 - pos);
oldproxy.Insert(pos, l_just); if(ERROR_SUCCESS != RegSetValueEx(hKeyOut, "ProxyServer", 0, REG_SZ, (const unsigned char *)oldproxy.LockBuffer(), oldproxy.GetLength() + 1))
{
return FALSE;
} RegCloseKey(hKeyOut);
//使设置生效
if(!InternetSetOption(NULL, INTERNET_OPTION_SETTINGS_CHANGED, NULL, 0))
{
return FALSE;
}
return TRUE;
} 最后在使用此函数的时候不要忘记包含头文件 #include <wininet.h> 和lib: wininet.lib
LPCTSTR data_Set="Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings";
long ret0=(::RegOpenKeyEx(HKEY_CURRENT_USER,data_Set, 0, KEY_WRITE, &hKEY)); if(ret0!=ERROR_SUCCESS)
{
MessageBox("错误: 无法打开有关的hKEY!");
return;
} LPBYTE owner_Get=new BYTE[80];
DWORD type_1=REG_SZ ;
DWORD cbData_1=80;
long ret1=::RegQueryValueEx(hKEY, "ProxyServer", NULL, &type_1, owner_Get, &cbData_1);
if(ret1!=ERROR_SUCCESS)
{
return;
} 用注册表改业出错 老是MessageBox("错误: 无法查询有关注册表信息!");
我这里是有那个键值的===============
Windows Registry Editor Version 5.00[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings]
"User Agent"="Mozilla/4.0 (compatible; MSIE 6.0; Win32)"
"IE5_UA_Backup_Flag"="5.0"
"NoNetAutodial"=dword:00000000
"MigrateProxy"=dword:00000001
"EmailName"="IEUser@"
"AutoConfigProxy"="wininet.dll"
"MimeExclusionListForCache"="multipart/mixed multipart/x-mixed-replace multipart/x-byteranges "
"WarnOnPost"=hex:01,00,00,00
"UseSchannelDirectly"=hex:01,00,00,00
"EnableHttp1_1"=dword:00000001
"PrivacyAdvanced"=dword:00000000
"EnableNegotiate"=dword:00000001
"ProxyEnable"=dword:00000001
"ProxyServer"="10.137.66.13:3128"
"EnableAutodial"=dword:00000000
"PrivDiscUiShown"=dword:00000001
"WarnOnZoneCrossing"=dword:00000000
"GlobalUserOffline"=dword:00000000
"MaxConnectionsPerServer"=dword:0000000a
long ret0=(::RegOpenKeyEx(HKEY_CURRENT_USER,data_Set, 0, KEY_WRITE, &hKEY));
.....
long ret1=::RegQueryValueEx(hKEY, "ProxyServer", NULL, &type_1, owner_Get, &cbData_1);
因为楼是用KEY_WRITE属性打开hKey的,所以RegQueryValueEx(hKey,...)会出错,建议楼主把KEY_WRITE 改为 KEY_WRITE|KEY_READ 试试