views:

611

answers:

4

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

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!

+9  A: 

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 ]
Kornel Kisielewicz
+1 but if I'm not mistaken, that's for IE only, though. Firefox and the other browsers maintain their own Proxy settings.
Pekka
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.
Kornel Kisielewicz
Thank you! Now I know what to search for, I found some code here: http://coding.derkeiler.com/Archive/Delphi/borland.public.delphi.nativeapi/2004-01/0205.html
devstopfix
+1  A: 

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.

Dave Swersky
The users will most definitely be using IE :) but thanks for the link!
devstopfix
A: 

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;
devstopfix
NB: TRegistry.ReadString(): "If the Registry entry contains something other than a string, an exception is raised."
devstopfix
+2  A: 

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;
Conor Boyd