How does the Import Library work? Details?

Viewed 48930

I know this may seem quite basic to geeks. But I want to make it crystal clear.

When I want to use a Win32 DLL, usually I just call the APIs like LoadLibrary() and GetProcAdderss(). But recently, I am developing with DirectX9, and I need to add d3d9.lib, d3dx9.lib, etc files.

I have heard enough that LIB is for static linking and DLL is for dynamic linking.

So my current understanding is that LIB contains the implementation of the methods and is statically linked at link time as part of the final EXE file. While DLL is dynamic loaded at runtime and is not part of the final EXE file.

But sometimes, there're some LIB files coming with the DLL files, so:

  • What are these LIB files for?
  • How do they achieve what they are meant for?
  • Is there any tools that can let me inspect the internals of these LIB files?

Update 1

After checking wikipedia, I remember that these LIB files are called import library. But I am wondering how it works with my main application and the DLLs to be dynamically loaded.

Update 2

Just as RBerteig said, there're some stub code in the LIB files born with the DLLs. So the calling sequence should be like this:

My main application --> stub in the LIB --> real target DLL

So what information should be contained in these LIBs? I could think of the following:

  • The LIB file should contain the fullpath of the corresponding DLL; So the DLL could be loaded by the runtime.
  • The relative address (or file offset?) of each DLL export method's entry point should be encoded in the stub; So correct jumps/method calls could be made.

Am I right on this? Is there something more?

BTW: Is there any tool that can inspect an import library? If I can see it, there'll be no more doubts.

5 Answers

In my mind, there are two method to link dll to exe.

  1. Use dll and the import library (.lib file) implicitly

  2. Use functions like loadlibrary() explicitly

Related