问题
I want to set proxy in action connection for IE with Delphi codes. I test this code:
Procedure SetProxy(const Server: String);
var
Reg : TRegistry;
begin
Reg := TRegistry.Create;
Reg.OpenKey('Software\Microsoft\Windows\CurrentVersion\Internet Settings',False);
Reg.WriteString('ProxyServer',Server);
Reg.WriteBool('ProxyEnable',True);
Reg.CloseKey;
Reg.Free;
InternetSetOption(0, INTERNET_OPTION_SETTINGS_CHANGED, 0, 0);
end;
But it is change only LAN Setting in IE Internet Option.
Anyone have a solution for that?
Additional: How can I retrieve Connection names list?
回答1:
Amin, in your code you are setting the global proxy server. The setting for each connection is stored in this location HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Connections
in a binary format which is not very well documented, so you must figure out the format and position to write the correct data.
Instead of the windows registry you can use the InternetSetOption function, check this sample application which update the proxy configuration for a particular connection.
program UpdateProxyApp;
{$APPTYPE CONSOLE}
uses
WinInet,
Windows,
SysUtils;
type
INTERNET_PER_CONN_OPTION = record
dwOption: DWORD;
Value: record
case Integer of
1: (dwValue: DWORD);
2: (pszValue: PAnsiChar);
3: (ftValue: TFileTime);
end;
end;
LPINTERNET_PER_CONN_OPTION = ^INTERNET_PER_CONN_OPTION;
INTERNET_PER_CONN_OPTION_LIST = record
dwSize: DWORD;
pszConnection: LPTSTR;
dwOptionCount: DWORD;
dwOptionError: DWORD;
pOptions: LPINTERNET_PER_CONN_OPTION;
end;
const
INTERNET_PER_CONN_FLAGS = 1;
INTERNET_PER_CONN_PROXY_SERVER = 2;
INTERNET_PER_CONN_PROXY_BYPASS = 3;
INTERNET_PER_CONN_AUTOCONFIG_URL = 4;
INTERNET_PER_CONN_AUTODISCOVERY_FLAGS = 5;
PROXY_TYPE_DIRECT = $00000001;
PROXY_TYPE_PROXY = $00000002;
PROXY_TYPE_AUTO_PROXY_URL = $00000004;
PROXY_TYPE_AUTO_DETECT = $00000008;
INTERNET_OPTION_REFRESH = 37;
INTERNET_OPTION_PER_CONNECTION_OPTION = 75;
INTERNET_OPTION_SETTINGS_CHANGED = 39;
function SetConnectionProxy(const conn_name, proxy_addr : AnsiString) : Boolean;
var
list : INTERNET_PER_CONN_OPTION_LIST;
dwBufSize: DWORD;
Options : array[0..2] of INTERNET_PER_CONN_OPTION;
begin
dwBufSize := SizeOf(list);
list.dwSize := dwBufSize;
list.pszConnection := PAnsiChar(conn_name);
list.dwOptionCount := 3;
list.pOptions := @Options;
// Set the flags
Options[0].dwOption := INTERNET_PER_CONN_FLAGS;
Options[0].Value.dwValue := PROXY_TYPE_DIRECT OR PROXY_TYPE_PROXY;
// Set proxy name
Options[1].dwOption := INTERNET_PER_CONN_PROXY_SERVER;
Options[1].Value.pszValue:= PAnsiChar(proxy_addr);
// Set proxy override .
Options[2].dwOption := INTERNET_PER_CONN_PROXY_BYPASS;
Options[2].Value.pszValue:= PAnsiChar('local');
// Set the new settings
Result := InternetSetOption(nil, INTERNET_OPTION_PER_CONNECTION_OPTION, @list, dwBufSize);
InternetSetOption(nil, INTERNET_OPTION_SETTINGS_CHANGED, nil, 0);
InternetSetOption(nil, INTERNET_OPTION_REFRESH , nil, 0);
end;
begin
try
Writeln(Format('Settings updated %s',[BoolToStr(SetConnectionProxy('Your connection Name', '192.168.15.15:80'),True)]));
except
on E:Exception do
Writeln(E.Classname, ': ', E.Message);
end;
Readln;
end.
来源:https://stackoverflow.com/questions/4802121/setting-internet-proxy-in-ie-on-active-dial-up-or-vpn-connection-in-delphi