Loading a dynamic library from path determinead ar runtime

I want to use an external 3rd party dynamic library.

The app targets MacOS, Windows and Linux.

The library’s SDK documentation tells me, I should either

  • ship the library with my app and copy it to the app’s installation folder or
  • find one that is installed on the system in a path pointed to by an environment variable

I would like to do the latter, if there is an installed version, and fall back to the one shipped with my app if there is none installed (or the environment variable is not defined or not valid). This way my app was able to use an updated version of the library without the need for an app update or for a need for the user to replace the library shipped with my app in it’s installation folder (if that is even possible with today’s app packages and software protection schemes like checksums, signatures and the like).

But is it even possible to load a library from a path defined at runtime in Xojo? As far as I know, both, a Define statement an an External Method declaration need a constant for the library, and a constant declaration needs a fixed, static value at compile time.

Did I understand these restrictions correctly?

Is there a workaround?

I found examples that use the construction @executable_path/../Frameworks/mylib.dylib in constant declarations for macOS, but I could not find documentation on this (and maybe more magic tokens) not even on the possibility to use a defined constant in the inspector preceded by a #-sign (are there more magic modifiers there?).

If the path is already in an environment variable, then the system might be able to locate the library by filename only.

Alternatively, on Windows if you can find the DLL file at runtime then you can add its parent folder to your app’s DLL search path. You just need to be sure to add the search path before any of the Declare statements go searching for the DLL.

e.g.

#If TargetWindows
   Dim DllParentFolder As FolderItem ' assumed to be the folder with the DLL in it

   Declare Function SetDllDirectoryW Lib "Kernel32" (DllPath As WString) As Boolean
   Call SetDllDirectoryW(DllParentFolder.NativePath)

   Soft Declare Function FooBar Lib "MyDll.dll" () As Integer 
#EndIf