How to program dynamically change IE's proxy settings and make it right!

zhaozj2021-02-17  51

Recently, many friends are discussing how to change the IE proxy server settings, just a thing I have recently used to use this function. I will share it with you.

The key function for use is the InternetStoption in the Wininet library. There is a detailed introduction to it in the MSDN. You can go to you yourself. When you set the parameter dwoption to Internet_Option_Settings_Changed. He will prompt IE to reach the next time you open the web page The registry will take the agent setting information. So we can first change the proxy information in the registry, then call the InternetSetOption function to achieve the purpose of making the proxy settings you want.

The following functions can enable the purpose of changing the HTTP proxy server settings of IE. Add a little annotation to help you understand.

Bool SethttpProxy (CSTRING IP, UINT Port) {CSTRING L_JUST; L_JUST.FORMAT ("HTTP =% S:% D", IP.LockBuffer (), Port);

// The following code registry key HKEY_CURRENT_USER // Software // Microsoft // Windows // CurrentVersion // Internet Settings // ProxyServer // contents taken out 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, & regsize)) {return FALSE;} LPBYTE pValue = new BYTE [regsize]; memset (pValue, 0x00, regsize);

IF (Error_Success! = RegQueryValueex (HKEYOUT, "Proxyserver", NULL, NULL, PVALUE, & RegSize) {Return False;}

CString OldProxy ((char *) PVALUE); delete [] pValue; pValue = null;

// The data format read from the registry is: http = 111.111.111.111: 80; ftp = 222.222.222.222: 21; ..., // If you only want to change the agent of HTTP, Turn 111.111.111.111:80 to the agent you want, // You can change the other agents.

// The following code will replace the HTTP agent to become the agent specified by the parameter. INT POS = 0; // If there is no "http =" in the string, the user does not set an HTTP agent, then add directly at this time. 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);

/ / Make the settings to take effect if (! InternetSetoption (null, internet_option_settings_changed, null, 0)) {Return False;}

Return True;}

Finally, don't forget to include the header file #include and lib: wininet.lib

If there is any problem with the code, you are welcome to correct. The author's msn: sxxny@hotmail.com

SRJ 2003-11-26

转载请注明原文地址:https://www.9cbs.com/read-29995.html

New Post(0)