1

我想用 Delphi 代码为 IE 设置代理连接。我测试这段代码:

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;

但它只是更改IE Internet 选项中的LAN 设置

有人有解决方案吗?

附加: 如何检索连接名称列表?

4

1 回答 1

7

Amin,在您的代码中,您正在设置全局代理服务器。每个连接的设置都HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Connections以二进制格式存储在此位置,这种格式没有很好的文档记录,因此您必须弄清楚写入正确数据的格式和位置。

您可以使用该功能代替 Windows 注册表InternetSetOption,检查此示例应用程序,该应用程序更新特定连接的代理配置。

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.
于 2011-01-26T09:41:42.843 回答