Solved

Detecting default web browser

Posted on 1998-08-18
7
333 Views
Last Modified: 2010-04-04
How can I detect the default web browser from Delphi 3 applicatuion?
0
Comment
Question by:fh1
7 Comments
 
LVL 8

Expert Comment

by:ZifNab
ID: 1337193
fh1,

is it for just starting the default browser?

Then use this :

ShellExecute(Self.Handle,'open','http://www.attitudesoft.com.au/news.htm','','',
SW_SHOWDEFAULT);

Regards, Zif.
0
 
LVL 8

Accepted Solution

by:
ZifNab earned 50 total points
ID: 1337194
And here is an answer to your question, from TI
Frequently Asked Questions
Displaying documents in the default browser
Question:
How can I start the default browser with an URL in a new browser window?
Answer:
The following example attempts to start the specified URL
in a new browser window. Note: Not all versions of
Internet Explorer will respond with a new window.

Example:

uses
 DdeMan,
{$IFDEF WIN32}
  Registry; {We will get it from the registry}
{$ELSE}
  IniFiles; {We will get it from the win.ini file}
{$ENDIF}

{$IFNDEF WIN32}
  const MAX_PATH = 144;
{$ENDIF}

function GetProgramAssociation (Ext : string) : string;
var
{$IFDEF WIN32}
  reg: TRegistry;
  s : string;
{$ELSE}
  WinIni : TIniFile;
  WinIniFileName : array[0..MAX_PATH] of char;
  s : string;
{$ENDIF}
begin
{$IFDEF WIN32}
  s := '';
  reg := TRegistry.Create;
  reg.RootKey := HKEY_CLASSES_ROOT;
  if reg.OpenKey('.' + ext + '\shell\open\command',
                 false) <> false then begin
  {The open command has been found}
    s := reg.ReadString('');
    reg.CloseKey;
  end else begin
  {perhaps thier is a system file pointer}
    if reg.OpenKey('.' + ext,
                   false) <> false then begin
      s := reg.ReadString('');
      reg.CloseKey;
      if s <> '' then begin
     {A system file pointer was found}
        if reg.OpenKey(s + '\shell\open\command',
                       false) <> false then
     {The open command has been found}
          s := reg.ReadString('');
        reg.CloseKey;
      end;
    end;
  end;
 {Delete any command line, quotes and spaces}
  if Pos('%', s) > 0 then
    Delete(s, Pos('%', s), length(s));
  if ((length(s) > 0) and
      (s[1] = '"')) then
    Delete(s, 1, 1);
  if ((length(s) > 0) and
      (pos('"', s) > 0)) then
    Delete(s, pos('"', s), Length(s));
  while ((length(s) > 0) and
         (s[length(s)] = #32)) do
    Delete(s, Length(s), 1);
{$ELSE}
  GetWindowsDirectory(WinIniFileName, sizeof(WinIniFileName));
  StrCat(WinIniFileName, '\win.ini');
  WinIni := TIniFile.Create(WinIniFileName);
  s := WinIni.ReadString('Extensions',
                          ext,
                          '');
  WinIni.Free;
 {Delete any command line}
  if Pos(' ^', s) > 0 then
    Delete(s, Pos(' ^', s), length(s));
 {$ENDIF}
  result := s;
end;

procedure StartNewBrowserWindow(URL : string);
var
  DDEConv : TDDEClientConv;
  URLFired : bool;
  App : string;
  UpApp : string;
  p : array[0..MAX_PATH] of char;
begin
  UrlFired := false;
  App := GetProgramAssociation('HTM');
  UpApp := Uppercase(App);
  Delete(App, Pos('.EXE', UpAPP), length(App));
  if Pos('NETSCAPE.EXE',
         UpApp) > 0 then begin
    DDEConv:=TDDEClientConv.Create(nil);
    DDEConv.ServiceApplication := App;
    if DDEConv.SetLink('NETSCAPE' , 'WWW_OpenURL') then
      if DDEConv.RequestData(URL +
                             ',,0x0,0x0') <> nil then
        if DDEConv.SetLink('NETSCAPE', 'WWW_Activate') then
          URLFired := DDEConv.RequestData('0xFFFFFFFF,0x0') <> nil;
    DDEConv.Free;
  end else
  if Pos('IEXPLORE.EXE',
         UpApp) > 0 then begin
    DDEConv:=TDDEClientConv.Create(nil);
    DDEConv.ServiceApplication := App;
    if DDEConv.SetLink('iexplore', 'WWW_OpenURL') then
      if DDEConv.RequestData(URL + ',,0') <> nil then
        if DDEConv.SetLink('iexplore', 'WWW_Activate') then
          URLFired := DDEConv.RequestData('0,0') <> nil;
    DDEConv.Free;
  end;
  if UrlFired = false then
    WinExec(StrPCopy(@p, URL), SW_SHOWNORMAL);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  StartNewBrowserWindow('http://www.borland.com');
  StartNewBrowserWindow('http://www.yahoo.com');
end;

Regards, Zif.
0
 

Expert Comment

by:j_mollaghan
ID: 1337195
The simplest way to detect the default browser is to check the .HTML association from the registry. (look in HKEY_CLASSES_ROOT). You'll get (from the default value for .HTML) a class value for the browser (in my case it's value is NETSCAPEMARKEUP). Then, simply check the class for NETSCAPEMARKUP, note the CLSID for ii (a guid). Then check the information for the guid (still in HKEYCLASSESROOT), to get the path toe the browser. It only requires a few lines of code to get the path to the broswers EXE.
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 4

Expert Comment

by:d003303
ID: 1337196
j_mollaghan,
this is the way ShellExecute does it:
- lookup for the value of HKEY_CLASSES_ROOT\.html (say it is NETSCAPEMARKEUP)
- lookup for the value of HKEY_CLASSES_ROOT\NETSCAPEMARKEUP\shell\open\command to get the command line.

Your solution describes the way OLE does it. OLE then checks for the HKEY_CLASSES_ROOT\CLSID\{61D8DE20-CA9A-11CE-9EA5-0080C82BE3B6} key and looks for InprocServer32, LocalServer32 and InprocHandler32. The values of these keys are then used for OLE Automation and OLE inplace activation/editing.
These values can differ between the values ShellExecute gets !

Slash/d003303
0
 

Author Comment

by:fh1
ID: 1337197
I am using ShellExecute, but if there is no web browser a window pops up and asks the user for the directory of the web browser. How can I prevent the window from showing up and instead show my own message?
0
 
LVL 4

Expert Comment

by:d003303
ID: 1337198
fh1,
two possibilities. One is to implement what ShellExecute does by yourself (bad), second is to check by yourself if a browser is installed. Use my comment above to check if the association exists, get the application filename and check if it exists. If yes, spawn ShellExecute. If no, pop up your message and don' spawn ShellExecute.

Slash/d003303
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1337199
fh1, don't know if it's possible with ShellExecute, but with ShellExecuteEx it is :

procedure TForm1.Button1Click(Sender: TObject);

var nHandle:integer;
    ShellInfo:TShellExecuteInfo;
    LastErrorMode : Uint;
begin
 FillChar(ShellInfo,Sizeof(ShellInfo), #0);
 ShellInfo.cbSize := Sizeof(ShellInfo);
 ShellInfo.Fmask := SEE_MASK_NOCLOSEPROCESS or SEE_MASK_FLAG_NO_UI {THIS ONE IS NEEDED FOR NO ERRORS!};
 ShellInfo.lpVerb := 'Open';
 ShellInfo.Wnd := Application.MainForm.Handle;

{Handles both EXE programs or Document Files}
 ShellInfo.lpFile := 'http://www.delphi.magsoft.com.pl';
 if not shellExecuteEx(@ShellInfo) then showmessage('error : '+IntToStr(ShellInfo.hInstApp));
 { if waiting till process is finished }
 WaitforSingleObject(ShellInfo.hProcess, INFINITE);
end;
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi Dbf export problem to a Visual Foxpro application 6 208
Delphi...Split view - idea? 1 90
firemonkey keyboard covers the controls 1 37
shape, triangle, dbctrlgrid 3 17
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

821 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