MFC regular Dll usage has decreased now a days
to a large extent. People are now moving towards COM dlls.
Though the COM Dlls' have a lot of advantages over regular
Dlls' in MFC, the regular dlls are still being used by many
developers.
MFC DLL wizard provides options to create 3 kinds of DLLs.
Regular DLL Statically linked to MFC, Regular DLL dynamically
linked to MFC and MFC extension DLLs. This article uses MFC
regular DLL Dynamically linked to MFC. The output produced by
this option (dynamically linked) is a very small DLL. The
statically linked DLLs will be of larger size, as they are
built with the whole MFC libraries within themselves.
A MFC Dll can provide/export with functions, variable values,
constants and classes. This article concentrates on exporting
a function, which is the mostly used export in DLL.
MFC DLL
- Building the DLL:
-
Create an MFC AppWizard Dll. Choose the option of Regular
MFC DLL - Dynamically Linked to MFC.
- After
the application gets created, add a header file and a source
file to the application. The article assumes the file names
to be utilities.h, utilities.cpp.
- Enter
a function declaration inside the header file utilities.h with the
following syntax.
__declspec(dllexport)
int AddValues(int a, int b);
- Add
the function definition inside the utilities.cpp file as follows.
int AddValues(int a, int b)
{
return a+b;
}
-
Compile the MFC DLL application. This will produce a
ProjectName.lib and ProjectName.dll in the debug folder.
These two files are enough for using inside an application.
MFC DLL
- Building the application:
-
Create a new MFC AppWizard Executable application. Even a
console application will be enough for using the MFC Dll.
- Copy
the utilities.h into the project folder. Copy the
ProjectName.lib and ProjectName.dll into debug folder of the
project.
- In
the source file of the application include the header file
as #include "utilities.h".
- In
Project Settings --> Link --> Library/Modules add Debug/ProjectName.lib.
This will ensure linking of the necessary MFC Dll files.
- Call
the AddValues function any where necesary.
- Build
and run the application. The MFC Dll function will be used.