Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

I couldn't get/set IE proxy via function InternetSetOption() and InternetQueryOption()

688 views
Skip to first unread message

Tong Narak

unread,
Jan 23, 2006, 9:23:18 PM1/23/06
to
Hello,

I couldn't get/set IE proxy via function InternetSetOption() and
InternetQueryOption().

I don't know what are wrong ? Thank for advance.

----------------------------------------------------------------------------
-----------------------

unit main;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, StrUtils,
Forms, Dialogs, ExtCtrls, StdCtrls, ComCtrls, WinInet, Wave;

type
TMainForm = class(TForm)
Wave1: TWave;
procedure FormCreate(Sender: TObject);
private
public
end;

var
MainForm : TMainForm;

implementation

{$R *.DFM}

////////////////////////////////////////////////////////////////////////////
////////////

procedure TMainForm.FormCreate(Sender: TObject);
var
ProxyEnabled : bool;
ProxyInfo : INTERNET_PROXY_INFO;
Size : DWord;

begin
Application.ShowMainForm := False; // effect when start only
MainForm.Visible := False;

Size := SizeOf(DWORD);

try
if InternetQueryOption(nil, INTERNET_OPTION_PROXY, @ProxyInfo, Size)
then
begin
//INTERNET_OPEN_TYPE_DIRECT
//INTERNET_OPEN_TYPE_PROXY
//INTERNET_OPEN_TYPE_PRECONFIG
if ProxyInfo.dwAccessType = INTERNET_OPEN_TYPE_DIRECT then
begin
ProxyEnabled := False;
end;
if ProxyInfo.dwAccessType = INTERNET_OPEN_TYPE_PROXY then
begin
ProxyEnabled := True;
end;
end;
finally
end;


if ProxyEnabled then
begin
ProxyInfo.dwAccessType := INTERNET_OPEN_TYPE_DIRECT;
Wave1.WhereWave := wwResource;
Wave1.WaveName := 'ProxyDisable';
Wave1.Play;
end
else
begin
ProxyInfo.dwAccessType := INTERNET_OPEN_TYPE_PROXY;
ProxyInfo.lpszProxy := PChar(ParamStr(1));
Wave1.WhereWave := wwResource;
Wave1.WaveName := 'ProxyEnable';
Wave1.Play;
end;

InternetSetOption(nil, INTERNET_OPTION_PROXY, @ProxyInfo,
SizeOf(ProxyInfo));
//InternetSetOption(nil, INTERNET_OPTION_SETTINGS_CHANGED, nil, 0);

Application.Terminate;
end;

end. // end of program

Liz

unread,
Jan 24, 2006, 1:23:12 AM1/24/06
to
Tong Narak wrote:

> I couldn't get/set IE proxy via function InternetSetOption() and
> InternetQueryOption().

I always just read the registry to read for proxy settings...

--
Liz the Brit
Delphi things I have released: http://www.xcalibur.co.uk/DelphiThings

Tong Narak

unread,
Jan 24, 2006, 4:04:55 AM1/24/06
to
These one is really working for IE 5.x and 6.x

1. Because of delphi7 winInet is old. We need to declare new addition types
and constants. Create new unit such as WinInetAppend.pas

2. Uses WinInetAppend in your Form.

//////////////// WinInetAppend.pas /////////////

unit WinInetAppend;

interface

uses
Windows;

type
PInternetPerConnOption = ^INTERNET_PER_CONN_OPTION;
INTERNET_PER_CONN_OPTION = record
dwOption: DWORD;
case Integer of
0: (dwValue: DWORD);
1: (pszValue:LPTSTR);
2: (ftValue: FILETIME);
end;

PInternetPerConnOptionList = ^INTERNET_PER_CONN_OPTION_LIST;
INTERNET_PER_CONN_OPTION_LIST = record
dwSize :DWORD;
pszConnection :LPTSTR;
dwOptionCount :DWORD;
dwOptionError :DWORD;
pOptions :PInternetPerConnOption;
end;

//
// Options used in INTERNET_PER_CONN_OPTON struct
//
const
INTERNET_PER_CONN_FLAGS = 1;
INTERNET_PER_CONN_PROXY_SERVER = 2;
INTERNET_PER_CONN_PROXY_BYPASS = 3;
INTERNET_PER_CONN_AUTOCONFIG_URL = 4;
INTERNET_PER_CONN_AUTODISCOVERY_FLAGS = 5;

//
// PER_CONN_FLAGS
//
PROXY_TYPE_DIRECT = $00000001; // direct to net
PROXY_TYPE_PROXY = $00000002; // via named proxy
PROXY_TYPE_AUTO_PROXY_URL = $00000004; // autoproxy URL
PROXY_TYPE_AUTO_DETECT = $00000008; // use autoproxy detection

//
// PER_CONN_AUTODISCOVERY_FLAGS
//
AUTO_PROXY_FLAG_USER_SET = $00000001; // user
changed this setting
AUTO_PROXY_FLAG_ALWAYS_DETECT = $00000002; // force
detection even when its not needed
AUTO_PROXY_FLAG_DETECTION_RUN = $00000004; // detection
has been run
AUTO_PROXY_FLAG_MIGRATED = $00000008; // migration
has just been done
AUTO_PROXY_FLAG_DONT_CACHE_PROXY_RESULT = $00000010; // don't
cache result of host=proxy name
AUTO_PROXY_FLAG_CACHE_INIT_RUN = $00000020; // don't
initalize and run unless URL expired
AUTO_PROXY_FLAG_DETECTION_SUSPECT = $00000040; // if we're
on a LAN & Modem, with only one IP, bad?!?

INTERNET_OPTION_PER_CONNECTION_OPTION = 75;

implementation

end.


///////// This exmaple is Proxy Toggle ////////

unit main;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, StrUtils,
Forms, Dialogs, ExtCtrls, StdCtrls, ComCtrls,

WinInet, WinInetAppend;

type
TMainForm = class(TForm)


procedure FormCreate(Sender: TObject);
private
public
end;

var
MainForm : TMainForm;

implementation

{$R *.DFM}

////////////////////////////////////////////////////////////////////////////
////////////

procedure TMainForm.FormCreate(Sender: TObject);
var
Option : array[0..1] of INTERNET_PER_CONN_OPTION;
List : INTERNET_PER_CONN_OPTION_LIST;


begin
Application.ShowMainForm := False; // effect when start only
MainForm.Visible := False;

////////////////////////////////////////////////////////////////////////

Option[0].dwOption := INTERNET_PER_CONN_FLAGS;
Option[0].dwValue := PROXY_TYPE_DIRECT; // PROXY_TYPE_PROXY
Option[1].dwOption := INTERNET_PER_CONN_PROXY_SERVER;
Option[1].pszValue := PChar(ParamStr(1));

List.dwSize := SizeOf(INTERNET_PER_CONN_OPTION_LIST);
List.pszConnection := nil; // LAN
List.dwOptionCount := 2;
List.dwOptionError := 0;
List.pOptions := @Option;

InternetQueryOption(nil, INTERNET_OPTION_PER_CONNECTION_OPTION, @List,
List.dwSize);

////////////////////////////////////////////////////////////////////////

if Option[0].dwValue = PROXY_TYPE_DIRECT then
begin
Option[0].dwOption := INTERNET_PER_CONN_FLAGS;
Option[0].dwValue := PROXY_TYPE_PROXY;
Option[1].dwOption := INTERNET_PER_CONN_PROXY_SERVER;
Option[1].pszValue := PChar(ParamStr(1));
end
else
begin
Option[0].dwOption := INTERNET_PER_CONN_FLAGS;
Option[0].dwValue := PROXY_TYPE_DIRECT;
Option[1].dwOption := INTERNET_PER_CONN_PROXY_SERVER;
Option[1].pszValue := PChar(ParamStr(1));
end;

List.dwSize := SizeOf(INTERNET_PER_CONN_OPTION_LIST);
List.pszConnection := nil; // LAN
List.dwOptionCount := 2;
List.dwOptionError := 0;
List.pOptions := @Option;

InternetSetOption(nil, INTERNET_OPTION_PER_CONNECTION_OPTION, @List,
List.dwSize);

////////////////////////////////////////////////////////////////////////

Tong Narak

unread,
Jan 24, 2006, 8:21:15 AM1/24/06
to
I forgot one command before Application.terminate;


InternetSetOption(nil, INTERNET_OPTION_SETTINGS_CHANGED, nil, 0);
// InternetSetOption(nil, INTERNET_OPTION_REFRESH , nil, 0);


By the way, MSDN told us to use INTERNET_OPTION_REFRESH but in fact
we have to use INTERNET_OPTION_SETTINGS_CHANGED which is only work!

Note:
INTERNET_OPTION_PER_CONNECTION_OPTION causes the settings to be changed on a
system-wide basis when a NULL handle is used. To correctly reflect global
proxy settings, you must call the InternetSetOption function with the
INTERNET_OPTION_REFRESH option flag. Or, to set the settings on a per
session basis, a valid session handle can be used.


INTERNET_OPTION_SETTINGS_CHANGED
39
Notifies the system that the registry settings have been changed so that it
verifies the settings on the next call to InternetConnect. This is used by
InternetSetOption.


INTERNET_OPTION_REFRESH
37
Causes the proxy data to be reread from the registry for a handle. No buffer
is required. This option can be used on the HINTERNET handle returned by
InternetOpen. It is used by InternetSetOption.


Mike Shkolnik

unread,
Jan 24, 2006, 9:37:30 AM1/24/06
to
> I couldn't get/set IE proxy via function InternetSetOption() and
> InternetQueryOption().
See the sample code at http://www.scalabium.com/faq/dct0161.htm

--
With best regards, Mike Shkolnik
EMail: mshk...@scalabium.com
http://www.scalabium.com


0 new messages