Loading a DLL that is not in the same directory as the executable

This is a discussion on Loading a DLL that is not in the same directory as the executable within the Windows Programming forums, part of the Platform Specific Boards category; Hi, I have a DLL with a corresponding import library (.lib) that I want to load-time link into my executable. ...

  1. #1
    Registered User starcatcher's Avatar
    Join Date
    Feb 2008
    Location
    Australia
    Posts
    45

    Question Loading a DLL that is not in the same directory as the executable

    Hi,

    I have a DLL with a corresponding import library (.lib) that I want to load-time link into my executable. (ie not using the LoadLibrary() functions etc...) When I link in the import library by adding it to the linker command line, the resulting executable produces an error when it is run, saying that the program can't find the DLL file. When I copy the DLL file into the same folder as the executable though, everything works perfectly. I can understand why this is happening as at no point I 'told' the program where to find the DLL. How could I do this?

    In other words, how can you use a DLL that is not in the same directory as the executable file?

    I am using Microsoft Visual C++ 2003 to build the executable file and even though it's probably not important, I created the DLL myself and could rebuild it at any time.

    Thank you in advance,

    Philipp
    I program solely as a hobby so this is definitely not homework.
    Best thing that's been said to me:
    I was so pleasantly surprised to see another post in this thread, and yours was brilliant; a fitting end to a subject (matrix manipulations of all kinds) that is quite intriguing.
    Read this thread to find out why...
    Cannibalism

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Set your PATH to include the directory of the DLL.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User starcatcher's Avatar
    Join Date
    Feb 2008
    Location
    Australia
    Posts
    45

    Exclamation How to do that...

    I found a similar sort of answer in the help files. I don't know however what exactly the 'PATH' variable is referring to. Where would I find it?

    If you're not familiar with MSVC, could you explain what the 'PATH' variable is?

    Thanks,

    Philipp
    I program solely as a hobby so this is definitely not homework.
    Best thing that's been said to me:
    I was so pleasantly surprised to see another post in this thread, and yours was brilliant; a fitting end to a subject (matrix manipulations of all kinds) that is quite intriguing.
    Read this thread to find out why...
    Cannibalism

  4. #4
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    PATH variable is one of the system's default environment variables. It specifies the application and DLL search paths.

    Every process has its own PATH variable, which can be changed by using GetEnvironmentVariable(), appending your path to it and then calling SetEnvironmentVariable(). The easy way to change it is using the command-line instruction 'set' (in a .bat file), so a simple launcher would look like this:
    Code:
    SET PATH=%PATH%;.\dlls\
    yourapp.exe
    The global PATH variable (aka the default of every new process) is stored in the registry and can be manually edited from somewhere in the Control Panel.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  5. #5
    Registered User starcatcher's Avatar
    Join Date
    Feb 2008
    Location
    Australia
    Posts
    45

    Exclamation Alternative methods?

    So, if I understood correctly, the only way to change a process's own PATH variable is by calling GetEnvironmentVariable() etc... (I require the output to be simply an .exe file, so the .bat option is not available to me).

    Is it possible that in MSVC, you can change the (future) process's own PATH variable in the development environment (IDE)? Like, is it possible that there is an entry in the Property Pages of the project or something like that, that would let you set the PATH variable?

    Another thought: Although it wouldn't help me, could you for example set the PATH variable from a Makefile or something like that?

    I do not really want to use GetEnvironmentVariable etc... as I do not want to include the windows header for just two functions...

    Any thoughts?

    Thanks,

    Philipp
    I program solely as a hobby so this is definitely not homework.
    Best thing that's been said to me:
    I was so pleasantly surprised to see another post in this thread, and yours was brilliant; a fitting end to a subject (matrix manipulations of all kinds) that is quite intriguing.
    Read this thread to find out why...
    Cannibalism

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You can change the global PATH variable by changing it in the system control panel, under the "Advanced" tab, "Environment variables", and just stick whatever path you want onto the PATH variable. It can be an absolute path (c:\mystuff\myproject\someplace\dlls\) or a relative path (..\dlls).

    Note that you need to restart your Visual Studio after this change, as it doesn't change all processes current environment variables, just any new process started AFTER you have changed it.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,691
    For an "installation" of your app, just keep the dll and exe in the same directory.

    For your ide/build environment, set the "current working directory" for running and debugging to the directory where the dll resides.

    What I usually do is have a root "bin\release\" and "bin\debug\" - then all related projects have post-build command to copy binaries to those folders. Running and debugging from the IDE uses those as the CWD.

    gg

  8. #8
    Registered User starcatcher's Avatar
    Join Date
    Feb 2008
    Location
    Australia
    Posts
    45

    Thumbs up Resolution in sight

    I reckon that adding a path to the global PATH variable is the way to go for me.

    Thanks to everyone,

    Philipp
    I program solely as a hobby so this is definitely not homework.
    Best thing that's been said to me:
    I was so pleasantly surprised to see another post in this thread, and yours was brilliant; a fitting end to a subject (matrix manipulations of all kinds) that is quite intriguing.
    Read this thread to find out why...
    Cannibalism

  9. #9
    Registered User starcatcher's Avatar
    Join Date
    Feb 2008
    Location
    Australia
    Posts
    45

    Question Further...

    I added the global variable and my program works now without the dll being in the same folder.

    Incidentally, would someone be able to tell me how to add a post-build event that copies the output of a build into another folder? I know where to find it in the property pages of the project, but I have no idea what the command line would be to copy the output file(s).

    Can anyone help me?

    Thanks,

    Philipp
    I program solely as a hobby so this is definitely not homework.
    Best thing that's been said to me:
    I was so pleasantly surprised to see another post in this thread, and yours was brilliant; a fitting end to a subject (matrix manipulations of all kinds) that is quite intriguing.
    Read this thread to find out why...
    Cannibalism

  10. #10
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,859
    Bring up the projects properties window (Right click on the project in the solution explorer)

    Select 'Build Events'

    Select Post Build event

    With folders like

    .\Project\DLL
    .\Project\BIN

    add this to copy the DLL to the 'BIN' folder, over any file already there

    copy "$(TargetPath)" "$(TargetDir)..\BIN" /y
    [EDIT]
    ..\ means UP one folder from the current project folder (and the DEBUG folder does not count in default MSVC setup).
    So ..\..\ is UP two folders
    and ..\..\New Folder is UP two folders and down into the 'New Folder' folder.....
    Last edited by novacain; 12-13-2008 at 12:40 AM.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  11. #11
    Registered User starcatcher's Avatar
    Join Date
    Feb 2008
    Location
    Australia
    Posts
    45

    Arrow

    Thanks, I'll try that...

    Philipp
    I program solely as a hobby so this is definitely not homework.
    Best thing that's been said to me:
    I was so pleasantly surprised to see another post in this thread, and yours was brilliant; a fitting end to a subject (matrix manipulations of all kinds) that is quite intriguing.
    Read this thread to find out why...
    Cannibalism

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Loading Struct from dll
    By peyman_k in forum C++ Programming
    Replies: 2
    Last Post: 08-15-2008, 04:12 PM
  2. Couple errors please help :-D
    By JJJIrish05 in forum C Programming
    Replies: 9
    Last Post: 03-06-2008, 02:54 AM
  3. Loading a DLL at runtime
    By filler_bunny in forum Windows Programming
    Replies: 9
    Last Post: 02-23-2003, 08:03 AM
  4. .lib vs .h vs .dll
    By Shadow12345 in forum C++ Programming
    Replies: 13
    Last Post: 01-01-2003, 05:29 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21