LoadLibaray Not Working

Posted on 2010-11-17
Last Modified: 2012-05-10
Hi can someone tell me is there a different approch to using loadlibaray from within a DLL than from within an exe.

For exampe when I load a dll from a exe the lib loads fine but if i try and load it from within another dll the dll is not loaded
Question by:nchannon
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +1

Expert Comment

ID: 34155587
In most cases, LoadLibrary failed when a dependency (another DLL for example) is missing and/or not reachable by the system (neither in current directory, or by using PATH environment variable).

> Where are your DLL / your exe?
> What is your project type? MFC? Other?

LVL 86

Expert Comment

ID: 34155598
Can you be a bit more specific? I.e. calling 'LoadLibrary()' from within 'DllMain()' is not a good idea, whereas using that anywhere else should be fine...

Expert Comment

ID: 34156369
You can check if there's a dependency issue for a specific DLL or EXE using the freeware Dependency Walker

If there's no dependency issue, please elaborate as the usage of LoadLibrary from in an EXE or DLL is similar...
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 34158266
Hi im using MFC and I load the DLL from an app like this HMODULE hLib = LoadLibrary(_T("mydll.dll"))
when I call LoadLibrary mydll.dll loads fine but if I call LoadLibaray(_T("mydll.dll") )from within another dll eg:
seconddll.dll which then trys and load LoadLibrary(_T("mydll.dlll")) it dosn't load and hLib returns null.
My dll,s are all in the same directory
LVL 86

Expert Comment

ID: 34158325
What does 'GetLastError()' report?

Author Comment

ID: 34158391
Ok found the problem DLL's were in the wrong directory for debugging they should of been the the main apps directory that was calling the first dll which inturn called the second dll I had them only in the root directory of the first dll so they werent avalible when the main app was running in debug mode

Expert Comment

ID: 34162873
Classical issue...don't worry, you're not the first, and surely not the last to face it ;-)

Expert Comment

ID: 34162883
Last thing : if you work with MFC, you'd better use ::AfxLoadLibrary.

Author Comment

ID: 34163154
Yes feel a little silly on that one but hay im not the first and not the last :)
I tried AfxLoadLibrary can you tell me what the difference is between AFXLoadLibrary and LoadLibrary. What would be the benifits between the two

Accepted Solution

Xper4net earned 500 total points
ID: 34163994
AfxLoadLibrary is thread safe, and moreover, it performs special handling when your DLL is an "extension DLL" (DLL which uses DLL version of MFC).
Even if it is not your case, it should be one day, so it's better to use it from now.

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Dialogbox API leak? 18 119
Microsoft C++ code failing in executable that worked 9 146
max float value 3 75
print bytes of an integer 6 48
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question