Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delphi - add trusted site in IE 2

Status
Not open for further replies.

kinnon

Programmer
Dec 21, 2005
9
0
0
GB
Hi there,

I work for a business who has a large client base spread across the UK. We have to add a specific URL to IE on each of our clients dedicated machines, and they really dont have the staff skills to do it themselves.

I've written a small program to perform all the updates we need done, bar one.

Is is possible to add a trusted site autopmatically from a delphi app. I've seen some VB code which is supposed to be able to do it, but its beyond my comprehension.

Thanks in advance for any responses.
 
A quick look on Google (for trusted sites registry)found this information:

and this (slightly more chatty):

There is also some basic info on writing to the registry at:

The registry key to change is
[tt]HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\Domains[/tt]

Given the key, you can add your trusted sites to the registy (read the Delphi help on [tt]TRegistry[/tt] and [tt]TRegIniFile[/tt]).
This is a bit of test code I've created for the task (from the Delphi help example):
Code:
procedure TForm1.B_SubmitClick(Sender: TObject);
const
  KeyName ='Software\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\Domains';
var
  Reg: tRegistry; //TRegistry;
  SectionName : string;
  nIntraNet,
  nTrusted,
  nInternetSites,
  nRestricted : word;

begin
  nIntraNet := 1;
  nTrusted := 2;
  nInternetSites := 3;
  nRestricted := 4;

  Reg := TRegIniFile.Create;  //create the key
  SectionName := E_WebSite.Text;       // This is the web site, without the HTTP - eg microsoft.com
  try
    Reg.RootKey := HKEY_CURRENT_USER; // write to this for all users, HKEY_CURRENT_USER for just the current user
    if Reg.OpenKey(KeyName + '\' + SectionName,TRUE) then  // open the key - the TRUE forces creation if its not there
      begin
        Reg.WriteInteger('http', nTrusted); // write the value - http or ftp can be used here
      end;
    Reg.CloseKey;
  finally
    Reg.Free;
  end;
end;

I've tested, but it might need some tweaking and should be straight forward to apply. It will probably have to run from a script on login for all users to ensure it applies to each of them.

Don't forget to add the [tt]Registry[/tt] unit to your uses clause.

Good luck

Cheers,
Chris [pc2]
 
ChrisGShultz you're right. I've approached the same issue in your way (using registry). I wrote a simple program to quickly add sites to trusted zones in IE6. Just wanted to be convinced that it worked really. It seemed interesting to me to do that. :). Some trial and error investigations showed to me that for each site added in trusted zones of IE6 assuming site addresses were entered in the format:

[protocol://][prefix.]domain/somepath/tosomewhere.html

protocol = [http|https|ftp]
domain = [aWord.???|aWord.??.??]
prefix = zero or more words separated by "."


the following happened:


0. The text after domain is ignored


1. A key with same name as domain entered is added under the registry key which Chris pointed out:
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\Domains


2. If there is a prefix then a subkey with its name is created

2.1 A value with same name as protocol entered and value 2 is created under the prefix key

3. If there was no prefix than

3.1 A value with same name as protocol entered and value 2 is created under the domain key


This was my code. Again nothing essentially different from what ChrisGShultz posted.



Code:
...

procedure AddTrustedSite(Site:string);
procedure DecodeSiteName(Site:string; 
                         var Protocol:string; 
                         var Prefix:string; 
                         var Domain:string);

implementation

uses Registry, StrUtils;

...

procedure TForm1.Button1Click(Sender: TObject);
begin
    try
      AddTrustedSite(Edit1.Text);
    except
      on e:Exception do
        ShowMessage('Try logging in as Administrator : '+e.Message);
    end;
end;

///////////////////////////////////////////////////////////////
// procedure : AddTrustedSite
// purpose   : Perform in IE6 the equivalent of user actions:
//            - Tools|Internet Options|Security|Trusted|
//              <Type and Add site>
//
// assumptions:
//      Assumes that the Site is passed in following format:
	[protocol://][{prefix.}...] domain.extension
//////////////////////////////////////////////////////////////
procedure AddTrustedSite(Site:string);
var
  reg:TRegistry;
  sDomain:string;
  sPrefix:string;
  sProtocol:string;
begin

   DecodeSiteName(Site,sProtocol,sPrefix,sDomain);

   reg:=TRegistry.Create;
   try
      reg.RootKey:=HKEY_CURRENT_USER;

      if reg.OpenKey(TRUSTED_ZONES_KEY,false) then
      begin
          //Make sure the domain is added as a key in the trusted zones
          reg.OpenKey(sDomain,true);

          //If there is a prefix create a subkey of it
          if sPrefix<>'' then
             reg.OpenKey(sPrefix,true);

          //Add a value into the current key
          if sProtocol='' then
             reg.WriteInteger('*',TRUSTED)
          else
             reg.WriteInteger(sProtocol,TRUSTED);
      end
      else
        raise Exception.Create('Failed to open Trusted Zones registry');

   finally
       reg.Free;
   end;
end;

//////////////////////////////////////////////////////////////////
// procedure: DecodeSiteName
// purpose  : Decode given Site string into three parts according
//            to site format specified in AddTrustedSite comments
//////////////////////////////////////////////////////////////////
procedure DecodeSiteName(Site:string; 
                         var Protocol:string; 
                         var Prefix:string; var Domain:string);
var
  nPos,nDomainWords:integer;
  slTmp:TStringList;
begin
    //Extract the protocol if there is any
    nPos:=Pos('://',Site);
    if nPos>0 then
    begin
      Protocol:=Copy(Site,1,nPos-1);
      Site:=Copy(Site,nPos+3,length(Site)-(nPos+2));
    end;

    //Ignore any text after the trailing "/"
    nPos:=Pos('/',Site);
    if nPos>0 then
      Site:=Copy(Site,1,nPos-1);

    slTmp:=TStringList.Create;

    with slTmp do
    try
      Delimiter:='.';
      DelimitedText:=Site;

      //Are there at least three words in Site Address ??
      if Count>2 then
      begin
          //If the last words in the site are less than 2 letters long
          //Then the domain is made out of last three words
          //otherwise it is made out of the last two words
          if (length(Strings[Count-1])<3) and (length(Strings[Count-2])<3) then
          begin
              Domain:=Strings[Count-3]+'.'+Strings[Count-2]+'.'+Strings[Count-1];
              nDomainWords:=3;
          end
          else
          begin
              Domain:=Strings[Count-2]+'.'+Strings[Count-1];
              nDomainWords:=2;
          end;

          //The prefix is the remaining part of the Site address (if any is left)
          if Count>nDomainWords then
            Prefix:=StringReplace(Site,'.'+Domain,'',[])
          else
            Prefix:='';
      end
      else
      begin
          Domain:=Site;
          Prefix:='';
      end;


    finally
      slTmp.Free;
    end;
end;
...


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top