Loading a DLL from a Folder?
-
- Posts: 355
- Joined: May 29, 2015 20:37
Loading a DLL from a Folder?
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.
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.
Re: Loading a DLL from a Folder?
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.
-
- Posts: 355
- Joined: May 29, 2015 20:37
Re: Loading a DLL from a Folder?
This doesn't work, unfortunately... I have tried pre-restart and post-restart.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.
Re: Loading a DLL from a Folder?
You might have to use #LibPath "dir-name_containing_lib" before you #Include the lib (dll or static).
-
- Posts: 355
- Joined: May 29, 2015 20:37
Re: Loading a DLL from a Folder?
This method is unattractive since:MrSwiss wrote:You might have to use #LibPath "dir-name_containing_lib" before you #Include the lib (dll or static).
http://www.freebasic.net/wiki/wikka.php ... gPplibpathFB 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.
I have tried something along the lines of #libpath "bin" in the main source file, but this didn't work either.
Re: Loading a DLL from a Folder?
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)
ExeDir = Lib-Dir
(you can seldom have it both ways: elegant, and being easy too)
-
- Posts: 355
- Joined: May 29, 2015 20:37
Re: Loading a DLL from a Folder?
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 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)
Re: Loading a DLL from a Folder?
Another possible way to get there:This isn't tried code, just an idea ...
Code: Select all
#Inclib [Once] ExePath + "\libdir\libname"
-
- Posts: 355
- Joined: May 29, 2015 20:37
Re: Loading a DLL from a Folder?
The compiler prevented me from using exepath with #include, it seems ;).MrSwiss wrote:Another possible way to get there:This isn't tried code, just an idea ...Code: Select all
#Inclib [Once] ExePath + "\libdir\libname"
Re: Loading a DLL from a Folder?
ExecPath is only set at runtime obviously.
Try with __Path__
(absolute path of the source file at the time of compilation)
Try with __Path__
(absolute path of the source file at the time of compilation)
-
- Posts: 355
- Joined: May 29, 2015 20:37
Re: Loading a DLL from a Folder?
Thanks for the help! But I think expressions aren't allowed in preprocessor statements (eg __PATH__ + "\bin" or __PATH__ & "\bin")?fxm wrote:ExecPath is only set at runtime obviously.
Try with __Path__
(absolute path of the source file at the time of compilation)
Re: Loading a DLL from a Folder?
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.
- 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.
-
- Posts: 355
- Joined: May 29, 2015 20:37
Re: Loading a DLL from a Folder?
Thanks for the info!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.
- 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).
Re: Loading a DLL from a Folder?
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.datwill310 wrote:- Sounds confusing XD I wouldn't know how to!
launch.bat
Code: Select all
@echo off
set APP_PATH=%~dp0
set PATH=%APP_PATH%my_dlls;%PATH%
%APP_PATH%my_application.exe %*
-
- Posts: 355
- Joined: May 29, 2015 20:37
Re: Loading a DLL from a Folder?
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!St_W wrote: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.datwill310 wrote:- Sounds confusing XD I wouldn't know how to!
launch.batCode: Select all
@echo off set APP_PATH=%~dp0 set PATH=%APP_PATH%my_dlls;%PATH% %APP_PATH%my_application.exe %*