Loading a DLL from a Folder?

New to FreeBASIC? Post your questions here.
Post Reply
datwill310
Posts: 355
Joined: May 29, 2015 20:37

Loading a DLL from a Folder?

Post by datwill310 »

Hi. I would like to load a DLL from a folder in the same path as the application file loading it (using #inclib). Eg:
bin
\-- dllfile.dll
app.exe
I run app.exe after using this:
#inclib "dllfile"
Compiles fine, but comes out with "unable to find dll" as a system warning.
However, when moved to the same path as app.exe:
bin
app.exe
dllfile.dll
Loading works fine.

I would prefer to tuck away ugly DLL files into that bin folder, but don't know how.

AFAIK, -p and #libpath only add a path relative to the FB installed directory, and I don't want to use a full path because I want to release this application to the public.

I've tried #inclib "bin\dllfile", but that didn't compile.
srvaldez
Posts: 3383
Joined: Sep 25, 2005 21:54

Re: Loading a DLL from a Folder?

Post by srvaldez »

just a suggestion, make a dll-folder with your dll's and add that folder to the PATH variable and see if it works.
Search Path Used by Windows to Locate a DLL

Visual Studio 2015 Other Versions

For the latest documentation on Visual Studio 2017, see Visual Studio 2017 Documentation.
With both implicit and explicit linking, Windows first searches for "known DLLs", such as Kernel32.dll and User32.dll. Windows then searches for the DLLs in the following sequence:
The directory where the executable module for the current process is located.
The current directory.
The Windows system directory. The GetSystemDirectory function retrieves the path of this directory.
The Windows directory. The GetWindowsDirectory function retrieves the path of this directory.
The directories listed in the PATH environment variable.
datwill310
Posts: 355
Joined: May 29, 2015 20:37

Re: Loading a DLL from a Folder?

Post by datwill310 »

srvaldez wrote:just a suggestion, make a dll-folder with your dll's and add that folder to the PATH variable and see if it works.
Search Path Used by Windows to Locate a DLL

Visual Studio 2015 Other Versions

For the latest documentation on Visual Studio 2017, see Visual Studio 2017 Documentation.
With both implicit and explicit linking, Windows first searches for "known DLLs", such as Kernel32.dll and User32.dll. Windows then searches for the DLLs in the following sequence:
The directory where the executable module for the current process is located.
The current directory.
The Windows system directory. The GetSystemDirectory function retrieves the path of this directory.
The Windows directory. The GetWindowsDirectory function retrieves the path of this directory.
The directories listed in the PATH environment variable.
This doesn't work, unfortunately... I have tried pre-restart and post-restart.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Loading a DLL from a Folder?

Post by MrSwiss »

You might have to use #LibPath "dir-name_containing_lib" before you #Include the lib (dll or static).
datwill310
Posts: 355
Joined: May 29, 2015 20:37

Re: Loading a DLL from a Folder?

Post by datwill310 »

MrSwiss wrote:You might have to use #LibPath "dir-name_containing_lib" before you #Include the lib (dll or static).
This method is unattractive since:
FB Doc wrote:Adds a library search path to the linker's list of search paths as if it had been specified on the command line with the '-p' option.

Paths are relative to the working directory where fbc was invoked and not relative to the directory of the source file.
http://www.freebasic.net/wiki/wikka.php ... gPplibpath

I have tried something along the lines of #libpath "bin" in the main source file, but this didn't work either.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Loading a DLL from a Folder?

Post by MrSwiss »

Why then, don't you simply stick to the most common way:
ExeDir = Lib-Dir
(you can seldom have it both ways: elegant, and being easy too)
datwill310
Posts: 355
Joined: May 29, 2015 20:37

Re: Loading a DLL from a Folder?

Post by datwill310 »

MrSwiss wrote:Why then, don't you simply stick to the most common way:
ExeDir = Lib-Dir
(you can seldom have it both ways: elegant, and being easy too)
I guess so :/ it just seems so nice and neat :) and I've seen plenty of other programs do such a thing (fbc itself seems to be an example, and fbc is programmed in FB, so with that reasoning, could you say that this can be done in FB?)
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Loading a DLL from a Folder?

Post by MrSwiss »

Another possible way to get there:

Code: Select all

#Inclib [Once] ExePath + "\libdir\libname"
This isn't tried code, just an idea ...
datwill310
Posts: 355
Joined: May 29, 2015 20:37

Re: Loading a DLL from a Folder?

Post by datwill310 »

MrSwiss wrote:Another possible way to get there:

Code: Select all

#Inclib [Once] ExePath + "\libdir\libname"
This isn't tried code, just an idea ...
The compiler prevented me from using exepath with #include, it seems ;).
fxm
Moderator
Posts: 12159
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Loading a DLL from a Folder?

Post by fxm »

ExecPath is only set at runtime obviously.

Try with __Path__
(absolute path of the source file at the time of compilation)
datwill310
Posts: 355
Joined: May 29, 2015 20:37

Re: Loading a DLL from a Folder?

Post by datwill310 »

fxm wrote:ExecPath is only set at runtime obviously.

Try with __Path__
(absolute path of the source file at the time of compilation)
Thanks for the help! But I think expressions aren't allowed in preprocessor statements (eg __PATH__ + "\bin" or __PATH__ & "\bin")?
St_W
Posts: 1627
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: Loading a DLL from a Folder?

Post by St_W »

You cannot modify the DLL search order at load time from your executable in Windows, afaik (however, e.g. Linux allows that, afair). You have (at least) two options:
- don't let the OS Loader search and load the DLL files, but load them manually (dylibload, ...). This could be some additional effort however as you have to do everything manually that's normally done by the loader.
- modify the search path before your executable is loaded. For example use a separate launcher-application that changes the DLL search path and then starts your application; or use a simple batch script for that

The thing is that "#inclib" and "-p" are just relevant at compile time, but don't change the DLL search order at load-time/runtime, so those won't help.
datwill310
Posts: 355
Joined: May 29, 2015 20:37

Re: Loading a DLL from a Folder?

Post by datwill310 »

St_W wrote: - don't let the OS Loader search and load the DLL files, but load them manually (dylibload, ...). This could be some additional effort however as you have to do everything manually that's normally done by the loader.
- modify the search path before your executable is loaded. For example use a separate launcher-application that changes the DLL search path and then starts your application; or use a simple batch script for that

The thing is that "#inclib" and "-p" are just relevant at compile time, but don't change the DLL search order at load-time/runtime, so those won't help.
Thanks for the info!

- I would prefer to include the library using the header file and DLL, as not only would loading the library manually might be an issue, I would also miss out on all the defined constants, types etc. I would have to define them manually unless I modified the FreeImage.bi header file.
- Sounds confusing XD I wouldn't know how to!

I guess I'll just have to put up with the DLL's being in the same folder ;)! It wouldn't matter I guess: people could create links, and so can the installer (I'll create using InnoSetup).
St_W
Posts: 1627
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: Loading a DLL from a Folder?

Post by St_W »

datwill310 wrote:- Sounds confusing XD I wouldn't know how to!
It sounds and maybe looks more complicated than it is. You just have to add the dll directory to the PATH environment variable. Here's a batch file that adds the "my_dlls" subdirectory to the PATH variable and executes "my_application.exe" from the same folder where the .bat script is located.

launch.bat

Code: Select all

@echo off
set APP_PATH=%~dp0
set PATH=%APP_PATH%my_dlls;%PATH%
%APP_PATH%my_application.exe %*
datwill310
Posts: 355
Joined: May 29, 2015 20:37

Re: Loading a DLL from a Folder?

Post by datwill310 »

St_W wrote:
datwill310 wrote:- Sounds confusing XD I wouldn't know how to!
It sounds and maybe looks more complicated than it is. You just have to add the dll directory to the PATH environment variable. Here's a batch file that adds the "my_dlls" subdirectory to the PATH variable and executes "my_application.exe" from the same folder where the .bat script is located.

launch.bat

Code: Select all

@echo off
set APP_PATH=%~dp0
set PATH=%APP_PATH%my_dlls;%PATH%
%APP_PATH%my_application.exe %*
The installer (InnoSetup) could probably do this, right? But your method would ensure that the app runs wherever, and not just in the installed directory. Then again, the user shouldn't move installed programs around. The choices, the choices! Again, Thanks for the help!
Post Reply