Thursday, January 15, 2009

What is the difference between dll and exe?

There are multiple differences between an .exe and .dll file. I can update few among those to my best.
Though both seem to be in files format but in reality the usability differs too far.
Exe: - Extensible Execute File
Dll: - Dynamic linkage library.

.Exe is readily executable

Any exe file (calc.exe, cmd.exe, winword.exe, notepad.exe etc…) can be executed directly by calling the exe file name or double clicking clicking on the exe file.
It is not required some other way of instantiating. So an exe can run by itself but we need to just call based upon our need.
Example: - If we would like to open calculator in windows operating system.
i) Go to command prompt, type ‘Calc’ and press enter.


ii) Go to “C:\WINDOWS\system32” and click on ‘calc.exe’ file available.

.Dll is not readily executable

Any dll file cannot be runned immediately or by itself. I mean we cannot do any thing with a dll file instantly. It can be used in application to prepare an exe file, so when we run this exe file it loads the referring dll file dynamically and links to it. So dll file is loaded and linked dynamically.

Dll file holds the compiled logic of methods or functionalities.
Say suppose to restart machine there could be a process or logic, this logic or process is written as method or function and then compiled.
This compiled logic can be a dll file. This can the be dynamically loaded or linked in an application or exe when and there required to restart machine.

So dll is a file that is composed of read only sections which has code and resources that can be reused among applications or other compile source codes (dll’s).

If we go to “C:\WINDOWS\system32”, we can find many dll files available which does do nothing by clicking on it, but they are loaded and linked dynamically by exe’s.

So Exe can run on its own, where as Dll is loaded, referenced or linked by Exe dynamically.

.Exe has single entry point

Any file that is exe has only one single entry point. Main method is the first and the only unique method to identify the entry point of an exe file.
So when we run an exe file, the main method composed in exe file is called first and starts from there.
There cannot be more than one entry in .exe

.Dll has multiple entries

Dll is very opposite to what Exe is. Dll file is composed of the logics or compiled version of many methods or functions that can be used in exe by being referred.

So we can refer multiple different functions available in dll from exe file, multiple times. So it is said to have multiple entries.

3) Reusability

Dll files support reusability more in actual.
Exe cannot be re used but to create such exe again, we need to compile again by copying the existing code of an exe.

Since dll support reusability, functions which are generic and could be used in multiples applications (exe’s) or with in multiple functions or logic’s (dll’s) can be placed in a single dll file. So when we need to run such generic function in any application or with in a function, we can just load that dll and refer to the method available in its respective dll file.
Please observe diagram below, how methods or function in abc.dll and def.dll can be reused among dll’s and exe’s instead of re writing the functions again when required.

So the advantage of reusability is, when there occurs a situation to modify logic in generic function it could be done easily to change at only one place as it is being referred in every exe and dll files.
In above example, we can observe Method2() from abc.dll is being referred by 456.exe.
So when i need to change some functionality in Method2(), I can just replace the existing abc.dll with compiled version of newly implemented logic or change in Method2.

So when I do such change in abc.dll, it automatically reflects when we run 456.exe because the abc.dll is linked and loaded dynamically by 456.exe file.

Also, method2() from abc.dll is referred in multiple exe’s or dll’s, it is not required to compile every dll where it is referred and prepare new exe where this functionality is used. Since acd.dll is loaded and linked dynamically to respective exe or dll.


Exe is out process

When we run an exe file or application, memory should be allocated externally to run.
Since it has to be done externally while running an exe, it is said to be “Out Process”.

Exe is in process
When a dll file is been refered with in an exe, so the dll file will be loaded into memory which is with in the memory consumed by the exe file.
So this is called as “In Process”.

From the above diagram, when we run 123.exe it loads abc.dll to call method2() function.

Advantages of dll
1) Reusability, Supports reusability of functionalities by implementing the generic function or method in a single dll file.
2) Dynamic, Can be linked and loaded dynamically, so changes made in dll file reflect at all instants where it is referred by either exe or dll.
3) Versioned, Dll can be versioned, such that several releases of same dll can be differentiated based upon version number.
4) Multiple entries, can directly call the required method available in the dll file.
5) In process, since dll is in process component, so any method calls or properties need not to be marshaled and which results to faster performance.
6) Low memory usage. Dll is loaded only when requested by a dll or exe and so memory is assigned or occupied by dll only when loaded or required.
7) Reduced swapping, if same dll is requested by 2 exe’s at same time or while one exe is running, then instead of loading the same dll it is loaded only once. A counter is incremented upon number of requests ande decremented when request is completed. So when the counter is zero, the dll is unloaded from memory.
8) Interoperable between languages, dll built or compiled in a specific language can be re used in an exe which might be built in other language.Unfortunately, there are few languages that support dll’s.
9) Efficient market support, If a release of application has been made and later there could be required to in corporate few changes.
In such case instead of re delivering the entire product, we can just re build the required dll and replace it in the delivered application folder where it is required to be.

Disadvantages of dll

1) Any unhandled exceptions that could occur in dll leads to stop client process as dll is referred by application or exe but not executed directly.
2) Dll Hell:- When we try to install a new application which refer to few dll’s, these dll files could also be installed along with exe. So, there could be situation where these dll’s could replace the existing dll files. If the version is different for both dll’s then the existing applications which refer to the older dll may face some problems while dynamically loading that dll file. So applications should be built in such a way to ensure back ward compatability.

Advantages of Exe

1) Readily executable, Exe files are very ready executable and can run on its own.
Disadvantages of Exe

1) Not reusable, Exe files are not reusable as dll files.
2) Should be very care full while running an unknown exe which could be a virus to collapse a machine.
3) Exe’s cannot be versioned as dll files.

No comments:

Post a Comment