Basics of DLL
Basics of DLL
Basics of DLL
INTRODUCTION
Firstly, I would like to tell you about why it is important to understand the concept of DLL. Dlls
are the programmer’s most useful component. Dll provides us these facilities: -
1. Faster application development.
2. Memory conservation.
3. Data (resource) sharing.
4. Better way of updating the software.
Few more that is programmer dependent like adding security in dlls, personal information, etc
Before we directly go for the core here are few general points which must have arrived in your
mind. The executables contain not only CPU instructions but data (sound, images etc). Suppose
you are creating a program which alarms by printing an image. You will put sound and image
code in your program. What would you do if you are required to develop 10 similar programs?
Bad habit! – Don’t just copy the code. Imagine, you made a program that executes the sound
playing code of your first program. Hanged! It is possible. Dll files allow you to do this. This
feature of DLL is known as linking. And b/c it’s done at run time (dynamically) it’s now called to
be dynamic linking. Further we added many more functions to our first program creating a small
library. Whenever we execute the later programs (client programs using the parent program)
they call the sound playing code of 1st program. Here our 1st program is acting as a library,
parent or source. Our newer programs will fail to work if there is no library program. That’s why
the library becomes very important to us and must be executed before we can run the later
programs.
The dll files are similar to what we were taking about our first program or library. But there are
some differences: -
1. Dll files can’t execute by themselves. Must be loaded by another executable.
2. They have extension “.dll” not “.exe”
If these instructions are code of DLL files then you are the program using this dll. Every program
shares this same code.
Everyone having their material follow the same instructions to give the result.
What happens if two programs are running and using a single DLL?
=>
Both are using the code of this dll. But if any of them finishes its task 1st. It’ll be unloaded. But dll
doesn’t unload. If it were unloaded what would happen with the 2nd program. To check this
troublesome state, windows uses a count strategy.
Windows uses a count value to check how many programs are using the dll file. Whenever a new
program (or another instance of 1st) executes windows increments the count of the dll by 1.
Similar happens when program unload i.e. decrements the count. When the count becomes 0,
finally unloads the dll from the memory.
If you have not yet developed any program using a dll, this is the right time to start. But there
are few things to mind before we go for dll programming. These are –
1. What compiler do I use?
2. Language preferred here is C or VC.
3. Must have developed some windows applications.
PLEASE NOTE:-
We are going to use a dll file that’s why we would not bother in creating static link library which
is mere a lib file like we used in TC++. Compiling Dynamic link library project creates exp,
lib(doesn’t contains the code) and dll files. Whereas on compiling Static link library project
produces a lib file which contains the code.
Fortran (__stdcall) Pushes parameters on the stack, in Called function clears the
reverse order (right to left) stack
If you have created a function in dll with __stdcall the function will contain some garbage like
letters in its name. If function is declared as: -
void DLLEXPORT __stdcall dllfunction()
_dllfunction@0 name is created in the dll.
If we were just named our file with cpp extension then the dll’s sign would have been something
like this
?dllfunction@@YAX
NOTE: Because c++ remembers functions by its parameters not only names. That’s why it
supports function overloading.
HOW TO SEE THESE NAMES: Use Dependency Walker See reference
To use a dll, we must declare the function prototype before using them. A header file is to be
created first.
Load-time dynamic linking: This is the easiest way of using the dll. The program will be linked
(save the address of the exported functions) with the dll at compile time. It requires the lib file
created with the dll. The lib file contains the information how the functions will be linked with the
target program.
Run-time dynamic linking: This is harder b/c it requires many function pointers. But any
change in the dll will not affect the user program. It doesn’t require a lib file.
int __stdcall WinMain(HINSTANCE hinst, HINSTANCE hprevinst, LPSTR lpcmdline, int cmdshow)
{
dllfunction();
return 0;
}
/********************** EOF ************************/
Click project -> settings -> Link -> On the list of lib files add your dll’s lib file i.e. dllprj.lib -> OK
Run the program. It will give error that dllprj.dll not found.
Paste the dll file in the exe’s folder and run the exe.
Run-time: -
int __stdcall WinMain(HINSTANCE hinst, HINSTANCE hprevinst, LPSTR lpcmdline, int cmdshow)
{
HINSTANCE hdll; /* creates a handle for dll */
void (*pfunc)(); /*creates a function pointer */
/* OR
BOOL (CALLBACK *pfunc)();
Using this pointer there is no need to use typecasting in GetProcAddress.
*/
/*
Use if callback function is used
pfunc = GetProcAddress(hdll,"dllfunction");
*/
REFERNCES: -
For all macros explore windef.h
Dependency Walker – See dll’s functions. ( Get it from support tools
provided with Visual Studio and XP cd-rom.