C Board  

Go Back   C Board > Platform Specific Boards > Windows Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 02-08-2010, 07:17 PM   #1
Registered User
 
Join Date: Feb 2010
Posts: 3
Loading DLL with specific path

Hi all,

I have a C project in which I need to load a dll, using LoadLibrary() I am able to load it if the dll is on the executable folder or in the system32 folder, but I cannot load it from a specific path. In my case, my app will use a dll located in Program Files\Common Files and LoadLibrary doesn't load when I write the full path.

How do I do this?


Regards,
Rafael
doublenach is offline   Reply With Quote
Old 02-08-2010, 11:50 PM   #2
Super Moderator
 
Bubba's Avatar
 
Join Date: Aug 2001
Posts: 7,813
LoadLibrary() will fail if the path is invalid. If it is not loading the DLL then it cannot find it. The DLL link process starts in the same folder as your EXE. If the DLL cannot be found there then Windows looks in the System32 folder. If it cannot be found there a message box will popup stating it could not find a component necessary to run your EXE.

Another way that LoadLibrary() can fail is if the DLL has not been registered on the computer via regsvr32. DLLs that have not been registered for use on the machine cannot be linked with, used, or dynamically loaded. If LoadLibrary() returns a NULL HMODULE then you must call GetLastError() to get the error information. The Platform SDK explains this in great detail.

http://msdn.microsoft.com/en-us/libr...75(VS.85).aspx
__________________
If you aim at everything you will hit something but you won't know what it is.

Last edited by Bubba; 02-08-2010 at 11:54 PM.
Bubba is offline   Reply With Quote
Old 02-09-2010, 11:34 AM   #3
Registered User
 
Join Date: Jan 2010
Posts: 230
Quote:
Originally Posted by doublenach View Post
In my case, my app will use a dll located in Program Files\Common Files and LoadLibrary doesn't load when I write the full path.
Sounds like the path is incorrect or your application doesn't have read access to that directory. What does GetLastError() say?

Quote:
Originally Posted by Bubba View Post
The DLL link process starts in the same folder as your EXE. If the DLL cannot be found there then Windows looks in the System32 folder. If it cannot be found there a message box will popup stating it could not find a component necessary to run your EXE.
Unless you specify an absolute path. Even says so in the link you posted:
Quote:
If the string specifies a full path, the function searches only that path for the module.

Quote:
Originally Posted by Bubba View Post
Another way that LoadLibrary() can fail is if the DLL has not been registered on the computer via regsvr32. DLLs that have not been registered for use on the machine cannot be linked with, used, or dynamically loaded.
Not true.
Isn't the use of regsvr32 to store a registry mapping between COM/ActiveX GUIDs and their respective dll location?
_Mike is offline   Reply With Quote
Old 02-09-2010, 01:01 PM   #4
Registered User
 
Join Date: Feb 2010
Posts: 3
Hi,

thanks for the reply, as suggested I tried GetLastError and I received 'The specified module could not be found.'

My code looks like this:
LoadLibrary("C:\Program Files\Common Files\example.dll");

The dll is there, if I copy/paste into the file browser there it is. I am thinking it is the syntax, how should it be? I really searched this but I can't find any example on microsoft website and also couldn't find any other code example on the internet that loads a dll from a specific path.

Regards,
Rafael
doublenach is offline   Reply With Quote
Old 02-09-2010, 01:40 PM   #5
the hat of redundancy hat
 
nvoigt's Avatar
 
Join Date: Aug 2001
Location: Hannover, Germany
Posts: 2,769
You need to use double backslashes, as always:

LoadLibrary("C:\\Program Files\\Common Files\\example.dll");

Please note that your code will only run on machines that are installed in English. "Program Files" is localized in XP
__________________
hth
-nv

She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

When in doubt, read the FAQ.
Then ask a smart question.
nvoigt is offline   Reply With Quote
Old 02-09-2010, 01:50 PM   #6
Senior software engineer
 
brewbuck's Avatar
 
Join Date: Mar 2007
Location: Portland, OR
Posts: 5,763
Quote:
Originally Posted by Bubba View Post
Another way that LoadLibrary() can fail is if the DLL has not been registered on the computer via regsvr32. DLLs that have not been registered for use on the machine cannot be linked with, used, or dynamically loaded. If LoadLibrary() returns a NULL HMODULE then you must call GetLastError() to get the error information. The Platform SDK explains this in great detail.
That's untrue -- DLLs do not need to be registered. regsvr32 is normally used to register and unregister DLLs which provide system-level services, such as COM or ActiveX components. If you don't believe me, try it -- create a simple DLL project which exports a function, build the DLL, and call LoadLibrary() on it. It works fine.
__________________
"Congratulations on your purchase. To begin using your quantum computer, set the power switch to both off and on simultaneously." -- raftpeople@slashdot
brewbuck is offline   Reply With Quote
Old 02-09-2010, 04:48 PM   #7
Registered User
 
Join Date: Jan 2010
Posts: 230
Quote:
Originally Posted by nvoigt View Post
You need to use double backslashes, as always:

LoadLibrary("C:\\Program Files\\Common Files\\example.dll");

Please note that your code will only run on machines that are installed in English. "Program Files" is localized in XP
Indeed, and so is "Common Files". And it also depends on if you're on a 32 or 64-bit OS.

If you want version-independant code, use the %CommonProgramFiles% environment variable together with ExpandEnvironmentStrings Function (Windows)
Example:
Code:
TCHAR *path = L"%CommonProgramFiles%\\microsoft shared\\Windows Live\\WindowsLiveLogin.dll";
TCHAR *expandedPath = NULL;
// Set length to 0 the first pass to find out required buffer length
DWORD numChars = ExpandEnvironmentStrings(path, expandedPath , 0);
expandedPath = new TCHAR[numChars];
ExpandEnvironmentStrings(path, expandedPath, numChars);
HMODULE hm = LoadLibrary(expandedPath);
delete [] expandedPath;
(proof of concept code, add your own error handling)
_Mike is offline   Reply With Quote
Old 02-09-2010, 07:22 PM   #8
Super Moderator
 
Bubba's Avatar
 
Join Date: Aug 2001
Posts: 7,813
Quote:
That's untrue -- DLLs do not need to be registered.
Interesting. I actually ran into this problem recently and regsvr32 fixed it. But again I am probably making a bad assumption that the DLL is a COM or ActiveX object since that is what I am used to. But yeah it would not make sense to force registration of every DLL. My apologies.

Quote:
Unless you specify an absolute path. Even says so in the link you posted:
Which is why I posted the link. There is a lot of good information there about the issue. Personally I never specify absolute paths so have never ran into this particular problem while using LoadLibrary().
__________________
If you aim at everything you will hit something but you won't know what it is.
Bubba is offline   Reply With Quote
Old 02-09-2010, 09:10 PM   #9
Registered User
 
Join Date: Jan 2010
Posts: 230
Quote:
Originally Posted by Bubba View Post
Which is why I posted the link. There is a lot of good information there about the issue. Personally I never specify absolute paths so have never ran into this particular problem while using LoadLibrary().
Ah okey, then I misunderstood your post. I thought you meant that you cannot use absolute paths. Sorry.
_Mike is offline   Reply With Quote
Old 02-09-2010, 10:23 PM   #10
I have a hat!?
 
Join Date: Apr 2008
Posts: 178
If you need more debugging info than just GetLastError() for failed dll loads, here's how you get some internal debug spew from Windows.
adeyblue is offline   Reply With Quote
Old 02-13-2010, 06:44 AM   #11
Registered User
 
Join Date: Feb 2010
Posts: 3
Thank you all very much, I am now able to load the DLL using double backslashes.


Rafael
doublenach is offline   Reply With Quote
Reply

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
Dll Injection Question zenox C Programming 13 03-15-2008 10:54 AM
Loading bitmap in dll Mithoric Windows Programming 2 12-22-2003 01:53 PM
Using class with DLL greg2 C++ Programming 2 09-12-2003 05:24 AM
Exporting Object Hierarchies from a DLL andy668 C++ Programming 0 10-20-2001 01:26 PM


All times are GMT -6. The time now is 02:05 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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