June 04, 2003
.NET and DLL HellRichard Grimes
The DLL model was designed with good intentions, but it still paved the way to perdition
DLL Hell was a phrase coined many years ago to describe a problem that is inherent in Windows method of sharing code: the Dynamic Link Library. In this and the next newsletter, I will outline what causes DLL Hell and how .NET solves these specific issues. I will start by describing the underlying causes of DLL Hell and explain .NETs solution in the next newsletter. The "D" in DLL refers to the the fact that the linking of libraries to a process occurs dynamically at run time rather than statically by the compiler. It's true that a developer can statically link to an import library, but the DLL is still linked at run time. The reason is because the import library gives the compiler enough information to build an Import Address Table, which contains information about the DLL and the function to be imported. The compiler directly calls to the DLL functions to entries in the IAT. At run time, the LoadLibraryEx Win32 function is called to load the DLL and the IAT entries are overwritten with the actual address of the functions in the DLL. The developer can also call LoadLibraryEx to explicitly load the DLL and then call GetProcAddress to get the address of the function; this involves messing around with function pointers, but it does give the developer a better opportunity to handle the situation when a DLL is missing or does not have the requested function. Notice that both of these mechanisms use LoadLibraryEx. DLL Hell occurs mainly because LoadLibraryEx is passed the name of the DLL to load. This can be an absolute path, indicating a DLL in a specific folder, but typically it is just the name of the DLL. If the path is omitted, then LoadLibraryEx will use an algorithm to locate the DLL (take a look in the entry for this function in the MSDN library for complete details), which includes the Windows system folder (for shared DLLs) and the folders in the PATH environment variable. The problem is that LoadLibraryEx loads a DLL by name and although PE (portable executable) files can have a version resource, this is not part of the name. Two versions of the same DLL must have different names, otherwise LoadLibraryEx could load the wrong version of a DLL, or it could even load a totally different DLL that has the same name. COM helps because the actual location of a library is stored in the registry and the client code never uses DLL names and paths, but instead refers to class names (CLSIDs), and the COM system resolves the class name to a DLL path through a registry lookup. The strict rules of COM are such that if a class changes, then its CLSID should also change, and so the DLL housing the class should change as well. However, people broke these rules, so DLL Hell persisted into COM code (if developers had followed the COM rules, then DLL Hell would have gone away). COM's reliance on the registry was also a problem because it meant that the library would have to be installed before use, and uninstalled when the application was removed. So, the problems of using Win32 DLLs come down to these issues:
There were ways to get round these problems, but basically they were just small fixes to a flawed process. To get round all of these problems, the .NET team came up with an entirely different mechanism called Fusion, which I will describe in the next newsletter. As a postscript, the DotNet Developer Group in the UK (www.richplum.net) has recently had its inaugural meeting at Microsoft's Thames Valley Park campus. I spoke at that meeting, and I'll speak at some of their other meetings later this year. If you're in the UK, come along to one of these user meetings and listen to some great talks about .NET Richard Grimes speaks at conferences and writes extensively on .NET, COM, and COM+. He is the author of Developing Applications with Visual Studio .NET (Addison-Wesley, 2002). If you have comments about this topic, Richard can be reached at dotnet.dev@grimes.demon.co.uk.
|
|
||||||||||||||||||||||||||||
|
|
|
|