Solved

Detecting default web browser

Posted on 1998-08-18
7
332 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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

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…
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…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

777 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