Recent

Author Topic: Is there a function that opens an internet web page?  (Read 5993 times)

DiCri

  • Full Member
  • ***
  • Posts: 151
  • My goal : Build a game
    • http://manueldicriscito.altervista.org/DinoLand.zip
Is there a function that opens an internet web page?
« on: July 21, 2016, 02:05:26 pm »
Hi Pascal Word,
I work with SDL unit. In a program, imagine there is a button and if the player press the button, google chrome starts with a specified url. Is this possible? Thanks!  :)
I'm a game developer.. Now studying..
Go download my game:
http://manueldicriscito.altervista.org/DinoLand.zip

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Is there a function that opens an internet web page?
« Reply #1 on: July 21, 2016, 02:33:13 pm »
Add LCLIntf to your uses clause, then use the function
Code: Pascal  [Select][+][-]
  1. function OpenURL(AURL: String): Boolean;

DiCri

  • Full Member
  • ***
  • Posts: 151
  • My goal : Build a game
    • http://manueldicriscito.altervista.org/DinoLand.zip
Re: Is there a function that opens an internet web page?
« Reply #2 on: July 21, 2016, 03:26:54 pm »
Add LCLIntf to your uses clause, then use the function
Code: Pascal  [Select][+][-]
  1. function OpenURL(AURL: String): Boolean;
I am using FreePascal so i have not LCLIntf unit..
I'm a game developer.. Now studying..
Go download my game:
http://manueldicriscito.altervista.org/DinoLand.zip

Bart

  • Hero Member
  • *****
  • Posts: 5290
    • Bart en Mariska's Webstek
Re: Is there a function that opens an internet web page?
« Reply #3 on: July 21, 2016, 03:35:28 pm »
If on Windows you can use:
Code: [Select]
  ShellExecuteW(0, nil, PWideChar(TheUrl), nil, nil, SW_SHOWNORMAL).

It will launch the default browser (which may or may ot be Chrome), with the specified URL.

(TheUrl must be a UnicodeString or WideString).
The returnvalue (of type HInst) must be > 32, otherwise the function failed.

Bart

Ñuño_Martínez

  • Hero Member
  • *****
  • Posts: 1186
    • Burdjia
Re: Is there a function that opens an internet web page?
« Reply #4 on: July 21, 2016, 09:49:32 pm »
Problem with ShellExecute is that it is Windows only.

There's no simple way to do it in a cross platform way.  I was looking for something like that, so I read the LCL sources to see how they do it and it's quite complicated.  Each operating system uses a very different way.

Linux seems to be the worst case.  It scans different functions and locations to find a webbrowser executable then run it passing the appropriate command-line options. :(
« Last Edit: July 21, 2016, 09:51:35 pm by Ñuño_Martínez »
Are you interested in game programming? Join the Pascal Game Development community!
Also visit the Game Development Portal

wp

  • Hero Member
  • *****
  • Posts: 11916
Re: Is there a function that opens an internet web page?
« Reply #5 on: July 21, 2016, 10:44:26 pm »
TProcess? (http://wiki.freepascal.org/Executing_External_Programs)

Code: Text  [Select][+][-]
  1. uses
  2.   Process;
  3.  
  4. var
  5.   AProcess: TProcess;
  6. begin
  7.   AProcess := TProcess.Create(nil);
  8.   try
  9.     AProcess.Executable := 'C:\Program Files (x86)\Mozilla Firefox\firefox.exe';
  10.     AProcess.Parameters.Add('somefile.html');
  11.     AProcess.Options := AProcess.Options + [poWaitOnExit];
  12.     AProcess.Execute;
  13.   finally
  14.     AProcess.Free;
  15.   end;  

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Is there a function that opens an internet web page?
« Reply #6 on: July 22, 2016, 12:44:38 am »
Linux seems to be the worst case.  It scans different functions and locations to find a webbrowser executable then run it passing the appropriate command-line options. :(
Unless you're using a very cryptic distro, you can assume that Linux is XDG compliant. In that case:
Code: Bash  [Select][+][-]
  1. $ xdg-open <url>
should suffice.

Handoko

  • Hero Member
  • *****
  • Posts: 5154
  • My goal: build my own game engine using Lazarus
Re: Is there a function that opens an internet web page?
« Reply #7 on: July 22, 2016, 06:29:07 am »
Add LCLIntf to your uses clause, then use the function
Code: Pascal  [Select][+][-]
  1. function OpenURL(AURL: String): Boolean;
I am using FreePascal so i have not LCLIntf unit..

So use Lazarus instead of FPC. I understand the extra 'fat' some people dislike. But fat can be delicious. Lazarus can really speed up your development a lot. Otherwise, write the function yourself using FPC, you can always look into the code of available solutions.

wiki.lazarus.freepascal.org/Webbrowser
« Last Edit: July 22, 2016, 06:32:58 am by Handoko »

Bart

  • Hero Member
  • *****
  • Posts: 5290
    • Bart en Mariska's Webstek
Re: Is there a function that opens an internet web page?
« Reply #8 on: July 22, 2016, 12:41:28 pm »
Unless you're using a very cryptic distro, you can assume that Linux is XDG compliant. In that case:
Code: Bash  [Select][+][-]
  1. $ xdg-open <url>
should suffice.

AFAIK that's exactly what LCLIntf.OpenUrl() does on *nix platform.

Bart

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Is there a function that opens an internet web page?
« Reply #9 on: July 22, 2016, 02:01:23 pm »
AFAIK that's exactly what LCLIntf.OpenUrl() does on *nix platform.
Extended to widely known browsers for non-XDG compliant distros:
Code: Pascal  [Select][+][-]
  1. {%MainUnit ../lclintf.pas}
  2.  
  3. // Locates the default browser associated in the system
  4.  
  5. function FindBrowserExecutable(const ShortFilename: String; out ABrowser: String): Boolean; inline;
  6. begin
  7.   ABrowser := SearchFileInPath(ShortFilename + GetExeExt, '',
  8.                     GetEnvironmentVariableUTF8('PATH'), PathSeparator,
  9.                     [sffDontSearchInBasePath]);
  10.   Result := (ABrowser <> '');
  11. end;
  12.  
  13. const
  14.   PredefinedBrowserStrings: array[1..10] of String = (
  15.     'xdg-open',
  16.     'htmlview',
  17.     'firefox',
  18.     'mozilla',
  19.     'galeon',
  20.     'konqueror',
  21.     'safari',
  22.     'netscape',
  23.     'opera',
  24.     'iexplore'
  25.   );
  26.  
  27. function FindPredefinedBrowser(out ABrowser, AParams: String): Boolean;
  28. var
  29.   i: Integer;
  30. begin
  31.   ABrowser := '';
  32.   AParams := '"%s"';
  33.   for i := Low(PredefinedBrowserStrings) to High(PredefinedBrowserStrings) do
  34.     if FindBrowserExecutable(PredefinedBrowserStrings[i], ABrowser) then Break;
  35.   Result := (ABrowser <> '');
  36. end;
  37.  

Bart

  • Hero Member
  • *****
  • Posts: 5290
    • Bart en Mariska's Webstek
Re: Is there a function that opens an internet web page?
« Reply #10 on: July 23, 2016, 12:02:23 am »
AFAIK that's exactly what LCLIntf.OpenUrl() does on *nix platform.
Extended to widely known browsers for non-XDG compliant distros:

I know, take a look at svn log  O:-)

FWIW: Here is some code that tries to find a default browser in *nix (got (parts of) it from Graeme).

Bart

 

TinyPortal © 2005-2018