Lazarus equivalent of ShellExecute for windows and linux? lazarus / 8-Other ---------------------------------------------------------------------- Anonymous · 2005-12-26 10:32 Hello, I wish to open a file after it's created by my application in the default application associated with it. Under Delphi, I used to have for instance : ShellExecute(Handle,'Open','C:\MyPage.htm',nil,nil,SW_SHOWDEFAULT); I would like to find an equivalent of the previous line of code that would allow me to open the *.html file in the default browser on both linux and windows. Can anyone provide me with a tip? Thanks in advance ---------------------------------------------------------------------- Anonymous · 2005-12-26 13:22 PS : i read the post about TProcess http://www.lazarus.freepascal.org/index.php?name=PNphpBB2&file=viewtop ic&t=855&highlight=open+file But I can't know the commandline in advance since I don't know which browser will be used to display the file. ---------------------------------------------------------------------- Anonymous · 2005-12-26 13:27 Re: Lazarus equivalent of ShellExecute for windows and linux? There is no WM-independent default-browser in Linux. What you could do use: Ask the user for a browser to choose the first time or use somthing like the code below to try some well known browsers and use the first that's found. Code: [Select]Procedure Openurl(Url:String); Var     Browsers:Array[0..3] of String;     Command, Extension, Returnval:String;     i:integer; Begin     Browsers[0]:='kfmclient openURL';     Browsers[1]:='mozilla';     Browsers[2]:='netscape';     Extension:=lowercase(ExtractFileExt(URL));     if (Extension='.htm') Or (Extension='.html') then       Begin       For i:=0 to High(Browsers) do         Begin         If i=High(Browsers) then           Begin           If InputQuery('No browser found!',           'Please enter command for starting your browser',             Returnval) then             Begin               Command:=Returnval+' '''+URL+'''';               Shell(Command)             End;           End           Else           Begin             Command:=Browsers[i]+' '''+URL+'''';             If Shell(Command) = 0 then break;           End;         End;       End       Else       Begin       //Same game here If you like....         Command:='kedit '''+URL+'''';         Shell(Command);       End; End; ---------------------------------------------------------------------- felipemdc · 2005-12-26 17:10 I think there is a way to do that, and I´ve seen applications do that. They call konqueror on my machine, althought I use firefox for common navigation. I´m not exactly sure how they do that. You will need to research, but here are some possible places to search: * The Free Desktop Standards. http://www.freedesktop.org/  if it is here, then it is not Windows manager specific * The GNOME API * The KDE API. I would bet there is something about it here, but is KDE-only. * Perhaps gtk has a function for this? ---------------------------------------------------------------------- Anonymous · 2005-12-26 21:43 Thanks a lot to both of you... I was wondering too since KDE and Gnome declare somewhere a default browser where this is stocked. In the meantime, the above code will do the trick. Thanks again ---------------------------------------------------------------------- Reenen · 2008-08-02 21:20 Do we now (3years down the line) have a better way? Did linux progress to have a default browser? I would really like to get code to just open a URL for my help files for my project. ---------------------------------------------------------------------- bonmario · 2008-08-03 05:35 I found some code one year ago and modified it. Now i have 2 functions. With this, in case of Linux, i found the desktop manager (for now only Gnome or Kde): Code: [Select]  function  DeterminaDesktopManager:TipoDeskMang;   begin     Result:=dmBoh; //Desktop Manager sconosciuto         if (GetEnvironmentVariable('GNOME_DESKTOP_SESSION_ID') <> '') or        (GetEnvironmentVariable('DESKTOP_SESSION') = 'gnome') then begin       Result:=dmGnome;     end else begin       if (GetEnvironmentVariable('KDE_FULL_SESSION') <> '') or          (GetEnvironmentVariable('DESKTOP_SESSION') = 'kde') then begin         Result:=dmKde;       end;     end;   end; And with this i open the file (it works also with HTML files): Code: [Select]  function  ApriFile(WrkHandle:THandle; NomeFile:String; ApplvForz:String = ''; ParmAggv:String = ''):Integer;   var WrkProcess:TProcess;       Comando, Parametri:String;       VFWindows:Boolean;       {$IFDEF LINUX}         DesktopManager:TipoDeskMang;         WrkApp:String;       {$ENDIF}   begin     Result:=-1;     if (not FileExists(NomeFile)) then begin       EmettiErrore(7, NomeFile);     end else begin       if (NomeFile <> '') then begin         //Determino qual'è il sistema operativo su cui sto lavorando         {$IFDEF MSWINDOWS}           VFWindows:=True;         {$ELSE}           VFWindows:=False;         {$ENDIF}         if VFWindows and (ApplvForz = '') then begin           {$IFDEF MSWINDOWS}             //Sto lavorando in WIndows e non mi è stato passato il nome dell'eseguibile con cui devo             //aprire il file: lancio l'apertura standard ...             Result:=ShellExecute(WrkHandle, 'open', PChar(MettiVirgolette(NomeFile)), '', nil, SW_SHOWNORMAL);           {$ENDIF}         end else begin           //... se invece lavoro in Linux oppure mi viene passato il nome dell'eseguibile, preparo           //le variabili per lanciare il TProcess                     //Valorizzazione del comando da lanciare           Comando:='';           if (ApplvForz <> '') then begin             Comando:=ApplvForz;           end else begin             {$IFDEF LINUX}               DesktopManager:=DeterminaDesktopManager;               if (DesktopManager <> dmBoh) then begin                 case DesktopManager of                   dmGnome:begin                     Comando:='gnome-open';                   end;                                     dmKde:begin                     Comando:='kfmclient exec';                   end;                 end;               end;             {$ENDIF}           end;                     //Valorizzazione dei parametri del comando           if (Comando = '') then begin             EmettiErrore(0, 'Errore nella codifica del comando, contattare il programmatore !!!');             Halt;           end else begin             Parametri:=MettiVirgolette(NomeFile);             if (ParmAggv <> '') then begin               Parametri:=ParmAggv +                          ' ' +                          Parametri;             end;                         //Adesso che è tutto pronto, posso creare e lanciare il processo             WrkProcess:=TProcess.Create(nil);               WrkProcess.Options:=[poNoConsole];               WrkProcess.CommandLine:=Comando +                                       ' ' +                                       Parametri;               WrkProcess.Execute;               Result:=WrkProcess.ExitStatus;             WrkProcess.Free;           end;         end;       end;     end;   end; Hi, Mario ---------------------------------------------------------------------- Reenen · 2008-08-03 21:47 Sorry... How do I use it? My vision is... to have when I click a button I do: Code: [Select] HelpFilePath := '.' + pathdelim + 'help' + pathdelim + 'main.htm'; ApriFile(HelpFilePath);  // (using your function name) What do I need to change from your code to make that happen. ApriFile(WrkHandle:THandle; NomeFile:String; ApplvForz:String = ''; ParmAggv:String = ''):Integer; How do I know what the WrkHandle is...?  The next is obviously the filename, and then the last stuff is optional, so I'm not too concerned with that.