Link to home
Start Free TrialLog in
Avatar of ChrisBerry
ChrisBerryFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Opening the Default Browser with a URL

Hi,

Can anybody please tell me how to open the default browser with a url. (I can do it for IE)

Browsers must include IE, FireFox, Opera at least.

Finding the reliably finding the default browser in the registry seems to be tricky as not all associations are not what I would have expected i.e
.htp, .html, htmlfiles, http

Cheers

Chris
Avatar of Jase-Coder
Jase-Coder

you use shellexecute(handle, 'open', 'iexplore.exe', 'www.google.com', nil, SW_SHOW);

you have to add ShellAPI to the uses section of the .pas

if you want it to work with other browsers just change the 'iexplore.exe' part
SOLUTION
Avatar of Jase-Coder
Jase-Coder

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Unfortunately it is impossible just to create an HTML file and see what application this is opened with, as changing the default browser, often doesn't change the file associations for HTML and HTM extensions, only what app is used when you type in a URL in the Run dialog box for example.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mohammed Nasman
Hello Chris

  Use FindExecutable API to  determined which default Browser, then you can open with the URL using ShellExecute API, FindExecutable need existing file to work properly, so create temporary file using CreateFile API, so using these 3 API should do everything you asked, here's the sample code ;-)

uses ShellAPI;

function GetDefaultBrowser : string;
var
  Exe : array[0..128] of char;
begin
  CreateFile('c:\page.htm',0,0,nil,CREATE_ALWAYS,0,0);
  FindExecutable('c:\page.htm','',Exe);
  if  FileExists('c:\page.htm') then
    DeleteFile('c:\page.htm');
  Result := Exe;
end;

procedure RunURL(Url: string);
begin
  ShellExecute(0,'open',PChar(GetDefaultBrowser),PChar(Url),nil,Sw_ShowNormal);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  RunURL('www.google.com')
end;

you can extend the code above to work with other types

Regards,
Mohammed
uses
  OleServer, Outlook8, DdeMan, ShellAPI;

//........

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 := AnsiUppercase(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;

emil
Avatar of ChrisBerry

ASKER

Hi,

Thanks for all the answers - time needed to digest. Just out of interest, which registry keys are reliably associated with the default browser? I suppose I mean - how do you associate browser type files, or any others for that matter, to an exe?

Cheers

Chris
The suggestion i gave used the registry key HKEY_LOCAL_MACHINE\software\classes\http\shell\open\command\. Looking at documents on the internet, it seems all your aforementioned browsers use this registry key to set themselves as the default browser. However, opening the registry to this key and sucking out the value is actually more work than you need to do. Vihmapuu mentioned a short one line code which actually indirectly causes the operating system to read this value from the registry, it takes the programme from the registry which is associated with HTTP connections, as the registry entry that i gave suggests, meaning its the same one. Therefore using that method should work for any browser, with exactly the same result as the longer solution which i gave you.

Quoting Vihmapuu:
 ShellExecute(handle,'open','http://www.myurl.com',nil,nil,SW_SHOWNORMAL);

-- and as for changing the shape, space and position of the window, i gave you loads of constants with SW_ at the beginning.. using these values at the end instead of SW_SHOWNORMAL, means you can force the browser to opened Maximized, Minimized, or even hidden.

Henry Thacker.
Sorry, i posted the SW_ constants and descriptions in another thread which i can't find right now, if you don't know them and want them, i will be happy to find them for you.
And finally, Some other experts mention the registry key \htmlfile\shell\open\command ... this is completely different to the default browser. This key contains the application which is associated with HTML files. If you click the "Set as my default browser" option in Mozilla, this DOESN'T change this key. IE, or whatever other browser will still be associated with your HTML files. All it changes is the registry key which is read when you enter "http://aurl.com" in the RUN dialog in windows / Shell Execute function. Hope this helps.
Henry, thanks, you need not post the constants as I am familiar with ShellExecute. I did investigate the various options and also found \htmlfile\... not to be the default browser key in fact I still do not know which is the key for the default browser as I have tried substituting various keys and setting file extension associations for html etc to another application completely just to see if this would open from a link in my e-mail but it still opens the default browser.

I think I will close this question and open another on the subject of how to trap URLs such that they always go to a specified program if that program is running and re-instating the default browser once the program closes.

Thanks for all your help