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!
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 ]
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;
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;
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.