ChrisBerry
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
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
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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,CRE ATE_ALWAYS ,0,0);
FindExecutable('c:\page.ht m','',Exe) ;
if FileExists('c:\page.htm') then
DeleteFile('c:\page.htm');
Result := Exe;
end;
procedure RunURL(Url: string);
begin
ShellExecute(0,'open',PCha r(GetDefau ltBrowser) ,PChar(Url ),nil,Sw_S howNormal) ;
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
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
FindExecutable('c:\page.ht
if FileExists('c:\page.htm') then
DeleteFile('c:\page.htm');
Result := Exe;
end;
procedure RunURL(Url: string);
begin
ShellExecute(0,'open',PCha
end;
procedure TForm1.Button1Click(Sender
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.Cr eate(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('0xFFF FFFFF,0x0' ) <> nil;
DDEConv.Free;
end
else
if Pos('IEXPLORE.EXE', UpApp) > 0 then
begin
DDEConv:=TDDEClientConv.Cr eate(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
OleServer, Outlook8, DdeMan, ShellAPI;
//........
procedure StartNewBrowserWindow(URL:
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.Cr
DDEConv.ServiceApplication
if DDEConv.SetLink('NETSCAPE'
if DDEConv.RequestData(URL + ',,0x0,0x0') <> nil then
if DDEConv.SetLink('NETSCAPE'
URLFired := DDEConv.RequestData('0xFFF
DDEConv.Free;
end
else
if Pos('IEXPLORE.EXE', UpApp) > 0 then
begin
DDEConv:=TDDEClientConv.Cr
DDEConv.ServiceApplication
if DDEConv.SetLink('iexplore'
if DDEConv.RequestData(URL + ',,0') <> nil then
if DDEConv.SetLink('iexplore'
URLFired := DDEConv.RequestData('0,0')
DDEConv.Free;
end;
if (UrlFired = False) then
WinExec(StrPCopy(@p, URL), SW_SHOWNORMAL);
end;
emil
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
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\softwar e\classes\ http\shell \open\comm and\. 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.
Quoting Vihmapuu:
ShellExecute(handle,'open'
-- 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\comma nd ... 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.
ASKER
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
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
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