How can a Delphi application detect the network proxy settings of a Windows PC?
Asked Answered
A

4

10

I have a Delphi application that communicates with web servers on the Internet using the Indy components. Most users of the application have direct Internet connections but some are behind a proxy server of a local network. I don't want to have to ask the users to lookup their proxy server in the Internet Options / Connections / LAN Settings dialog

alt text http://toybase.files.wordpress.com/2008/11/ie-proxy-settings.png

as quite frankly most people won't know or care what this setting is.

Can I get this information via some system calls from a Delphi-7 appplication?

Many thanks!

Alyce answered 6/1, 2010 at 15:4 Comment(0)
B
14

Via WinAPI -- WinHttpGetIEProxyConfigForCurrentUser. You gotta love MS's long WINAPI names ^_^.

After OP edit: You can read from the registry, AFAIR it would be located here :

 [ HKEY_CURRENT_USER/Software/Microsoft/Windows/CurrentVersion/Internet Settings ]
Bacteroid answered 6/1, 2010 at 15:9 Comment(3)
+1 but if I'm not mistaken, that's for IE only, though. Firefox and the other browsers maintain their own Proxy settings.Piston
That's true but I assumed that if the OP want's to get it via system calls, then that's what he's after.Bacteroid
Thank you! Now I know what to search for, I found some code here: coding.derkeiler.com/Archive/Delphi/…Alyce
A
4

The Delphi code for Kornel Kisielewicz's answer:

uses Registry, Windows;

function detectIEProxyServer() : string;
begin
  with TRegistry.Create do
    try
        RootKey := HKEY_CURRENT_USER;
        if OpenKey('\Software\Microsoft\Windows\CurrentVersion\Internet Settings', False) then begin
          Result := ReadString('ProxyServer');
          CloseKey;
        end
        else
          Result := '';
    finally
      Free;
    end;
end;
Alyce answered 6/1, 2010 at 15:5 Comment(1)
NB: TRegistry.ReadString(): "If the Registry entry contains something other than a string, an exception is raised."Alyce
S
3

Here's another method that I use, which doesn't require direct registry access. This works under D2007, but I can't see why it wouldn't work under D7.

uses
  WinInet,
  SysUtils;

function UseIEProxyInfo(var ProxyHost: String; var ProxyPort: Integer): Boolean;
var
  ProxyInfo: PInternetProxyInfo;
  Len: LongWord;
  ProxyDetails: String;
  s2: String;
  i1: Integer;

  procedure RemoveProtocol(var str: string);
  var
    i1 : integer;
  begin
    i1 := PosText('://', str);
    if i1 > 0 then
      Delete(str, 1, i1 + 2);
    i1 := PosText('http=', str);
    if i1 > 0 then begin
      Delete(str, 1, i1 + 4);
      str := SubStr(str, 1, ' ');
    end;
  end;

begin
  Result := False;

  Len := 4096;
  GetMem(ProxyInfo, Len);
  try
    if InternetQueryOption(nil, INTERNET_OPTION_PROXY, ProxyInfo, Len) then
    begin
      if ProxyInfo^.dwAccessType = INTERNET_OPEN_TYPE_PROXY then
      begin
        Result := True;
        ProxyDetails := ProxyInfo^.lpszProxy;

        RemoveProtocol(ProxyDetails);
        s2 := SubStr(ProxyDetails, 2, ':');
        if s2 <> '' then
        begin
          try
            i1 := StrToInt(s2);
          except
            i1 := -1;
          end;

          if i1 <> -1 then
          begin
            ProxyHost := SubStr(ProxyDetails, 1, ':');
            ProxyPort := i1;
          end;
        end;
      end;
    end;
  finally
    FreeMem(ProxyInfo);
  end;
end;
Simulant answered 12/1, 2010 at 21:56 Comment(2)
It would be good to know the definition of your SubStr function, as it is not entirely obvious here.Iodoform
@mj2008: it's a reference to a function in the madStrings.pas unit from the madExcept exception handling library. You can find some online help here: help.madshi.net/StringSub.htmSimulant
P
1

You would have to get the proxy setting from the browser, which could be in several different locations depending on the browser in use.

You might consider looking into Web Proxy Autodiscovery Protocol, which automatically detects proxy settings on a network.

Paly answered 6/1, 2010 at 15:15 Comment(1)
The users will most definitely be using IE :) but thanks for the link!Alyce

© 2022 - 2024 — McMap. All rights reserved.