在 delphi 中的活动拨号或 VPN 连接上设置 IE 中的 Internet 代理

发布于 2024-10-14 21:49:36 字数 586 浏览 6 评论 0原文

我想使用 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设置

有人有解决方案吗?

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

桃扇骨 2024-10-21 21:49:36

阿明,在您的代码中您正在设置全局代理服务器。每个连接的设置都以二进制格式存储在 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.

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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文