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 = (LPTSTR)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(TEXT("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 = (LPTSTR)proxy_full_addr;//"http://proxy:80"; // Set proxy override.
list.pOptions[2].dwOption = INTERNET_PER_CONN_PROXY_BYPASS;
list.pOptions[2].Value.pszValue = TEXT("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;
}
调用代码: const wchar_t* connection_name=TEXT("Connection to adsl3");
if (SetConnectionOptions(connection_name,TEXT("62.81.236.23:80")))
{
AfxMessageBox(_T("设置IE成功")) ;
}的确是提示设置IE成功,但我打开浏览器-工具-IE选项...-连接-局域网设置-代理服务器 却没看到有任何设置,求解释????在线等.
只是针对WinINet相关的访问设置代理。
http://topic.csdn.net/u/20070325/14/b992ac7e-2077-493e-bc98-b8274b00829e.html
unsigned short proxyport,
BOOL bAutoDetect,
const char* proxyscript)
{
BOOL bRetVal = FALSE; INTERNET_PER_CONN_OPTION_LISTA list;
DWORD dwBufSize = sizeof(list); // Fill out list struct.
list.dwSize = sizeof(list); // NULL == LAN, otherwise connectoid name.
list.pszConnection = NULL; // Set three options.
list.dwOptionCount = 5;
list.pOptions = new INTERNET_PER_CONN_OPTIONA[list.dwOptionCount]; // Make sure the memory was allocated.
if(NULL == list.pOptions)
{
// Return FALSE if the memory wasn't allocated.
return bRetVal;
} // Set flags.
list.pOptions[0].dwOption = INTERNET_PER_CONN_FLAGS;
list.pOptions[0].Value.dwValue = PROXY_TYPE_DIRECT
| PROXY_TYPE_PROXY
| PROXY_TYPE_AUTO_DETECT; // Set proxy name.
char szProxyName[64] = {0};
_snprintf(szProxyName, 64, "%s:%d", proxyserver, proxyport);
list.pOptions[1].dwOption = INTERNET_PER_CONN_PROXY_SERVER;
list.pOptions[1].Value.pszValue = szProxyName; // Set proxy override.
list.pOptions[2].dwOption = INTERNET_PER_CONN_PROXY_BYPASS;
list.pOptions[2].Value.pszValue = ""; // Set auto detect
list.pOptions[3].dwOption = INTERNET_PER_CONN_AUTODISCOVERY_FLAGS;
list.pOptions[3].Value.dwValue = bAutoDetect; // Set auto detect script
list.pOptions[4].dwOption = INTERNET_PER_CONN_AUTOCONFIG_URL;
list.pOptions[4].Value.pszValue = (LPSTR)proxyscript; if(proxyscript != NULL)
{
list.pOptions[0].Value.dwValue |= PROXY_TYPE_AUTO_PROXY_URL;
} // Set the options on the connection.
bRetVal = InternetSetOptionA(NULL,
INTERNET_OPTION_PER_CONNECTION_OPTION, &list, dwBufSize); // Free the allocated memory.
delete [] list.pOptions; return bRetVal;}
参考一下这个帖子,希望对你有帮助~
BOOL SetHttpProxy(CString ip, UINT port)
{
CString l_just;
l_just.Format(TEXT("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, TEXT("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, TEXT("ProxyServer"), NULL, NULL, NULL, ®size))
{
return FALSE;
} LPBYTE pValue = new BYTE[regsize];
memset(pValue, 0x00, regsize); if(ERROR_SUCCESS != RegQueryValueEx(hKeyOut, TEXT("ProxyServer"), NULL, NULL, pValue, ®size))
{
return FALSE;
} CString oldproxy((TCHAR *)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(TEXT("http="))))
{
pos = 0;
} int pos1 = 0;
if(-1 == (pos1 = oldproxy.Find(TEXT(";"), pos)))
{
pos1 = oldproxy.GetLength();
} oldproxy.Delete(pos, pos1 - pos);
oldproxy.Insert(pos, l_just);
if(ERROR_SUCCESS != RegSetValueEx(hKeyOut,TEXT("ProxyServer"), 0, REG_SZ, (const BYTE *)oldproxy.LockBuffer(), oldproxy.GetLength()-1))
{
return FALSE;
} RegCloseKey(hKeyOut);
//使设置生效
InternetSetOption(NULL, INTERNET_OPTION_SETTINGS_CHANGED, NULL, 0) ;
InternetSetOption(NULL, INTERNET_OPTION_REFRESH , NULL, 0); return TRUE;
}原来注册表中ProxyServer的值为bbkav-y:8080
测试代码: CString ip = TEXT("bbkav-y") ;
int port = 8081 ;
if(SetHttpProxy(ip,port) )
{
AfxMessageBox(_T("设置IE成功")) ;
}再次打开注册表,发现值变成bbk了,!
if(ERROR_SUCCESS != RegSetValueEx(hKeyOut,TEXT("ProxyServer"), 0, REG_SZ, (const BYTE *)oldproxy.LockBuffer(), oldproxy.GetLength()-1))
{
return FALSE;
}这里面的RegSetValueEx第五个参数const byte*在unicode和多字节下有什么不同吗?要怎么传入啊。
oldproxy的值调试为http=bbkav-y:8081,怎么写入后就只有个bbk了,在线继续等.