Re: how do static import libraries actually work? by John
John
Fri Jun 01 05:42:00 CDT 2007
Hi Tim Roberts,
Thank you.
"I could create an import library called xxx.lib that contained an entry
for
MyEntryPoint that points to YourEntryPoint in zzz.dll. " - How?
--
Thanks & Regards,
John.
"Tim Roberts" wrote:
> kilik3000@gmail.com 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@probo.com
> Providenza & Boekelheide, Inc.
>