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 !
Delphi

Avatar of undefined
Last Comment
Russell Libby

8/22/2022 - Mon
mokule

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

ASKER
lol
what if that`s not the correct path ?
mokule

Search for the path in registry
HKLM\Software\classes\Applications\iexplore.exe\shell\open\command
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
mokule

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;
alkisg

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
crystyan

ASKER
could u write the whole code for that pls ?
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER CERTIFIED SOLUTION
Russell Libby

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Russell Libby


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