Recent

Author Topic: Detecting how an application was launched  (Read 6117 times)

Red_prig

  • Full Member
  • ***
  • Posts: 153
Re: Detecting how an application was launched
« Reply #30 on: June 22, 2024, 03:58:36 pm »
Alternatively you can do it like this:
Code: Pascal  [Select][+][-]
  1.  Handle:=GetStdHandle(STD_OUTPUT_HANDLE);
  2.  GetFileType(Handle)=FILE_TYPE_CHAR;

Thaddy

  • Hero Member
  • *****
  • Posts: 16168
  • Censorship about opinions does not belong here.
If I smell bad code it usually is bad code and that includes my own code.

Red_prig

  • Full Member
  • ***
  • Posts: 153
Re: Detecting how an application was launched
« Reply #32 on: June 22, 2024, 04:16:28 pm »
So what's the problem? If stdout is FILE_TYPE_CHAR then an output terminal is connected to the application

geraldholdsworth

  • Full Member
  • ***
  • Posts: 214
Re: Detecting how an application was launched
« Reply #33 on: June 22, 2024, 04:44:35 pm »
So what's the problem? If stdout is FILE_TYPE_CHAR then an output terminal is connected to the application

Unfortunately, that doesn't appear to yield the required result. It only tells me whether it is opening into a console or a GUI, not where it is being launched from (both are zero when opening into a GUI and non-zero when into a console). Either way, the result certainly does not equal FILE_TYPE_CHAR. Sorry.

geraldholdsworth

  • Full Member
  • ***
  • Posts: 214
Re: Detecting how an application was launched
« Reply #34 on: June 22, 2024, 10:12:04 pm »
Regarding macOS - I think this might have some mileage in it...if I can work out how to access it from Lazarus. It should be a simple case of including a unit...just which one? Foundation, according to my install, can't be found (but I know it's there because I opened it).

MarkMLl

  • Hero Member
  • *****
  • Posts: 8027
Re: Detecting how an application was launched
« Reply #35 on: June 22, 2024, 10:17:19 pm »
Regarding macOS - I think this might have some mileage in it...if I can work out how to access it from Lazarus. It should be a simple case of including a unit...just which one? Foundation, according to my install, can't be found (but I know it's there because I opened it).

Assuming that MacOS is a BSD derivative... does it allow you to get at /proc which should be a standard filesystem?

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

geraldholdsworth

  • Full Member
  • ***
  • Posts: 214
Re: Detecting how an application was launched
« Reply #36 on: June 22, 2024, 10:19:42 pm »
Assuming that MacOS is a BSD derivative... does it allow you to get at /proc which should be a standard filesystem?

No - it just returned an empty string. The folder 'proc' doesn't exist either, where in Linux is does.

MarkMLl

  • Hero Member
  • *****
  • Posts: 8027
Re: Detecting how an application was launched
« Reply #37 on: June 22, 2024, 10:31:11 pm »
Assuming that MacOS is a BSD derivative... does it allow you to get at /proc which should be a standard filesystem?

No - it just returned an empty string. The folder 'proc' doesn't exist either, where in Linux is does.

That's weird, it certainly exists on SunOS. Look, most definitely not trying to offend but you /were/ treating it as a directory containing files, weren't you?

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

duralast

  • New Member
  • *
  • Posts: 23
Re: Detecting how an application was launched
« Reply #38 on: June 22, 2024, 11:10:15 pm »
MacOS does not have /proc, but you can use sudo fs_usage

In Linux, if it was launched by the desktop, e.g., Gnome, it will have a .desktop file and search for Exec= that will return something like gnome-control-center or cinnamon-settings-users.

« Last Edit: June 22, 2024, 11:41:53 pm by duralast »

geraldholdsworth

  • Full Member
  • ***
  • Posts: 214
Re: Detecting how an application was launched
« Reply #39 on: June 23, 2024, 12:14:07 pm »
I have found the answer to my later question, about processInfo on macOS. The answer lies in this wiki page. And it doesn't yeald the information I was after. :(
Back to the drawing board.

That's weird, it certainly exists on SunOS. Look, most definitely not trying to offend but you /were/ treating it as a directory containing files, weren't you?
Don't worry, you're not offending me - I value your input Mark. And, yes, I was (I used the UNIX code given on the first page of this post).

MacOS does not have /proc, but you can use sudo fs_usage
Sorry for being inexperienced in this area, but how would I do that in Lazarus?

geraldholdsworth

  • Full Member
  • ***
  • Posts: 214
Re: Detecting how an application was launched
« Reply #40 on: June 23, 2024, 12:29:04 pm »
Ah...well, actually I may have jumped the gun a bit. The results from processInfo.environment are:
Run via app bundle:
Code: [Select]
{
    "COMMAND_MODE" = unix2003;
    HOME = "/Users/geraldholdsworth";
    LOGNAME = geraldholdsworth;
    PATH = "/usr/bin:/bin:/usr/sbin:/sbin";
    SHELL = "/bin/zsh";
    "SSH_AUTH_SOCK" = "/private/tmp/com.apple.launchd.35SDrFixwG/Listeners";
    TMPDIR = "/var/folders/sp/r7dyc78d19j36ftwtmckmr3w0000gn/T/";
    USER = geraldholdsworth;
    "XPC_FLAGS" = 0x0;
    "XPC_SERVICE_NAME" = "application.com.company.project1.36549144.37180253";
    "__CFBundleIdentifier" = "com.company.project1";
    "__CF_USER_TEXT_ENCODING" = "0x1F5:0:2";
}

Run via Terminal, or Run via Filer (direct to binary):
Code: [Select]
{
    HOME = "/Users/geraldholdsworth";
    "HOMEBREW_CELLAR" = "/opt/homebrew/Cellar";
    "HOMEBREW_PREFIX" = "/opt/homebrew";
    "HOMEBREW_REPOSITORY" = "/opt/homebrew";
    INFOPATH = "/opt/homebrew/share/info:";
    LANG = "en_GB.UTF-8";
    LOGNAME = geraldholdsworth;
    MANPATH = "/opt/homebrew/share/man::";
    OLDPWD = "/Users/geraldholdsworth/Library/Mobile Documents/com~apple~CloudDocs/Programming/Lazarus/Old and Test Projects/Console and GUI/lib";
    PATH = "/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:/Library/Apple/usr/bin:/Applications/VMware Fusion.app/Contents/Public";
    PWD = "/Users/geraldholdsworth/Library/Mobile Documents/com~apple~CloudDocs/Programming/Lazarus/Old and Test Projects/Console and GUI/lib/x86_64-darwin";
    SHELL = "/bin/zsh";
    SHLVL = 1;
    "SSH_AUTH_SOCK" = "/private/tmp/com.apple.launchd.35SDrFixwG/Listeners";
    TERM = "xterm-256color";
    "TERM_PROGRAM" = "Apple_Terminal";
    "TERM_PROGRAM_VERSION" = 453;
    "TERM_SESSION_ID" = "1CDE8358-C3EF-492A-955D-6A36DDECAC31";
    TMPDIR = "/var/folders/sp/r7dyc78d19j36ftwtmckmr3w0000gn/T/";
    USER = geraldholdsworth;
    "XPC_FLAGS" = 0x0;
    "XPC_SERVICE_NAME" = 0;
    "_" = "/Users/geraldholdsworth/Library/Mobile Documents/com~apple~CloudDocs/Programming/Lazarus/Old and Test Projects/Console and GUI/lib/x86_64-darwin/./project1";
    "__CFBundleIdentifier" = "com.apple.Terminal";
    "__CF_USER_TEXT_ENCODING" = "0x1F5:0:2";
}

So, it looks like there is something we can use here.

MarkMLl

  • Hero Member
  • *****
  • Posts: 8027
Re: Detecting how an application was launched
« Reply #41 on: June 23, 2024, 02:46:36 pm »
OK, but couldn't you get that from running  env  in a terminal? I think that's a standard unix program rather than a Linuxism.
And from a terminal, what happens if you do  ls /proc  ?

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

geraldholdsworth

  • Full Member
  • ***
  • Posts: 214
Re: Detecting how an application was launched
« Reply #42 on: June 23, 2024, 04:37:55 pm »
OK, but couldn't you get that from running  env  in a terminal? I think that's a standard unix program rather than a Linuxism.
Oh yeah - displays a whole load of other info. But that is info that is more relevant to the running Terminal instance, rather than the Lazarus project that I got the info for. Plus, using NSProcessInfo.ProcessInfo.environment gives it to you in way that you can manipulate.
I'm now using:
Code: Pascal  [Select][+][-]
  1. {$IFDEF Darwin}
  2.  i:=0;
  3.  while(i<NSProcessInfo.ProcessInfo.environment.count-1)
  4.    and(NSProcessInfo.ProcessInfo.environment.allKeys.objectAtIndex(i).UTF8String<>'XPC_SERVICE_NAME')do inc(i);
  5.  Result:=(NSProcessInfo.ProcessInfo.environment.allKeys.objectAtIndex(i).UTF8String='XPC_SERVICE_NAME')
  6.       and(NSProcessInfo.ProcessInfo.environment.allValues.objectAtIndex(i).UTF8String='0');
  7. {$ENDIF}
To determine if the application was launched from Filer or Terminal (I'm sure that there is a better way to access the key/value, but I just couldn't get objectForKey('XPC_SERVICE_NAME') to work).

And from a terminal, what happens if you do  ls /proc  ?
Code: [Select]
zsh: no such file or directory: /proc

geraldholdsworth

  • Full Member
  • ***
  • Posts: 214
Re: Detecting how an application was launched
« Reply #43 on: June 23, 2024, 06:02:51 pm »
Worked it out. Code now looks like this:
Code: Pascal  [Select][+][-]
  1. program project1;
  2.  
  3. {$mode objfpc}{$H+}
  4. {$IFDEF Darwin}
  5. {$modeswitch objectivec1}
  6. {$ENDIF}
  7.  
  8. uses
  9.  {$IFDEF Windows}Windows{$ENDIF}
  10.  {$IFDEF Linux}BaseUnix{$ENDIF}
  11.  {$IFDEF Darwin}typinfo,CocoaAll{$ENDIF}
  12. ;
  13.  
  14. function IsRunFromConsole: Boolean;
  15. {$IFDEF Windows}
  16. var
  17.  StartUp: StartUpInfoA;
  18. {$ENDIF}
  19. begin
  20.  Result:=False;//Default, if not covered by Windows, Linux or Darwin
  21. {$IFDEF Windows}
  22.  StartUp.dwFlags:=0;//Prevents 'variable not initialised' message
  23.  GetStartupInfo(StartUp);
  24.  Result:=(StartUp.dwFlags AND 1)<>1;
  25. {$ENDIF}
  26. {$IFDEF Linux}
  27.  Result:=fpReadLink('/proc/'+fpGetppid.ToString+'/exe')<>'';
  28. {$ENDIF}
  29. {$IFDEF Darwin}
  30.  Result:=NSProcessInfo.ProcessInfo.environment.objectForKey(NSStr('XPC_SERVICE_NAME')).UTF8String='0';
  31. {$ENDIF}
  32. end;
  33.  
  34. begin
  35.  if IsRunFromConsole then WriteLn('Run from console')
  36.  else WriteLn('Run from Filer/Explorer');
  37. end.

I've encapsulated it inside a program as you'd need the units and the modeswitch.

Thank you all for your assistance, it has been invaluable. I've certainly learnt a lot from this excercise. Of course, like all programs, it's never finished and probably can be improved upon.

 

TinyPortal © 2005-2018