Can a DLL call/load another DLL?

23,749

Solution 1

You can use load-time dynamic linking or run-time dynamic linking in your DLL in the same way as in the executable. The only restriction is not to call LoadLibrary from your DllMain function to avoid deadlocks.

Solution 2

LoadLibrary and GetProcAddress are, but one of, your friends ...

Solution 3

If this dll has .lib file, you just add it to linker input and import its functions statically. If it don't, there are some tools to generate .lib file from .dll.

Also you can import functions dynamically, using LoadLibrary and GetProcAddress.
MSDN says that you can't call LoadLibrary from DllMain. But in most cases nothing bad happens.

Solution 4

Typically you link to the DLL via an export library in your project. Then the DLL functions can be called by your program, provided the DLL is in the program's path at runtime.

It's also possible (but a lot more work) to avoid link-time resolution of the required functions by manually loading the DLL and getting the required function addresses, but that should not be necessary if the third-party DLL supports the usual link-time mechanisms.

Share:
23,749
samulisoderlund
Author by

samulisoderlund

Updated on December 19, 2020

Comments

  • samulisoderlund
    samulisoderlund over 3 years

    I want to call some third party DLL routines from my own DLL. I'm just not sure how to do that in C++.

  • seand
    seand over 13 years
    import library is appropriate as long as the .dll can be safely assumed to be there before the app starts. Otherwise it needs LoadLibrary() etc.
  • Steve Townsend
    Steve Townsend over 13 years
    @seand - sure. I think the 99% case is to link to the export library and have the DLL in place at runtime, though. Depends on whether the library in questions has this, that's all. Most well-written ones do it this way.
  • seand
    seand over 13 years
    Not sure about 99% but perhaps 80-90%. This is an issue if you link to a dll that might not exist on the machine. (such as mapi32.dll). If you link as an import library your app will render a user-unfriendly dialog at startup. But if you dynamically link you can give a friendlier response and perhaps operate in a degraded mode or something.