其实就一个注册表的简单操作
在HKEY_CURRENT_USER//Software//Microsoft//Windows//CurrentVersion//Internet Settings//ProxyServer中有几个值
ProxyEnable不为0表示当前IE使用代理服务器,ProxyServer表示代理服务器的值,
直接贴代码,代码还有部分不完整处,支持多字节和UNICODE
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; HKEY hKeyOut; if( ERROR_SUCCESS != RegOpenKeyEx(hKeyIn, TEXT("Software//Microsoft//Windows//CurrentVersion//Internet Settings"), 0, KEY_CREATE_LINK | KEY_WRITE | KEY_READ | KEY_NOTIFY, &hKeyOut)) { RegCloseKey(hKeyOut); return FALSE; } ULONG regsize = 0; if(ERROR_SUCCESS != RegQueryValueEx(hKeyOut, TEXT("ProxyServer"), NULL, NULL, NULL, ®size)) { RegCloseKey(hKeyOut); return FALSE; } LPBYTE pValue = new BYTE[regsize]; memset(pValue, 0x00, regsize); if(ERROR_SUCCESS != RegQueryValueEx(hKeyOut, TEXT("ProxyServer"), NULL, NULL, pValue, ®size)) { RegCloseKey(hKeyOut); 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)*sizeof(TCHAR) )) { int Err = GetLastError() ; TRACE1("ERROR:%d/r/n",Err) ; RegCloseKey(hKeyOut); return FALSE; } RegCloseKey(hKeyOut); //使设置生效 InternetSetOption(NULL, INTERNET_OPTION_SETTINGS_CHANGED, NULL, 0) ; InternetSetOption(NULL, INTERNET_OPTION_REFRESH , NULL, 0); return TRUE; } BOOL GetHttpProxy(CString &ip,BOOL &bProxyEnable) { HKEY hKeyIn = HKEY_CURRENT_USER, hKeyOut; if(RegOpenKeyEx(hKeyIn, TEXT("Software//Microsoft//Windows//CurrentVersion//Internet Settings"), 0, KEY_CREATE_LINK | KEY_WRITE | KEY_READ | KEY_NOTIFY, &hKeyOut) != ERROR_SUCCESS ) { return FALSE; } //检测是否已经允许使用代理服务器 ULONG regsize = 0; if(RegQueryValueEx(hKeyOut, TEXT("ProxyEnable"), NULL, NULL, NULL, ®size)!=ERROR_SUCCESS) { return FALSE; } LPBYTE pValue = new BYTE[regsize]; memset(pValue, 0x00, regsize); if(RegQueryValueEx(hKeyOut, TEXT("ProxyEnable"), NULL, NULL, pValue, ®size)!=ERROR_SUCCESS) { return FALSE; } bProxyEnable=(int)*pValue; delete[] pValue; pValue = NULL; /************************************************************/ if(bProxyEnable) { regsize = 0; if(RegQueryValueEx(hKeyOut, TEXT("ProxyServer"), NULL, NULL, NULL, ®size)!=ERROR_SUCCESS) { return FALSE; } pValue = new byte[regsize]; memset(pValue, 0x00, regsize); if(RegQueryValueEx(hKeyOut, TEXT("ProxyServer"), NULL, NULL, pValue, ®size)!=ERROR_SUCCESS) { return FALSE; } ip = (LPTSTR)pValue ; delete[] pValue; pValue = NULL; } else ip=""; RegCloseKey(hKeyOut); return TRUE; } BOOL DelProxy() { HKEY hKeyIn = HKEY_CURRENT_USER, hKeyOut; if(RegOpenKeyEx( hKeyIn, TEXT("Software//Microsoft//Windows//CurrentVersion//Internet Settings"), 0, KEY_CREATE_LINK | KEY_WRITE | KEY_READ | KEY_NOTIFY, &hKeyOut) != ERROR_SUCCESS ) { return FALSE; } ULONG regsize = 0; if(RegQueryValueEx( hKeyOut, TEXT("ProxyEnable"), NULL, NULL, NULL, ®size)!=ERROR_SUCCESS) { return FALSE; } LPBYTE pValue = new BYTE[regsize]; memset(pValue, 0x00, regsize); if(RegQueryValueEx( hKeyOut, TEXT("ProxyEnable"), NULL, NULL, pValue, ®size)!=ERROR_SUCCESS) { return FALSE; } BOOL bEnable=(BOOL)*pValue; delete[] pValue; pValue = NULL; /************************************************************/ if(bEnable) { regsize = 0; if(RegQueryValueEx( hKeyOut, TEXT("ProxyServer"), NULL, NULL, NULL, ®size)!=ERROR_SUCCESS) { return FALSE; } pValue = new BYTE[regsize]; memset(pValue, 0x00, regsize); DWORD dwData=0; if(RegSetValueEx( hKeyOut, TEXT("ProxyEnable"), 0, REG_DWORD, (LPBYTE)&dwData, sizeof(DWORD))!=ERROR_SUCCESS) { return FALSE; } RegCloseKey(hKeyOut); //使设置生效 if(!InternetSetOption(NULL, INTERNET_OPTION_SETTINGS_CHANGED, NULL, 0)) { return FALSE; } } return TRUE; }
RegSetValueEx中最后一个参数大小为byte计算.