Scalabium Software

SMReport Autogenerated
Knowledge for your independence'.
Home Delphi and C++Builder tips


#161: How can I read the proxy information?

most tasks that works with internet, requires to read the current proxy settings from computer before connect to remote server.

Today I want to show two way to read this information.

1. to use registry
this is easy and fast way but note that if in future the Microsoft will change a key where proxy stored, you must change your code too:-)

var
  Handle: HKey;
  Buffer: array[0..256] of Char;
  BufSize: Integer;
  DataType: Integer;
begin
  if RegOpenKeyEx(HKEY_CURRENT_USER,
                  'SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings',
                  0, KEY_READ, Handle) = ERROR_SUCCESS then
  begin
    BufSize := SizeOf(Buffer);
    DataType := reg_sz;
    if RegQueryValueEx(Handle, 'ProxyServer', nil, @DataType, @Buffer, @BufSize) = ERROR_SUCCESS then
      ProxyServer := Buffer;

    BufSize := SizeOf(Integer);
    if RegQueryValueEx(Handle, 'ProxyEnable', nil, @DataType, @i, @BufSize) = ERROR_SUCCESS then
      UseProxyServer := (i <> 0);
    RegCloseKey(Handle);
  end;
end;

2. to use WinInet library
this is more correct way to get an information but require IE 3.x (or higher)

var
  ProxyInfo: PInternetProxyInfo;
  Len: LongWord;
begin
  Len := 4096;
  GetMem(ProxyInfo, Len);
  try
    if InternetQueryOption(nil, INTERNET_OPTION_PROXY, ProxyInfo, Len)
then
      if ProxyInfo^.dwAccessType = INTERNET_OPEN_TYPE_PROXY then
      begin
        UseProxyServer := True;
        ProxyServer := ProxyInfo^.lpszProxy
      end
  finally
    FreeMem(ProxyInfo);
  end;
end;

Now if proxy used, we'll receive in ProxyServer variable similar string:

"ftp=proxy.domain.com:8082;gopher=proxy.domain.com:8083;http=proxy.domai n.com:8080;https=proxy.domain.com:8081"

where domain.com is our proxy.

So as you see, now we must parse this string for correct protocol (http/ftp etc) and to get the proxy server and port number. For example:

  if UseProxyServer and (ProxyServer <> '') then
  begin
    i := Pos('http=', ProxyServer);
    if (i > 0) then
    begin
      Delete(ProxyServer, 1, i+5);
      j := Pos(';', ProxyServer);
      if (j > 0) then
        ProxyServer := Copy(ProxyServer, 1, j-1);
    end;

    i := Pos(':', ProxyServer);
    if (i > 0) then
    begin
      ProxyPort := StrToIntDef(Copy(ProxyServer, i+1, Length(ProxyServer)-i), 0);
      ProxyServer := Copy(ProxyServer, 1, i-1)
    end
  end;


Published: April 6, 2004

See also
 
Viewer for MS Outlook Messages
ABA Database Convert
DBLoad
Clarion Viewer
Paradox Password Recovery
ABA Document Convert
SMMsg suite
DBExport tools
SMDBGrid
Paradox ActiveX
 
 


Contact to webmaster

 

Borland Software Code Gear Scalabium Delphi tips

Copyright© 1998-2025, Scalabium Software. All rights reserved.
webmaster@scalabium.com

SMReport Autogenerated