Page 1 of 1
Forum

Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!

Topics for particular software or systems: Start your topic link with the name of the application or system.
For example “MacOS X – Your question“, or “MS Word – Your Tip or Trick“.

Please note that switching to another language when reading a post will not bring you to the same post, in Dutch, as there is no translation for that post!



Share:
Notifications
Clear all

[Solved] Lazarus Pascal - Detect if running in IDE (better said: in project folder)

1 Posts
1 Users
0 Reactions
249 Views
 Hans
(@hans)
Famed Member Admin
Joined: 12 years ago
Posts: 2859
Topic starter  

Seems detecting if your application is started from the Lazarus IDE can be a little bit of a challenge.
Under Delphi one could use the DebugHook variable, but that does not work under Lazarus.

Having seen plenty of trick - none of the automatically doing what its supposed to do, I figured I go for this approach;

function RunningInProjectFolder:boolean;
begin
  Result := FileExists(ChangeFileExt(ParamStr(0), '.lpr'));
end; 

This detects if your application was started from the project directory by seeing if the .lpr file exists.
This works cross-platform, but the caveat is that it will think its running in the IDE if you started the executable in the project directory. IDE running or not. So that is why the function is called "RunningInProjectFolder" as this is more accurate.

I mainly use this under macOS, since the executable yourprojectfolder/yourapplication.app/Contents/macOS/yourapplication is actually linked to yourprojectfolder/yourapplication and ParamStr(0) is returning this (incorrect) location of the actual executable and not the link in the .app bundle.  

So when pointing to resources in the .app bundle I need to use a different path.

For example resources in e yourprojectfolder/yourapplication.app/Contents/Resources would be accessed as "yourapplication.app/Contents/Resources" when running from the project folder.

However the path to those Resources will be "../Contents/Resources" when the application (.app bundle) is started outside of the project folder.

So here I use "RunningInProjectFolder" to set the proper path in my code.


   
ReplyQuote
Share: