I'm trying to get the IE proxy settings using WinHttp

in the sample, WinHttpGetIEProxyConfigForCurrentUser works as expected, but WinHttpGetProxyForUrl always fails with errocode 87 (ERROR_INVALID_PARAMETER)
The call WinHttpGetProxyForUrl is neccesary with pac/wpad scripts

Anyone has an idea ?
Maybe there's something wrong with the C++ conversion ?

MS link: http://msdn.microsoft.com/en-us/library/aa384122(VS.85).aspx

thanks,

Paul


program sample:
the program has a button and a Memo

uses

procedure ShowInfo(S: string);
begin
 frmMain.Memo1.Lines.Add(S);
end;


function GetProxyForUrl(Url: string;
var Proxy, ProxyBypass, AutoConfigUrl: string): DWord;
var
 HLib: THandle;
 Config: TWinHttpCurrentUserIEProxyConfig;
 AutoProxyOptions: TWinHttpAutoProxyOptions;
 ProxyInfo: TWinHttpProxyInfo;
 fDoAutoFroxy: boolean;
 hSession: HINTERNET;

begin
 fDoAutoFroxy:= false;
 AutoProxyOptions.dwFlags:= 0;
 AutoProxyOptions.dwAutoDetectFlags:= 0;
 AutoProxyOptions.lpvReserved:= nil;
 AutoProxyOptions.dwReserved:= 0;
 Result:= 0;
 HLib := LoadLibrary ('winhttp.dll');
 if HLib = 0 then exit;
 try
   if WinHttpProxy.WinHttpGetIEProxyConfigForCurrentUser(Config) then begin
       // save what we got already
       Proxy:= Config.lpszProxy;
       ProxyBypass:= Config.lpszProxyBypass;
       AutoConfigUrl:= Config.lpszAutoConfigUrl;
                 ShowInfo('ProxyConfigForCurrentUser result:');
                 ShowInfo('    Proxy = ' + Proxy);
                 ShowInfo('    ProxyBypass = ' + ProxyBypass);
                 ShowInfo('    AutoConfigUrl = ' + AutoConfigUrl);
ShowInfo(' AutoDetect = ' + BoolToStr(Config.fAutoDetect));


       if Config.fAutoDetect then begin // IE configured for auto-detect
           AutoProxyOptions.dwFlags:= WINHTTP_AUTOPROXY_AUTO_DETECT;
AutoProxyOptions.dwAutoDetectFlags:= WINHTTP_AUTO_DETECT_TYPE_DHCP +
                                                WINHTTP_AUTO_DETECT_TYPE_DNS_A;
           fDoAutoFroxy:= true;
       end;
       if Config.lpszAutoConfigUrl <> '' then begin // IE uses pac
           AutoProxyOptions.dwFlags:=
             AutoProxyOptions.dwFlags + WINHTTP_AUTOPROXY_CONFIG_URL;
           AutoProxyOptions.lpszAutoConfigUrl:= Config.lpszAutoConfigUrl;
           fDoAutoFroxy:= true;
       end;
   end
   else begin
       // IE proxy config not available - try auto-detection
       AutoProxyOptions.dwFlags:= WINHTTP_AUTOPROXY_AUTO_DETECT;
       AutoProxyOptions.dwAutoDetectFlags:= WINHTTP_AUTO_DETECT_TYPE_DHCP +
                                            WINHTTP_AUTO_DETECT_TYPE_DNS_A;
       fDoAutoFroxy:= true;
   end;

   if fDoAutoFroxy then
     try
       // we need to create a temp internet session
       hSession:= WinHttpOpen(nil,
                              WINHTTP_ACCESS_TYPE_NO_PROXY,
                              nil,
                              nil,
                              0);
                 ShowInfo('Auto Detect');

       // automatically supply client domain credentials
       AutoProxyOptions.fAutoLogonIfChallenged:= true;
       if WinHttpProxy.WinHttpGetProxyForUrl(hSession,
                                             PWideChar(Url),
                                             AutoProxyOptions,
                                             ProxyInfo) then begin
          Proxy:= ProxyInfo.lpszProxy;
       end
       else result:= GetLastError;

     finally
       WinHttpCloseHandle(hSession);
     end;
 finally
   FreeLibrary (HLib);
 end;
end;

procedure TfrmMain.Button1Click(Sender: TObject);
var
 Proxy, ProxyBypass, AutoConfigUrl: string;
 ErrCode: DWord;
begin
ErrCode:= GetProxyForUrl('https://www.microsoft.com/en/us/default.aspx', ProxyBypass, AutoConfigUrl);
   ShowInfo('ErrCode = ' + IntToStr(ErrCode));
end;




//--------------------------------------
//unit with WinHttp const and function calls:

unit WinHttpProxy;

interface

uses Windows;

type
 HINTERNET = Pointer;

 PWinHttpProxyInfo = ^TWinHttpProxyInfo;
 TWinHttpProxyInfo = record
   dwAccessType    : DWORD;    // see WINHTTP_ACCESS_ types below
   lpszProxy       : LPWSTR;   // proxy server list
   lpszProxyBypass : LPWSTR;   // proxy bypass list
 end;

 PWinHttpAutoProxyOptions = ^TWinHttpAutoProxyOptions;
 TWinHttpAutoProxyOptions = record
     dwFlags               : DWORD;
     dwAutoDetectFlags     : DWORD;
     lpszAutoConfigUrl     : LPWSTR; //PWideChar;
     lpvReserved           : Pointer;
     dwReserved            : DWORD;
     fAutoLogonIfChallenged: BOOL;
 end;


 PWinHttpCurrentUserIEProxyConfig = ^TWinHttpCurrentUserIEProxyConfig;
 TWinHttpCurrentUserIEProxyConfig = record
     fAutoDetect       : boolean;
     lpszAutoConfigUrl : LPWSTR; //PWideChar;
     lpszProxy         : LPWSTR; //PWideChar;
     lpszProxyBypass   : LPWSTR; //PWideChar;
 end;

const
 winhttpdll = 'winhttp.dll';

 WINHTTP_ACCESS_TYPE_DEFAULT_PROXY       = 0;
 WINHTTP_ACCESS_TYPE_NO_PROXY            = 1;
 WINHTTP_ACCESS_TYPE_NAMED_PROXY         = 3;

 WINHTTP_NO_PROXY_NAME                   = nil;
 WINHTTP_NO_PROXY_BYPASS                 = nil;

 INTERNET_DEFAULT_PORT                   = 0;
 INTERNET_DEFAULT_HTTP_PORT              = 80;
 INTERNET_DEFAULT_HTTPS_PORT             = 443;
 INTERNET_SCHEME_HTTP                    = (1);
 INTERNET_SCHEME_HTTPS                   = (2);
 WINHTTP_AUTOPROXY_AUTO_DETECT           = $00000001;
 WINHTTP_AUTOPROXY_CONFIG_URL            = $00000002;
 WINHTTP_AUTOPROXY_RUN_OUTPROCESS_ONLY   = $00020000;
 WINHTTP_AUTO_DETECT_TYPE_DHCP           = $00000001;
 WINHTTP_AUTO_DETECT_TYPE_DNS_A          = $00000002;

 WINHTTP_ERROR_BASE = 12000;
 ERROR_WINHTTP_AUTO_PROXY_SERVICE_ERROR = (WINHTTP_ERROR_BASE + 178);
 ERROR_WINHTTP_BAD_AUTO_PROXY_SCRIPT = (WINHTTP_ERROR_BASE + 166);

 { prototypes }

//function WinHttpQueryOption(hInet: HINTERNET; dwOption: DWORD;
//  lpBuffer: Pointer; var lpdwBufferLength: DWORD): BOOL; stdcall;
//  {$EXTERNALSYM WinHttpQueryOption}

//function WinHttpDetectAutoProxyConfigUrl(dwAutoDetectFlags: DWORD;
//  var ppwszAutoConfigUrl: LPWSTR): BOOL; stdcall;
//  {$EXTERNALSYM WinHttpDetectAutoProxyConfigUrl}

function WinHttpGetDefaultProxyConfiguration(
 var pProxyInfo: TWinHttpProxyInfo): BOOL; stdcall;
 {$EXTERNALSYM WinHttpGetDefaultProxyConfiguration}

function WinHttpGetIEProxyConfigForCurrentUser(
 var pProxyInfo: TWinHttpCurrentUserIEProxyConfig): BOOL; stdcall;
 {$EXTERNALSYM WinHttpGetIEProxyConfigForCurrentUser}

function WinHttpGetProxyForUrl(hSession: HINTERNET; lpcwszUrl: PWideChar;
 pAutoProxyOptions: TWinHttpAutoProxyOptions;
 var pProxyInfo: TWinHttpProxyInfo): BOOL; stdcall;
 {$EXTERNALSYM WinHttpGetProxyForUrl}


function WinHttpCheckPlatform: BOOL; stdcall;
 {$EXTERNALSYM WinHttpCheckPlatform}

function WinHttpOpen(pwszUserAgent: PWideChar; dwAccessType: DWORD;
pwszProxyName, pwszProxyBypass: PWideChar; dwFlags: DWORD): HINTERNET; stdcall;
 {$EXTERNALSYM WinHttpOpen}

function WinHttpCloseHandle(hInternet: HINTERNET): BOOL; stdcall;
 {$EXTERNALSYM WinHttpCloseHandle}


implementation


function WinHttpGetDefaultProxyConfiguration; external winhttpdll name 'WinHttpGetDefaultProxyConfiguration'; function WinHttpGetIEProxyConfigForCurrentUser; external winhttpdll name 'WinHttpGetIEProxyConfigForCurrentUser'; function WinHttpCheckPlatform; external winhttpdll name 'WinHttpCheckPlatform'; function WinHttpOpen; external winhttpdll name 'WinHttpOpen'; function WinHttpCloseHandle; external winhttpdll name 'WinHttpCloseHandle'; function WinHttpGetProxyForUrl; external winhttpdll name 'WinHttpGetProxyForUrl';

end.

--
To unsubscribe or change your settings for TWSocket mailing list
please goto http://lists.elists.org/cgi-bin/mailman/listinfo/twsocket
Visit our website at http://www.overbyte.be

Reply via email to