Introduction
If you like to create modular software, you choose DLLs. Dynamic linked libraries are easy to create compared to applications. Dynamic linked library is a set of modules that contains a set of functions or other called DLLs.
In windows operating system some of the DLLs are important. They are the following - User32.dll manages user interface related tasks. The Gdi32.dll manages graphic device interface related (GDI) tasks. In DOS world, programs write directly into video memory. But, in windows layer called Graphic Device Interface (GDI) exists. The advapi32.dll manages registry related tasks. The comdlg32.dll manages the common dialog box related tasks (Example File open dialog box, file save and save as dialog box). The comctrl32.dll manages the windows common control related tasks.
The Windows DLLs are the backbone of windows operating system. The windows operating system manages the large set of system DLLs. When the windows operating system starts, the DLLs load to system memory.
Types of DLL
The windows operating system has two types of DLLs. There are the following.
- Load Time Dynamic Linking
- Run Time Dynamic Linking
The Load Time Dynamic Linking load the DLLs at the load time. When the application starts, all the required DLLs are loaded into memory in the startup time. In this method the system tries to find the DLLs in the following locations.
- The Windows System Directory
- The Windows directory
- The Application local path
- The directories listed in the path environment variables
Load Time DLLs requires LIB file and declaration file (.h), when we create application programs. We insert the header file in the application and link with the LIB file in the application project settings. The DLLs do not handle windows messages. The DLLs are simply set of functions or variables declared in the declaration file ( .h) and implemented in the implementation file(.cpp).
The run time dynamic linking tries to load DLL at run time. The LoadLibrary function is used to load a DLL at run time. The syntax for LoadLibrary is the following.
HINSTANCE hinst = LoadLibrary(LPCTSTR dllfilename);
If the LoadLibrary returns success, then we use GetProcAddress get the address of the functions. After calling DLL function we call FreeLibrary to free the DLL library. In this run time dynamic linking, we do not load all the DLL files in the startup time. Whenever we want to load, we load and unload the DLLs.
Create Load time Win32 DLL
We already saw, that using DLLs is easy to create modular applications. In win32 Dynamic linked library project choose DLL and add support for some export symbols in second step. In that class declaration add your method and return types. When you allocate memory in the DLL file, you also deallocate it. If you deallocated from the client (like from application) it may be deallocated but we cannot be sure.
__declspec(dllimport) tells the compiler to add some symbols for import from the DLLs. This way the __declspec(dllexport) tells the compiler to export some symbols. In our sample program declare the three functions. The functions are to add, multiply and subtract the two values. These simple DLLs help you understand the basic DLLs functionality.
The DLL entry point is the DllMain; the DllMain has the following syntax.
BOOL APIENTRY DllMain(HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved)
{
switch( ul_reason_for_call ) {
case DLL_PROCESS_ATTACH:
.
.
case DLL_THREAD_ATTACH:
.
.
case DLL_THREAD_DETACH:
.
.
case DLL_PROCESS_DETACH:
.
.
}
return TRUE;
}
You copy DLL file into system directory and copy LIB file into Visual C++ Lib directory and link to lib file. In application module make sure to implement the header file. In our example file is declared in the following manner.
#ifdef WIN32DLL_EXPORTS
#define WIN32DLL_API __declspec(dllexport)
#else
#define WIN32DLL_API __declspec(dllimport)
#endif
class WIN32DLL_API CWin32DLL {
public:
CWin32DLL(void);
int add(int a,int b);
int sub(int a,int b);
int mul(int a,int b);
};
extern WIN32DLL_API int nWin32DLL;
The class CWin32DLL has been declared as __declspec(dllexport) using WIN32DLL_API. So, all the class member functions and member variables are __declspec(dllexport). All the member functions are declared as public.
In our implementation file, we link and call the functions.
The run time dynamic linking in the client program are the following. #include "stdio.h"
#include "windows.h"
typedef VOID (*MYPROC)(LPTSTR);
VOID main(VOID)
{
HINSTANCE hinstLib;
MYPROC ProcAdd;
BOOL fFreeResult, fRunTimeLinkSuccess = FALSE;
hinstLib = LoadLibrary("win32dll.dll");
if (hinstLib != NULL)
{
ProcAdd = (MYPROC) GetProcAddress(hinstLib, "add");
if (fRunTimeLinkSuccess = (ProcAdd != NULL))
(ProcAdd) ("message via DLL function\n");
fFreeResult = FreeLibrary(hinstLib);
}
if (! fRunTimeLinkSuccess)
printf("message via alternative method\n");
}
Create MFC DLL
Microsoft Foundation Class (MFC) library can be used to create simplified DLLs. The MFC supports two types of DLLs. Those are the following.
- Regular DLL with MFC statically linked
- Regular DLL using shared MFC Dll
- MFC Extension DLL ( Using shared MFC DLL)
In the regular DLL with MFC statically linked, the Client may be MFC based application or Non-MFC application. Our sample program application is based on a DLL using shared MFC dll.
MFC Extension DLL has reusable classes derived from existing Microsoft foundation Classes. Extension DLL is built from Dynamic linked library version of the DLL. In MFC, regular DLLs are initialized in three ways. The DLL has a CWinApp Object. MFC OLE initializes using MFC global AfxOleInitModule function. The MFC Database initializes into MFC using global AfxDbInitModule function. MFC Sockets are initialized using AfxNetInitModule function.
Initializing the Extension DLLs is as following.
extern "C" int APIENTRY DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID
lpReserved)
{
if (dwReason == DLL_PROCESS_ATTACH)
{
AfxInitExtensionModule(PROJNAMEDLL, hInstance);
new CDynLinkLibrary(dll);
}
else if (dwReason == DLL_PROCESS_DETACH)
{
}
return 1;
}
In our example, The Dll has DrawEllipse function. We pass the CRect structure and CDC to DLL. The DLL function draws the DLL with selected Blue Brush.
Benefits of DLLs
The benefits of using DLLs are the following.
- The Dynamic linked library shares the memory. So, the system performance is improving compared to using applications.
- We can build and test separately each DLL.
- We can create DLLs for different languages. Windows use C DLL, C++ DLL, Fortran DLL and PASCAL DLL etc. Even windows support some version of Smalltalk DLLs.
- We can load and unload at run time. This helps to improve application performance.
- The big software products were divided into several DLLs. The developers easily develop their application.
| You must Sign In to use this message board. |
|
|
 |
|
 |
I modified the class declaration as WIN32DLL_API class CWin32DLL {...}, WIN32DLL_API before the class CWin32DLL, compilation error occured. Who could tell me the reason, Thanks!
|
| Sign In·View Thread·PermaLink | 1.00/5 |
|
|
|
 |
|
 |
It isn't simple question. We need write code on c# or VB or VBScript to create load time dll. How I can do it on .net
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
 |
|
 |
Well i have to port an application from VC++ to MC++.
VC++ application scenario: - The VC++ application was linked to two static libs(a.lib & b.lib) with a definition file (x.h).
Now for the Managed C++ application: - 1. I added these above "a.lib" & "b.lib" in project's Properties>Linker>Command Line>Additional Options
2. Included the x.h file in the project Functions are defined in the x.h are as follows bool InitNetwork(int network_no, void callback(int,int))
3. Included a delegate function on the MC++ application for the callback object to send as parameter
4. Called the function with the paramters.
Result the method was never called
Now, i heard that i'm supposed to put put __declspec(dllimport in front he imported function so i modified the function as below- __declspec(dllimport) InitNetwork(int network_no, void callback(int,int))
Is this the correct way...
Help required to implement this, as more functionalities are to implemented from x.lib. Note: - I don't have the x.lib source code & I'm not good at VC++ code.
Thanks in advance to anyone who could guide me in the right way.
Arun Antony Microsoft .NET Developer
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
First of all, thanks a lot for this introduction article. It gives a good idea about creating DLLs from scratch.
One thing I found wrong is the example for run time dynamic linking. It is obviously has been adopted from MSDN site: "using run time dynamic linking", but code has not been adapted to the given example.
For instance, declaration "VOID (*MYPROC)(LPTSTR);" I guess should be changed to something more appropriate, as well as call to the DLL function -- (ProcAdd) ("Message via DLL function\n"); (introduced in Win32Dll.cpp, "add" takes two integers, not a string!).
Fixing these will improve the whole thing quite a lot.
Rather that that article is pretty useful. Again, thanks for it.
Elena
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Hi -
I have created a DLL as you describe (but using Visual Studio's DLL project wizard - it sets up a DLL to look just like you describe). In my application, where I want to use the DLL, I have used a typedef and LoadLibrary and GetProcAddress as you describe, to get access to a function in the DLL. I have also linked with my DLL's .lib file. However, when I run my application, it can't find the function name in the DLL. Is there a common mistake that I might be doing?
Eric
|
| Sign In·View Thread·PermaLink | 1.00/5 |
|
|
|
 |
|
|
 |
|
 |
Hi,
possible an export problem
A little example. Your source code.
#define DLL_EXPORT1 __declspec(dllexport) #define DLL_EXPORT2 extern "C" __declspec(dllexport)
DLL_EXPORT1 int GetAnInt1(); DLL_EXPORT2 int GetAnInt2();
Show the dependencies e.g. with the VC Dependecy Walker and You can see the different naming conventions from the functions.
?GetAnInt1@@YAHXZ GetAnInt2
This is the reason why you can't find the function address of 'GetAnInt1' because the correct name is '?GetAnInt1@@YAHXZ'.
HTH Frank
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
 |
|
|
 |
|
|
 |
|
 |
Hello, I am curently experimenting with DLL's and i am wondering about how functions are exported.
I have made a MFC dll project using the following settings: Regular DLL using static linked MFC.
The only Class/function created is a class that inherints from the CWinApp class. Can i export member functions from this class? and if yes, how do i do that?
In advance thanks Michael
|
| Sign In·View Thread·PermaLink | 1.00/5 |
|
|
|
 |
|
 |
in your .def file under 'Explictit exports' just put the name of your function there.
If it's broken, I probably did it
bdiamond
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
If you use win32 dynamic linked library that is less memory space compare then MFC dynamic linked library (stated MSDN).
If you choose MFC DLL, two types of DLL available. 1.Regular DLL 2.Extension DLL.
The Extension DLL exports the C++ interface. It loads quickly. Extension DLL is small size. Extension DLL requires your client program dynamically version of MFC library and DLL synchronized version of MFC DLL’s.
If you need Win32 environment (like Visual Basic as client) you should use regular DLL. The regular DLL has two types. The Dynamically linked and Statically linked.
The restriction in regular DLL exports C style function only.
If you choose statically linked your DLL include copy all the needed MFC library code with in your DLL. So, it size is more. If you choose dynamic linked library the memory space is less compare then static linked library regular DLL. But, you will use proper MFC DLL in the target machine. It does not copy DLL the code in the MFC.
|
| Sign In·View Thread·PermaLink | 1.67/5 |
|
|
|
 |
|
 |
Hi, A very nice article for the beginners. Can u please explain what is the relation between libs and dlls and its headers,you briefly mentioned it in your article but things remain a bit unclear, or may be you can refer to a book or site which explains the above.Also do the run time dlls also need lib and headers? Thanx in advance.
|
| Sign In·View Thread·PermaLink | 1.00/5 |
|
|
|
 |
|
 |
Hi,
When we compiled the source code, each source create separate object (*.obj) file. The linker links all object files and create the library (*.lib) and DLL module. The lib file is small size. Because, it does not contains variables and functions. It exports some symbols.
The header file contains the declaration of variables and functions. The declaration tells the compiler the required memory space. In the declaration time the memory does not allocated. When we create the client program, it requires the header file in the DLL module.
In our client program does not know about the DLL module. In header file we declareas functions __declspec(dllimport). In our client program does not use any __declspec calling convention. It simply includes the DLL’s header file. When the compiler sees the __declspec(dllimport), it knows the implementation are from the DLL module.
R.Selvam
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
This is good stuff for beginners and as every developer which went through this times of using/making the first own Dlls, I wish that it would clearly point to the pros and cons of using Loadtime/Runtime-Dlls and MFC/MFC-Enabled/NonMFC-Dlls. For me important were also the following Tech notes: TN011: Using MFC as Part of a DLL TN033: DLL Version of MFC
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
 |
i'm missing some things, for example explaining how a dll works, things like showing how to use the (extern "C" ...)__declspec( dllexport ) statement and using .def files, the difference between C and C++ name convention(it's important if u use the dll from other languages!) and so on. 4 points.
Don't try it, just do it! 
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Yes. I missed some concepts in this article. A single article does not cover all the concepts. So, I can try to post some articles related to DLL’s. I did not explain more in Run time DLL, (you mentioned! ) Delay loading, LoadLibrary, LoadLibraryEx, TLS, Calling Convention, etc
|
| Sign In·View Thread·PermaLink | 5.00/5 |
|
|
|
 |
|
|
 |
|
|