如题所述
第1个回答 2022-11-19
分类: 电脑/网络 >> 软件 >> 其他软件
问题描述:
估计与IE的配置文件或者注册表某些特定键值有关,谢~
解析:
在具体使用的时候需要增加头文件#include <wini.h>和库 WININET.LIB
程序代码
------------------------------------------------------------------
@ProxyType 代理类型,如HTTP,SOCKS,FTP...
@ProxyAddr 返回代理服务器的地址
@ProxyPort 返回代理服务器的端口
返回值
0: 无错误
-1: 查询IE的代理失败
-2: 无此类型的代理
-3: 其他错误
-4: 无代理设置信息
int GetIEProxy(CString ProxyType,CString &ProxyAddr,int &ProxyPort)
{
unsigned long nSize = 4096;
char szBuf[4096] = { 0 };
Read IE settings
INTERNET_PROXY_INFO* pInfo = (INTERNET_PROXY_INFO*)szBuf;
if(!InterQueryOption(NULL, INTERNET_OPTION_PROXY, pInfo, &nSize))
{
return -1; 查询IE的代理失败!
}
CString strTmp (pInfo->lpszProxy);
if (strTmp.IsEmpty()) return -4;无代理设置信息
int nStart=0,nPos,nCurLen;
CString strProxyType = ProxyType;
strProxyType += "=";
此时的结构如下
socks=192.168.1.100:3080 =192.168.1.100:808 =192.168.1.100:808 gopher=192.168.1.100:808 ftp=192.168.1.100:808
strTmp.MakeUpper();转化为大写
strProxyType.MakeUpper();
nCurLen = strTmp.GetLength();
nPos = strTmp.Find(strProxyType.GetBuffer(0),nStart);
if (nPos>=0)
{
strTmp = strTmp.Right(nCurLen - nPos);
strTmp.TrimLeft();
nCurLen = strTmp.GetLength();
到此结构如下
HTTP=192.168.1.100:808 gopher=192.168.1.100:808 ftp=192.168.1.100:808
获取单独的协议部分
nPos = strTmp.Find(" ",nStart);
if (nPos>=0)
{
strTmp = strTmp.Left(nPos);
strTmp.TrimRight();
nCurLen = strTmp.GetLength();
到此结构如下
HTTP=192.168.1.100:808
取掉协议名称部分
strTmp = strTmp.Right(nCurLen-strProxyType.GetLength());
nCurLen = strTmp.GetLength();
到此结构如下
192.168.1.100:808
nPos = strTmp.Find(":",nStart);
if (nPos>0)必须要大于0
{
ProxyAddr = strTmp.Left(nPos);
strTmp = strTmp.Right(nCurLen-nPos-1);
strTmp.TrimLeft();
strTmp.TrimRight();
ProxyPort = atoi(strTmp);
return 0;
}
else
{
return -3;
}
}
else
{
return -3;
}
}
else
{
return -2;无此类型的代理
}
}
你这个是旧的方法(IE4 and earlier)了,从IE5开始,最好使用
INTERNET_OPTION_PER_CONNECTION_OPTION来Invoke InterQueryOption,详细内容可以参考MS Q226473
如何编程动态改变IE的代理服务器设置, 并且使之马上生效!
用到的关键函数是wini库里面的InterSetOption. msdn里面有对它详细的介绍, 可以自己去看看. 当把参数dwOption设置为INTERNET_OPTION_SETTINGS_CHANGED的时候. 他就会促使IE在下一次打开网页的时候重新到注册表里面去取代理的设置信息. 所以我们就可以先将注册表里面的代理信息更改掉, 然后调用InterSetOption函数, 从而达到使自己想要的代理设置马上生效的目的.
下面的函数可以实现改变IE的代理服务器设置的目的. 里面加了少许的注释以帮助大家理解.
BOOL SetHttpProxy(CString ip, UINT port)
{
CString l_just;
l_just.Format("=%s:%d", ip.LockBuffer(), port);
下面的代码将注册表项HKEY_CURRENT_USER\\Sofare\\Microsoft\\Windows\\CurrentVersion\\Inter Settings\\ProxyServer
的内容取出来
HKEY hKeyIn = HKEY_CURRENT_USER, hKeyOut;
if( ERROR_SUCCESS != RegOpenKeyEx(hKeyIn, "Sofare\\Microsoft\\Windows\\CurrentVersion\\Inter 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;
从注册表中读出来的数据格式为:=111.111.111.111:80;ftp=222.222.222.222:21;......,
如果你只想改变的代理的话, 就只要把其中的111.111.111.111:80换成你想要的代理就行了,
类似的你可以改变其他的代理.
下面的代码就替换代理成为参数所指定的代理.
int pos = 0;
如果没有字符串中没有找到"="说明用户没有设置代理,这时候直接加在最前面.
if(-1 == (pos = oldproxy.Find("=")))
{
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(!InterSetOption(NULL, INTERNET_OPTION_SETTINGS_CHANGED, NULL, 0))
{
return FALSE;
}
return TRUE;
}
最后在使用此函数的时候不要忘记包含头文件 #include <wini.h> 和lib: wini.lib
问题描述:
估计与IE的配置文件或者注册表某些特定键值有关,谢~
解析:
在具体使用的时候需要增加头文件#include <wini.h>和库 WININET.LIB
程序代码
------------------------------------------------------------------
@ProxyType 代理类型,如HTTP,SOCKS,FTP...
@ProxyAddr 返回代理服务器的地址
@ProxyPort 返回代理服务器的端口
返回值
0: 无错误
-1: 查询IE的代理失败
-2: 无此类型的代理
-3: 其他错误
-4: 无代理设置信息
int GetIEProxy(CString ProxyType,CString &ProxyAddr,int &ProxyPort)
{
unsigned long nSize = 4096;
char szBuf[4096] = { 0 };
Read IE settings
INTERNET_PROXY_INFO* pInfo = (INTERNET_PROXY_INFO*)szBuf;
if(!InterQueryOption(NULL, INTERNET_OPTION_PROXY, pInfo, &nSize))
{
return -1; 查询IE的代理失败!
}
CString strTmp (pInfo->lpszProxy);
if (strTmp.IsEmpty()) return -4;无代理设置信息
int nStart=0,nPos,nCurLen;
CString strProxyType = ProxyType;
strProxyType += "=";
此时的结构如下
socks=192.168.1.100:3080 =192.168.1.100:808 =192.168.1.100:808 gopher=192.168.1.100:808 ftp=192.168.1.100:808
strTmp.MakeUpper();转化为大写
strProxyType.MakeUpper();
nCurLen = strTmp.GetLength();
nPos = strTmp.Find(strProxyType.GetBuffer(0),nStart);
if (nPos>=0)
{
strTmp = strTmp.Right(nCurLen - nPos);
strTmp.TrimLeft();
nCurLen = strTmp.GetLength();
到此结构如下
HTTP=192.168.1.100:808 gopher=192.168.1.100:808 ftp=192.168.1.100:808
获取单独的协议部分
nPos = strTmp.Find(" ",nStart);
if (nPos>=0)
{
strTmp = strTmp.Left(nPos);
strTmp.TrimRight();
nCurLen = strTmp.GetLength();
到此结构如下
HTTP=192.168.1.100:808
取掉协议名称部分
strTmp = strTmp.Right(nCurLen-strProxyType.GetLength());
nCurLen = strTmp.GetLength();
到此结构如下
192.168.1.100:808
nPos = strTmp.Find(":",nStart);
if (nPos>0)必须要大于0
{
ProxyAddr = strTmp.Left(nPos);
strTmp = strTmp.Right(nCurLen-nPos-1);
strTmp.TrimLeft();
strTmp.TrimRight();
ProxyPort = atoi(strTmp);
return 0;
}
else
{
return -3;
}
}
else
{
return -3;
}
}
else
{
return -2;无此类型的代理
}
}
你这个是旧的方法(IE4 and earlier)了,从IE5开始,最好使用
INTERNET_OPTION_PER_CONNECTION_OPTION来Invoke InterQueryOption,详细内容可以参考MS Q226473
如何编程动态改变IE的代理服务器设置, 并且使之马上生效!
用到的关键函数是wini库里面的InterSetOption. msdn里面有对它详细的介绍, 可以自己去看看. 当把参数dwOption设置为INTERNET_OPTION_SETTINGS_CHANGED的时候. 他就会促使IE在下一次打开网页的时候重新到注册表里面去取代理的设置信息. 所以我们就可以先将注册表里面的代理信息更改掉, 然后调用InterSetOption函数, 从而达到使自己想要的代理设置马上生效的目的.
下面的函数可以实现改变IE的代理服务器设置的目的. 里面加了少许的注释以帮助大家理解.
BOOL SetHttpProxy(CString ip, UINT port)
{
CString l_just;
l_just.Format("=%s:%d", ip.LockBuffer(), port);
下面的代码将注册表项HKEY_CURRENT_USER\\Sofare\\Microsoft\\Windows\\CurrentVersion\\Inter Settings\\ProxyServer
的内容取出来
HKEY hKeyIn = HKEY_CURRENT_USER, hKeyOut;
if( ERROR_SUCCESS != RegOpenKeyEx(hKeyIn, "Sofare\\Microsoft\\Windows\\CurrentVersion\\Inter 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;
从注册表中读出来的数据格式为:=111.111.111.111:80;ftp=222.222.222.222:21;......,
如果你只想改变的代理的话, 就只要把其中的111.111.111.111:80换成你想要的代理就行了,
类似的你可以改变其他的代理.
下面的代码就替换代理成为参数所指定的代理.
int pos = 0;
如果没有字符串中没有找到"="说明用户没有设置代理,这时候直接加在最前面.
if(-1 == (pos = oldproxy.Find("=")))
{
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(!InterSetOption(NULL, INTERNET_OPTION_SETTINGS_CHANGED, NULL, 0))
{
return FALSE;
}
return TRUE;
}
最后在使用此函数的时候不要忘记包含头文件 #include <wini.h> 和lib: wini.lib