Re: how do static import libraries actually work?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



kilik3000@xxxxxxxxx wrote:

One thing I've never really understood are import libraries for DLLs
on Windows.

For example, let's say that I use a function from user32.dll in my
code. Without linking to user32.lib in VS I get a link error, so I
link to user32.lib and all is well.

I guess what I don't understand is how this jives with static linking
and dynamic linking.

From what I understand, static linking is when you take object code
and just plop it into your binary.

Dynamic linking (on windows at least) is when you call LoadLibray()
and then GetProcAddress() and then make your call on a function
pointer.

What happens when you statically link to a DLL import library (i.e.
user32.lib)?

Does the import library object code contain implicit calls to
LoadLibray() and GetProcAddress that are performed on your behalf?

It is kind of an interesting process. The object file produced by the
compiler just calls, for example, CreateWindowA. The object file doesn't
know where that entry point will come from, it relies on the linker to fix
it.

In this particular case, the CreateWindowA entry point will be found in
user32.lib. But the library entry for CreateWindowA doesn't actually
contain any code. Instead, it contains a special linker table that says
"this entry point is found in CreateWindowA in user32.dll". The linker
creates an entry in the import descriptor table in the resulting
executable.

Although it is most common for the library entry point name to match the
DLL entry point name, and it is also most common for all of the imports in
a single library to point to a single DLL, neither is required. I could
create an import library called xxx.lib that contained an entry for
MyEntryPoint that points to YourEntryPoint in zzz.dll.

When the executable is loaded, the OS loader runs through the import
descriptor table, loads all of the DLLs that are mentioned, and fixes up
the addresses.
--
Tim Roberts, timr@xxxxxxxxx
Providenza & Boekelheide, Inc.
.



Relevant Pages

  • Re: how do static import libraries actually work?
    ... "I could create an import library called xxx.lib that contained an entry ... One thing I've never really understood are import libraries for DLLs ... Without linking to user32.lib in VS I get a link error, ... What happens when you statically link to a DLL import library (i.e. ...
    (microsoft.public.vc.language)
  • Re: C coding guidelines
    ... I wouldn't call those entry points either: ... _DLLMainCRTStartup is provided for you, but it is there just like ... link with to produce the DLL. ... libraries. ...
    (comp.lang.c)
  • Re: How to get imagebase after a DLL gets loaded
    ... The main difference between DLL and static library in this context is ... so that it is not meant to be integrated with the client code ... SPEAKING ABOUT STATIC LIBRARIES HERE, AND I *EXPLICITLY* SAID IT THIS ...
    (microsoft.public.win32.programmer.kernel)
  • Re: How to get a .lib from .dll
    ... Using the Borland tools, you should first run "IMPDEF" on the DLL file ... information in it to create ".LIB" import libraries with;)... ... from Windows version to Windows version...so, ...
    (comp.lang.asm.x86)
  • Re: How to get a .lib from .dll
    ... Using the Borland tools, you should first run "IMPDEF" on the DLL file ... information in it to create ".LIB" import libraries with;)... ... from Windows version to Windows version...so, ...
    (alt.lang.asm)