![]() |
| |||||||
![]() |
| | LinkBack | Thread Tools | Display Modes |
| | #1 | |
| Registered User Join Date: Feb 2008 Location: Australia
Posts: 41
| 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: Quote:
| |
| starcatcher is offline | |
| | #2 |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| 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. |
| matsp is offline | |
| | #3 | |
| Registered User Join Date: Feb 2008 Location: Australia
Posts: 41
| 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: Quote:
| |
| starcatcher is offline | |
| | #4 |
| Reverse Engineer Join Date: Aug 2005 Location: Estonia
Posts: 2,236
| 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 duck is irrelevant to my point. |
| maxorator is offline | |
| | #5 | |
| Registered User Join Date: Feb 2008 Location: Australia
Posts: 41
| 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: Quote:
| |
| starcatcher is offline | |
| | #6 |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| 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. |
| matsp is offline | |
| | #7 |
| Registered User Join Date: Mar 2003
Posts: 3,844
| 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 |
| Codeplug is offline | |
| | #8 | |
| Registered User Join Date: Feb 2008 Location: Australia
Posts: 41
| 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: Quote:
| |
| starcatcher is offline | |
| | #9 | |
| Registered User Join Date: Feb 2008 Location: Australia
Posts: 41
| 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: Quote:
| |
| starcatcher is offline | |
| | #10 | |
| train spotter Join Date: Aug 2001 Location: near a computer
Posts: 3,359
| 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 Quote:
..\ 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.....
__________________ "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 Last edited by novacain; 12-13-2008 at 12:40 AM. | |
| novacain is offline | |
| | #11 | |
| Registered User Join Date: Feb 2008 Location: Australia
Posts: 41
| 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: Quote:
| |
| starcatcher is offline | |
![]() |
| Tags |
| dll, error, import, library, link |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Loading Struct from dll | peyman_k | C++ Programming | 2 | 08-15-2008 03:12 PM |
| Couple errors please help :-D | JJJIrish05 | C Programming | 9 | 03-06-2008 02:54 AM |
| Loading a DLL at runtime | filler_bunny | Windows Programming | 9 | 02-23-2003 08:03 AM |
| .lib vs .h vs .dll | Shadow12345 | C++ Programming | 13 | 01-01-2003 05:29 AM |