C Board  

Go Back   C Board > Platform Specific Boards > Windows Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 12-12-2008, 06:01 AM   #1
Registered User
 
starcatcher's Avatar
 
Join Date: Feb 2008
Location: Australia
Posts: 41
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:
Quote:
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
starcatcher is offline   Reply With Quote
Old 12-12-2008, 06:19 AM   #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   Reply With Quote
Old 12-12-2008, 06:25 AM   #3
Registered User
 
starcatcher's Avatar
 
Join Date: Feb 2008
Location: Australia
Posts: 41
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:
Quote:
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
starcatcher is offline   Reply With Quote
Old 12-12-2008, 06:44 AM   #4
Reverse Engineer
 
maxorator's Avatar
 
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 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 duck is irrelevant to my point.
maxorator is offline   Reply With Quote
Old 12-12-2008, 07:10 AM   #5
Registered User
 
starcatcher's Avatar
 
Join Date: Feb 2008
Location: Australia
Posts: 41
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:
Quote:
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
starcatcher is offline   Reply With Quote
Old 12-12-2008, 07:46 AM   #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   Reply With Quote
Old 12-12-2008, 07:48 AM   #7
Registered User
 
Codeplug's Avatar
 
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   Reply With Quote
Old 12-12-2008, 08:05 AM   #8
Registered User
 
starcatcher's Avatar
 
Join Date: Feb 2008
Location: Australia
Posts: 41
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:
Quote:
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
starcatcher is offline   Reply With Quote
Old 12-12-2008, 08:04 PM   #9
Registered User
 
starcatcher's Avatar
 
Join Date: Feb 2008
Location: Australia
Posts: 41
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:
Quote:
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
starcatcher is offline   Reply With Quote
Old 12-13-2008, 12:29 AM   #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:
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.....
__________________
"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   Reply With Quote
Old 12-13-2008, 07:05 AM   #11
Registered User
 
starcatcher's Avatar
 
Join Date: Feb 2008
Location: Australia
Posts: 41
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:
Quote:
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
starcatcher is offline   Reply With Quote
Reply

Tags
dll, error, import, library, link

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 11:41 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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