Solved

Detecting default web browser

Posted on 1998-08-18
7
330 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 4

Expert Comment

by:d003303
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

762 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now