Solved

Set proxy in IE

Posted on 2008-11-01
7
2,483 Views
Last Modified: 2013-11-23
Hello,
I need two simple procedures for delphi which one of them sets proxy in IE and another one disables the proxy in IE. the proxy should be set for all dial up and LAN connections , not just for LAN , something like this :
SetProxy('localhost:8080');
DisableProxy;
I've done my homework and I think this thread will help a lot :
http://www.experts-exchange.com/Programming/Languages/Pascal/Delphi/Q_21707537.html
seems it should be done with InternetSetOption().
but I dont feel comfortable with windows APIs :D
Regards
0
Comment
Question by:fifthelement80
  • 4
  • 2
7 Comments
 
LVL 3

Expert Comment

by:smot
ID: 22857425
function SetProxy(UserAgent, Address, UserName, Password: string; Port: integer): Boolean;
var
  list: INTERNET_PER_CONN_OPTION_LIST;
  dwBufSize: DWORD;
  hInternet, hInternetConnect: Pointer;
  Options: array[1..3] of INTERNET_PER_CONN_OPTION;
begin
  Result := False;
  dwBufSize := SizeOf(list);
  list.dwSize := SizeOf(list);
  list.pszConnection := nil;
  list.dwOptionCount := High(Options);
  Options[1].dwOption := INTERNET_PER_CONN_FLAGS;
  Options[1].Value.dwValue := PROXY_TYPE_DIRECT or PROXY_TYPE_PROXY;
  Options[2].dwOption := INTERNET_PER_CONN_PROXY_SERVER;
  Options[2].Value.pszValue := PAnsiChar(string(Address));
  Options[3].dwOption := INTERNET_PER_CONN_PROXY_BYPASS;
  Options[3].Value.pszValue := '<local>';
  list.pOptions := @Options;
  hInternet := InternetOpen(PChar(UserAgent), INTERNET_OPEN_TYPE_DIRECT, nil, nil, 0);
  if hInternet <> nil then
  try
    hInternetConnect := InternetConnect(hInternet, PChar(Address), Port, PChar(UserName), PChar(Password),
      INTERNET_SERVICE_HTTP, 0, 0);
    if hInternetConnect <> nil then
    begin
      Result := InternetSetOption(hInternet, INTERNET_OPTION_PER_CONNECTION_OPTION, @list, dwBufSize);
      if Result then
        Result := InternetSetOption(hInternet, INTERNET_OPTION_REFRESH, nil, 0);
    end;
  finally
    InternetCloseHandle(hInternet)
  end;
end;

function DisableProxy(): Boolean;
var
  list: INTERNET_PER_CONN_OPTION_LIST;
  dwBufSize: DWORD;
  hInternet: Pointer;
  Options: array[1..3] of INTERNET_PER_CONN_OPTION;
begin
  Result := False;
  dwBufSize := SizeOf(list);
  list.dwSize := SizeOf(list);
  list.pszConnection := nil;
  list.dwOptionCount := High(Options);
  Options[1].dwOption := INTERNET_PER_CONN_FLAGS;
  Options[1].Value.dwValue := PROXY_TYPE_DIRECT or PROXY_TYPE_PROXY;
  Options[2].dwOption := INTERNET_PER_CONN_PROXY_SERVER;
  Options[2].Value.pszValue := PAnsiChar('');
  Options[3].dwOption := INTERNET_PER_CONN_PROXY_BYPASS;
  Options[3].Value.pszValue := '<local>';
  list.pOptions := @Options;
  hInternet := InternetOpen(PChar(''), INTERNET_OPEN_TYPE_DIRECT, nil, nil, 0);
  if hInternet <> nil then
  try
    Result := InternetSetOption(hInternet, INTERNET_OPTION_PER_CONNECTION_OPTION, @list, dwBufSize);
    if Result then
      Result := InternetSetOption(hInternet, INTERNET_OPTION_REFRESH, nil, 0);
  finally
    InternetCloseHandle(hInternet)
  end;
end;

0
 
LVL 7

Author Comment

by:fifthelement80
ID: 22857519
I added the WinInet to uses clause , but INTERNET_PER_CONN_OPTION_LIST is still undeclared , are you using any other units to declare these identifiers ?
0
 
LVL 3

Expert Comment

by:smot
ID: 22857561
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 7

Author Comment

by:fifthelement80
ID: 22858369
I did , first the structure INTERNET_PER_CONN_OPTION , didnt have a .value.dwValue , so I removed .value part from the code and changed the code to this :

  Options[1].dwOption := INTERNET_PER_CONN_FLAGS;
  Options[1].dwValue := PROXY_TYPE_DIRECT or PROXY_TYPE_PROXY;
  Options[2].dwOption := INTERNET_PER_CONN_PROXY_SERVER;
  Options[2].pszValue := PAnsiChar(string(Address));
  Options[3].dwOption := INTERNET_PER_CONN_PROXY_BYPASS;
  Options[3].pszValue := '<local>';
  list.pOptions := @Options;

then it was giving error on this line :
  Options[2].pszValue := PAnsiChar(string(Address));
Incompatible type 'AnsiChar' and 'Char'

so I changed it to :
  Options[2].pszValue := PChar(string(Address));

and it compiled fine , then I called the procedure by following command :
SetProxy('','localhost','','',8080);

but it didnt work :( I am using delphi 2009
0
 
LVL 1

Expert Comment

by:SaTaX
ID: 22859190
I do not see why doing so much with all those hard keys.

You can simply do this with registry keys. Internet Explorer adds and removes registry keys to get it worked. Don't forget the proxy will be applied on browsers and internet activities.

Here it is:
This uses registry and wininet.

Procedure SetProxy(cost Server: String);
var
  Reg : TRegistry;
begin
  Reg := TRegistry.Create;
  Reg.OpenKey('Software\Microsoft\Windows\CurrentVersion\Internet Settings',False);
  Reg.WriteString('ProxyServer',Server);
  Reg.WriteBool('ProxyEnable,True);
  Reg.CloseKey;
  Reg.Free;
  InternetSetOption(0, INTERNET_OPTION_SETTINGS_CHANGED, 0, 0);
end;

This is how to use:
SetProxy('Proxy:Port');



Procedure 2 to disable:

Procedure DisableProxy;
var
  Reg : TRegistry;
begin
  Reg := TRegistry.Create;
  Reg.OpenKey('Software\Microsoft\Windows\CurrentVersion\Internet Settings',False);
  Reg.WriteBool('ProxyEnable,False);
  Reg.CloseKey;
  Reg.Free;
  InternetSetOption(0, INTERNET_OPTION_SETTINGS_CHANGED, 0, 0);
end;

This is how to use:
DisableProxy;


If IE is running and those new proxy settings are applied, IE will not read the new settings.
To get the new settings it'll need to not have Internet explorer open.
So if the user has InternetExplorer Running you can use read_internet procedure:

procedure IE_Read;
var
  HInet: HINTERNET;
begin
  hInet := InternetOpen(PChar('myappname'), INTERNET_OPEN_TYPE_DIRECT,
    nil, nil, INTERNET_FLAG_OFFLINE);
  try
    if hInet <> nil then
      InternetSetOption(hInet, INTERNET_OPTION_SETTINGS_CHANGED, nil, 0);
  finally
    InternetCloseHandle(hInet);
  end;
end;

This is how to use it:
IE_Read;
0
 
LVL 7

Author Comment

by:fifthelement80
ID: 22860562
@SaTaX : Thank you for your reply , but your solution doesn't set proxy on dial up connections and only works for LAN connection.
0
 
LVL 7

Accepted Solution

by:
fifthelement80 earned 0 total points
ID: 22860727
I found the solution , I attach the complete code for further use.
in order to get the names of dialup connections , I user TMagRas component , here is the link to component : http://www.magsys.co.uk/delphi/magras.asp
unit proxyu;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls,wininet,magrasent;
 
type
  TForm1 = class(TForm)
    SetProxybtn: TButton;
    Disableproxybtn: TButton;
    procedure SetProxybtnClick(Sender: TObject);
    procedure DisableproxybtnClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
 
var
  Form1: TForm1;
 
implementation
 
{$R *.dfm}
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;
 
  PROXY_TYPE_DIRECT         = $00000001;   // direct to net
  PROXY_TYPE_PROXY          = $00000002;   // via named proxy
 
  INTERNET_OPTION_PER_CONNECTION_OPTION   = 75;
 
//--TProxy Section--------------------------------------------------------------
type
   INTERNET_PER_CONN_OPTION = record
      dwOption: DWORD;
      Value: record
         case Integer of
            1: (dwValue: DWORD);
            2: (pszValue: PAnsiChar);
            3: (ftValue: TFileTime);
      end;
   end;
   LPINTERNET_PER_CONN_OPTION = ^INTERNET_PER_CONN_OPTION;
   INTERNET_PER_CONN_OPTION_LIST = record
      dwSize: DWORD;
      pszConnection: LPTSTR;
      dwOptionCount: DWORD;
      dwOptionError: DWORD;
      pOptions: LPINTERNET_PER_CONN_OPTION;
   end;
   LPINTERNET_PER_CONN_OPTION_LIST = ^INTERNET_PER_CONN_OPTION_LIST;
 
procedure SetProxy(Con,ProxyServer: string); //mladen
var
   list: INTERNET_PER_CONN_OPTION_LIST;
   dwBufSize: DWORD;
   Options: array[1..2] of INTERNET_PER_CONN_OPTION;
begin
   dwBufSize := sizeof(list);
   list.dwSize := sizeof(list);
   list.pszConnection := pchar(Con);
   list.dwOptionCount := High(Options); // the highest index of the array (in this case 3)
   Options[1].dwOption := INTERNET_PER_CONN_FLAGS;
   Options[1].Value.dwValue := PROXY_TYPE_PROXY;
   Options[2].dwOption := INTERNET_PER_CONN_PROXY_SERVER;
   Options[2].Value.pszValue := PansiChar(ProxyServer);
   list.pOptions := @Options;
   InternetSetOption(nil, INTERNET_OPTION_PER_CONNECTION_OPTION, @list, dwBufSize);
   InternetSetOption(nil, INTERNET_OPTION_REFRESH, nil, 0);
end;
 
procedure DisableProxy(Con:string); //mladen
var
   list: INTERNET_PER_CONN_OPTION_LIST;
   dwBufSize: DWORD;
   Options: array[1..2] of INTERNET_PER_CONN_OPTION;
begin
   dwBufSize := sizeof(list);
   list.dwSize := sizeof(list);
   list.pszConnection := pchar(Con);
   list.dwOptionCount := High(Options); // the highest index of the array (in this case 3)
   Options[1].dwOption := INTERNET_PER_CONN_FLAGS;
   Options[1].Value.dwValue := PROXY_TYPE_DIRECT;
   Options[2].dwOption := INTERNET_PER_CONN_PROXY_SERVER;
   Options[2].Value.pszValue := PansiChar('');
   list.pOptions := @Options;
   InternetSetOption(nil, INTERNET_OPTION_PER_CONNECTION_OPTION, @list, dwBufSize);
   InternetSetOption(nil, INTERNET_OPTION_REFRESH, nil, 0);
end;
 
// End of Proxy-----------------------------------------------------------------
 
 
procedure TForm1.SetProxybtnClick(Sender: TObject);
var
  i : integer;
begin
        I := MagRasGetEntryList ('') ;
        if (i=0) then
        begin
          for I := 0 to Pred (MagRasNumEntryRec) do SetProxy(MagRasEntryRecs [I].EntryName,'localhost:8080');
        end;
        SetProxy('','localhost:8080');
end;
 
procedure TForm1.DisableproxybtnClick(Sender: TObject);
var
  i : integer;
begin
        I := MagRasGetEntryList ('') ;
        if (i=0) then
        begin
          for I := 0 to Pred (MagRasNumEntryRec) do  DisableProxy(MagRasEntryRecs [I].EntryName);
        end;
        DisableProxy('');
end;
end.

Open in new window

0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Intraweb download file link ? 1 134
find a node in VST 2 69
Multi-layered image in FireMonkey 9 37
How to Get Images From Server to Client using App Tethering 1 12
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question