Recent

Author Topic: Find installation directory of a program  (Read 2981 times)

WJSwanepoel

  • New Member
  • *
  • Posts: 25
Find installation directory of a program
« on: January 20, 2023, 08:35:14 am »
Is it possible to find the installation directory of a program programmatically in FreePascal/Lazarus?

WooBean

  • Sr. Member
  • ****
  • Posts: 278
Re: Find installation directory of a program
« Reply #1 on: January 20, 2023, 09:03:27 am »
Hi,
try to visit page https://www.freepascal.org/docs-html/rtl/system/paramstr.html
It may be something near you are looking for.
Platforms: Win7/64, Linux Mint Ulyssa/64

Bogen85

  • Hero Member
  • *****
  • Posts: 702
Re: Find installation directory of a program
« Reply #2 on: January 20, 2023, 09:09:42 am »
Is it possible to find the installation directory of a program programmatically in FreePascal/Lazarus?

Something like this:

Code: Pascal  [Select][+][-]
  1. program FindBinaryDir;
  2.  
  3. {$APPTYPE CONSOLE}
  4.  
  5. uses
  6.   SysUtils;
  7.  
  8. var
  9.   ExeName: string;
  10.   ExePath: string;
  11.  
  12. begin
  13.   ExeName := ParamStr(0);
  14.   ExePath := ExtractFilePath(ExeName);
  15.   Writeln('The directory that the application''s binary is in is: ' + ExePath);
  16. end.

Bogen85

  • Hero Member
  • *****
  • Posts: 702
Re: Find installation directory of a program
« Reply #3 on: January 20, 2023, 09:13:40 am »
This was answered already here: https://forum.lazarus.freepascal.org/index.php/topic,6309.0.html
(don't reply in there, continue the thread here)

WJSwanepoel

  • New Member
  • *
  • Posts: 25
Re: Find installation directory of a program
« Reply #4 on: January 20, 2023, 09:40:29 am »
This is not what I am looking for. The examples given will give me the path of the running program.

What I for instance want is to find the installation path of say excel.exe or lazarus.exe.

Bogen85

  • Hero Member
  • *****
  • Posts: 702
Re: Find installation directory of a program
« Reply #5 on: January 20, 2023, 10:10:30 am »
Code: Pascal  [Select][+][-]
  1. program FindBinaryDir;
  2. {$APPTYPE CONSOLE}
  3. {$mode objfpc}
  4. {$H+}
  5.  
  6. uses
  7.   SysUtils;
  8.  
  9. function SearchPath(const exe: string): string;
  10.   begin
  11.     result := ExeSearch(exe, GetEnvironmentVariable('PATH'));
  12.   end;
  13.  
  14. var
  15.   ExeName: string;
  16.   ExePath: string;
  17.  
  18. begin
  19.   if ParamCount > 0 then begin
  20.     ExeName := SearchPath(ParamStr(1));
  21.     ExePath := ExtractFilePath(ExeName);
  22.     Writeln('The directory that the application ', ExeName, ' has its binary is: ' + ExePath);
  23.   end;
  24. end.

This works on Linux. Not sure about Windows.

Code: Text  [Select][+][-]
  1. $ fpc appdir3.pas
  2. Free Pascal Compiler version 3.3.1 [2023/01/19] for x86_64
  3. Copyright (c) 1993-2023 by Florian Klaempfl and others
  4. Target OS: Linux for x86-64
  5. Compiling appdir3.pas
  6. appdir3.pas(2,2) Warning: APPTYPE is not supported by the target OS
  7. Linking appdir3
  8. 24 lines compiled, 0.2 sec, 374896 bytes code, 166632 bytes data
  9. 1 warning(s) issued

Pass in the executable to search for on the command line:

Code: Text  [Select][+][-]
  1. $ ./appdir3 gcc
  2. The directory that the application /usr/bin/gcc has its binary is: /usr/bin/

« Last Edit: January 20, 2023, 10:34:43 am by Bogen85 »

Bogen85

  • Hero Member
  • *****
  • Posts: 702
Re: Find installation directory of a program
« Reply #6 on: January 20, 2023, 10:17:07 am »
So ExeSearch combined with what it is the PATH environment variable might be what you are looking for.

Bogen85

  • Hero Member
  • *****
  • Posts: 702
Re: Find installation directory of a program
« Reply #7 on: January 20, 2023, 10:32:42 am »
It should work on Windows https://www.freepascal.org/docs-html/rtl/sysutils/exesearch.html as the PathSeparator is OS specific, and is what is in the PATH variable.

https://www.freepascal.org/docs-html/rtl/system/pathseparator.html
« Last Edit: January 20, 2023, 03:35:05 pm by Bogen85 »

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Find installation directory of a program
« Reply #8 on: January 20, 2023, 11:02:57 am »
On Windows you do usual iterate over the "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\" registry, parse it to your needs and its done within a few seconds.
You do not get the executable name, you get the installation name of product.
key "DisplayName" = this is how a product calls itself
key "InstallLocation" = this is the root folder of installed product

ps: and "HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\" registry.... sorry forget to mention earlier.

above is used when product was installed via a setup.exe that add entries to my mentioned registry, lazarus/fpc does not use it, for that you really need to search in a long long long way all your drives for a matching executable name.
« Last Edit: January 20, 2023, 11:13:56 am by KodeZwerg »
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Find installation directory of a program
« Reply #9 on: January 20, 2023, 11:40:46 am »
Fastest way on windows machines to find whatever file withing a second is to install "Everything".
That app index all your files and you can search by typing a name.

In attachment a german installation of everything with results for "Lazarus.exe"
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

balazsszekely

  • Guest
Re: Find installation directory of a program
« Reply #10 on: January 20, 2023, 11:41:20 am »
@WJSwanepoel
Quote
Is it possible to find the installation directory of a program programmatically in FreePascal/Lazarus?
Try attached project.

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Find installation directory of a program
« Reply #11 on: January 20, 2023, 11:59:42 am »
@WJSwanepoel
Quote
Is it possible to find the installation directory of a program programmatically in FreePascal/Lazarus?
Try attached project.
Perfect translation of what I said with registry! Great job GetMem!
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

balazsszekely

  • Guest
Re: Find installation directory of a program
« Reply #12 on: January 20, 2023, 12:25:16 pm »
@KodeZwerg
Quote
Perfect translation of what I said with registry! Great job GetMem!
Thanks. It was already discussed in the following thread: https://forum.lazarus.freepascal.org/index.php/topic,60440.0.html
However if you compare the results with the list from "Add remove programs" you will find a few differences, not many, just a few. So windows knows something we don't. I'm not motivated enough to find out the secret though.  :)

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Find installation directory of a program
« Reply #13 on: January 20, 2023, 02:39:28 pm »
@GetMem, my own tool shows way more, I try to see what you do differently.
Code: Pascal  [Select][+][-]
  1. Caption := Caption + ' (' + IntToStr(lwInst.Items.Count) + ')';
This I've added as last line to know how many items you find.

In attachment is the result of my own tool visible 80 vs 212  :o
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

balazsszekely

  • Guest
Re: Find installation directory of a program
« Reply #14 on: January 20, 2023, 02:49:52 pm »
@GetMem, my own tool shows way more, I try to see what you do differently.
Code: Pascal  [Select][+][-]
  1. Caption := Caption + ' (' + IntToStr(lwInst.Items.Count) + ')';
This I've added as last line to know how many items you find.

In attachment is the result of my own tool visible 80 vs 212  :o
My example ignores Microsoft entries by default, see the last parameter of GetInstalledPrograms method. If you set it to false, you will get much more entries.

 

TinyPortal © 2005-2018