Link to home
Start Free TrialLog in
Avatar of crystyan
crystyan

asked on

Open a webpage from code.

Hi,

I want a function to open an url in the browser. I don`t want the ShellExecute(Handle, 'open', PChar(url), '', '', SW_SHOWDEFAULT) function because sometimes it doesn`t work, maybe cuz it`s not set any default browser ?
I would want the function to open Internet Explorer if possible ? I just need something that woun`t fail when opening a page!

Thanks a lot !
Avatar of mokule
mokule
Flag of Poland image

 WinExec('"c:\program files\internet explorer\iexplore.exe" http://www.google.com/', SW_SHOW);
Avatar of crystyan
crystyan

ASKER

lol
what if that`s not the correct path ?
Search for the path in registry
HKLM\Software\classes\Applications\iexplore.exe\shell\open\command
uses registry;

procedure TForm1.Button1Click(Sender: TObject);
var
  Registry: TRegistry;
  s: string;
  p: integer;
begin
  Registry := TRegistry.Create(KEY_READ);
  try
    Registry.RootKey := HKEY_LOCAL_MACHINE;
    if Registry.OpenKeyReadOnly('Software\classes\Applications\iexplore.exe\shell\open\command') then
      begin
      s := Registry.ReadString('');
      p := Pos(' %1',s);
      if p > 0  then
        s := Copy(s,1,p-1);
      WinExec(PAnsiChar(s+ ' http://www.google.com/'), SW_SHOW);
      end;
  finally
    Registry.Free;
  end;
end;
I think it is more probable that a user has uninstalled his IE, than that he doesn't have a default browser.
Anyway, if you want to be as sure as it gets, do multiple tests:

1) ShellExecute as you said it. If it returns <= 32, goto step 2
2) Read HKEY_CLASSES_ROOT\http\shell\open\command to get the default browser.
If the key exists, see if the FileExists.
Try ShellExecute. If it returns <= 32, goto next step.
3) The same with HKEY_CLASSES_ROOT\htmlfile\shell\open\command
4) The same with HKEY_LOCAL_MACHINE_Software\classes\Applications\iexplore.exe\shell\open\command
could u write the whole code for that pls ?
ASKER CERTIFIED SOLUTION
Avatar of Russell Libby
Russell Libby
Flag of United States of America image

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

One month is more than enough time to respond to a question....
That being said, before asking new questions, its usually in good taste to respond to help that has been provided for your outstanding questions.

Russell