Recent

Author Topic: Determine where the runing binary program is stored  (Read 1136 times)

Jonas Maebe

  • Hero Member
  • *****
  • Posts: 801
Re: Determine where the runing binary program is stored
« Reply #15 on: October 07, 2020, 05:42:14 pm »
Do what I say, not what I do?

More "don't make the same mistake as someone else made in the 1990s". Please stop being so confrontational all the time, especially when people are trying to help.

Fred vS

  • Hero Member
  • *****
  • Posts: 1982
    • StrumPract is the musicians best friend
Re: Determine where the runing binary program is stored
« Reply #16 on: October 07, 2020, 05:53:26 pm »
Do what I say, not what I do?

More "don't make the same mistake as someone else made in the 1990s". Please stop being so confrontational all the time, especially when people are trying to help.

Ok, ok, sorry.

(But I am trying to help too and I understand the frustration of Handoko to know that paramstr[0] should not be used in Linux.)
« Last Edit: October 07, 2020, 06:08:47 pm by Fred vS »
I use Lazarus 2.0.6 32/64 and FPC 3.2.0 32/64 on Debian 10.2 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64 and Mac OS X Snow Leopard 32.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt, Carbon.

https://github.com/fredvs
https://gitlab.com/fredvs

Fred vS

  • Hero Member
  • *****
  • Posts: 1982
    • StrumPract is the musicians best friend
Re: Determine where the runing binary program is stored
« Reply #17 on: October 07, 2020, 05:56:34 pm »
Why fpc do use this in his compiler build ?:
That is not the compiler build, that is RTL code to initialise paramstr[0]. And as mentioned, you cannot rely on paramstr[0] actually containing "the" path to the binary on platforms other than Windows (and DOS and OS/2). Even that code already mentions a case where it won't work at all (admittedly under a very old Linux version).

That said, the compiler itself also does use that value (with a fallback to search the PATH for the binary name) in order to load a configuration file. That is legacy code from back when people thought it was a good idea to emulate the Windows compiler binary behaviour under Linux (which happened sometime before 1999). It turns out it is not, but at the same time changing that now will undoubtedly break things for some people that rely on it.

Ooops, I did not see your previous post.

Ok, thanks it is clear now.

Peace.

Fre;D
I use Lazarus 2.0.6 32/64 and FPC 3.2.0 32/64 on Debian 10.2 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64 and Mac OS X Snow Leopard 32.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt, Carbon.

https://github.com/fredvs
https://gitlab.com/fredvs

nanobit

  • Jr. Member
  • **
  • Posts: 93
Re: Determine where the runing binary program is stored
« Reply #18 on: October 07, 2020, 09:14:16 pm »
On Windows, ParamStr(0) seems to be abstracted away from commandline params.
uses internally windows.GetModuleFileName(), which is ansi version only and limited to 260 characters.

Search for "ParamStr(0)" in Lazarus/FPC source shows quite a lot of example calls.

winni

  • Hero Member
  • *****
  • Posts: 1992
Re: Determine where the runing binary program is stored
« Reply #19 on: October 07, 2020, 11:02:19 pm »
Hi!

Since a lot of years we got

Code: Pascal  [Select][+][-]
  1. MyApp := Application.exeName;

It gives you the absolute path and the name of the binary.
You can extract the directory path with ExstractFilePath

But you even don't have to do that. In one of the last versions they added

Application .location

Which gives you the path to your binary.

In the background they do nothing but to get paramstr(0)

Winni
Winni


Fred vS

  • Hero Member
  • *****
  • Posts: 1982
    • StrumPract is the musicians best friend
Re: Determine where the runing binary program is stored
« Reply #20 on: October 07, 2020, 11:34:40 pm »
Hi!

Since a lot of years we got

Code: Pascal  [Select][+][-]
  1. MyApp := Application.exeName;

It gives you the absolute path and the name of the binary.
You can extract the directory path with ExstractFilePath

But you even don't have to do that. In one of the last versions they added

Application .location

Which gives you the path to your binary.

In the background they do nothing but to get paramstr(0)

Winni
Winni

This will work OK only for Windows (see previous post about Linux).
« Last Edit: October 07, 2020, 11:37:12 pm by Fred vS »
I use Lazarus 2.0.6 32/64 and FPC 3.2.0 32/64 on Debian 10.2 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64 and Mac OS X Snow Leopard 32.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt, Carbon.

https://github.com/fredvs
https://gitlab.com/fredvs

GAN

  • Sr. Member
  • ****
  • Posts: 305
Re: Determine where the runing binary program is stored
« Reply #21 on: October 07, 2020, 11:47:25 pm »
Application.Location works fine Linux/Windows.
Lazarus 2.0.8 FPC 3.0.4 Linux Mint Mate 19.3
Zeos 7.2.6 - Sqlite 3.32.3

https://gitlab.com/users/GAN__/projects

Jonas Maebe

  • Hero Member
  • *****
  • Posts: 801
Re: Determine where the runing binary program is stored
« Reply #22 on: October 08, 2020, 12:11:08 am »
Application.Location works fine Linux/Windows.
It's a "best effort" thing under Linux. As explained above, you cannot rely on it being correct under Linux, and you should only ever use it for informative/debugging purposes.

Fred vS

  • Hero Member
  • *****
  • Posts: 1982
    • StrumPract is the musicians best friend
Re: Determine where the runing binary program is stored
« Reply #23 on: October 08, 2020, 12:41:19 am »
Application.Location works fine Linux/Windows.
It's a "best effort" thing under Linux. As explained above, you cannot rely on it being correct under Linux, and you should only ever use it for informative/debugging purposes.

Hello Jonas.
You are totally right and agree with you.

But in some case it is useful to know where the binary is stored in Linux.

What do you think about the workaround:
Code: Pascal  [Select][+][-]
  1. fpReadLink('/proc/self/exe')
?

I did many test, using symlinks, running the exe stored on a server, ... and did not find any problems.

In what situation could appear problems?

I use Lazarus 2.0.6 32/64 and FPC 3.2.0 32/64 on Debian 10.2 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64 and Mac OS X Snow Leopard 32.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt, Carbon.

https://github.com/fredvs
https://gitlab.com/fredvs

Jonas Maebe

  • Hero Member
  • *****
  • Posts: 801
Re: Determine where the runing binary program is stored
« Reply #24 on: October 08, 2020, 12:43:01 am »
In what situation could appear problems?
At least in the situations that I mentioned in my first reply in this thread: https://forum.lazarus.freepascal.org/index.php/topic,51692.msg379972.html#msg379972 . There are probably more.

GAN

  • Sr. Member
  • ****
  • Posts: 305
Re: Determine where the runing binary program is stored
« Reply #25 on: October 08, 2020, 12:46:37 am »
I use it (and a lot) in all my programs that I develop under GNU/Linux and then compile them for Windows.
I'm just mentioning my experience with that command.
Lazarus 2.0.8 FPC 3.0.4 Linux Mint Mate 19.3
Zeos 7.2.6 - Sqlite 3.32.3

https://gitlab.com/users/GAN__/projects

Fred vS

  • Hero Member
  • *****
  • Posts: 1982
    • StrumPract is the musicians best friend
Re: Determine where the runing binary program is stored
« Reply #26 on: October 08, 2020, 01:40:09 am »
In what situation could appear problems?
At least in the situations that I mentioned in my first reply in this thread: https://forum.lazarus.freepascal.org/index.php/topic,51692.msg379972.html#msg379972 . There are probably more.

Ooops, sorry, I did not try the case of hard link (I usually use soft link).

But in that case, could a pre-workaround be used to analyse the inode of the link and so determine where it is stored?

Imho, the program got moved while it was running has less chance to appear in real world.
« Last Edit: October 08, 2020, 02:15:35 am by Fred vS »
I use Lazarus 2.0.6 32/64 and FPC 3.2.0 32/64 on Debian 10.2 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64 and Mac OS X Snow Leopard 32.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt, Carbon.

https://github.com/fredvs
https://gitlab.com/fredvs

Fred vS

  • Hero Member
  • *****
  • Posts: 1982
    • StrumPract is the musicians best friend
Re: Determine where the runing binary program is stored
« Reply #27 on: October 08, 2020, 03:31:48 am »

But in that case, could a pre-workaround be used to analyse the inode of the link and so determine where it is stored?


Ok, non sense because for each same inode no way to know what is the original vs the hard-link.

To resume, is it ok to say that ParamStr(0) should eventually be used in Linux only for the original binary or a symlink (soft link) and only if you are sure that the binary will not move?

« Last Edit: October 08, 2020, 03:42:10 am by Fred vS »
I use Lazarus 2.0.6 32/64 and FPC 3.2.0 32/64 on Debian 10.2 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64 and Mac OS X Snow Leopard 32.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt, Carbon.

https://github.com/fredvs
https://gitlab.com/fredvs

PascalDragon

  • Hero Member
  • *****
  • Posts: 2405
  • Compiler Developer
Re: Determine where the runing binary program is stored
« Reply #28 on: October 08, 2020, 09:18:33 am »
On Windows, ParamStr(0) seems to be abstracted away from commandline params.
uses internally windows.GetModuleFileName(), which is ansi version only and limited to 260 characters.

In trunk this was changed to GetModuleFileNameW around five months ago. Maybe this will be merged to 3.2.1.

 

TinyPortal © 2005-2018