Introduction
What is a DLL? DLL stands for Dynamic Link Library. Using DLL's offers several advantages as mentioned below:- They simplify project management. If different groups work on different modules during the development process, the project is easier to manage.
- They help conserve memory. If two or more applications use the same DLL, the DLL has its pages in RAM once and the pages are shared by all of the applications. The C/C++ run-time library is a perfect example. Many applications use this library.
- They extend the features of an application. Since DLLs can be dynamically loaded into a process's address space, an application can determine at run time what actions to perform and then load the code to execute those actions on demand.
- They can be written in many programming languages. You can choose the best language for the job at hand. Perhaps your application's user interface is best programmed with Microsoft Visual Basic, but the business logic is better handled by C++.
Writing the application using a DLL
There are two parts to writing this application.- Writing the DLL whose functions we are going to call
- Writing a test client for the DLL
Creating the DLL
- Fire up Visual C++ & choose the Project as MFC AppWizard(Dll) and type in Project name as
MyDll
- Let the default selection for DLL type remain, i.e "Regular DLL using Shared MFC DLL"
- Click Finish and then Ok to get Visual Studio to generate the necessary files.
- Go to the class view, right click on
“MyDll classes”
and choose“New Class”.
- Now choose Class type as
"Generic Class"
. - Type in the Class name
CMyClass
, the wizard automatically fills in the rest of the names. - Go to the class view again
- Right click on the
CMyClass
and choose“Add Member Function”
- Type in the name of the function type as
CString
and fill in the function declaration asSayHello (CString strName)
. Choose Access type of the function aspublic
. Note :CString
is a MFC class that makes manipulation of strings very easy. - Now go to file view and type in the following code into
CMyClass.cpp
as shown below in Code Snippet 2 - To be able to call the functions from an external application we have to prefix all function signatures with
__declspec(dllexport)
. This change is made in the CMyClass.h file as shown in Code Snippet 1
Compile the application and your DLL is ready



























Creating the DLL ClientNow we write a 'Client' to test our DLL This is a MFC dialog based application with a edit box and two buttons.
- Select
New MFC AppWizard(exe)
from the project menu and type in the project nameTestDLL
- Choose
Dialog based application
and click Finish - You would now be looking at the application in the resource view. Add an edit box to the application by selecting the
edit box
, next click on the dialog box and drag. - Also create a
CString
(value) variable associated with it, call this variable m_edit i.e Click on the edit box and press CTRL and W to bring up the class wizard. Choose the member variables tab and choose IDC_EDIT1 and click on “Add Variable”, of type "Value" and typem_edit
- Now a file called
TestDLLDlg.cpp
would have been generated . - Double click the “Ok” button on the dialog, the wizard pops up a box asking a name for the function, choose the default name “OnOk” to go to the
TestDLLDlg.cpp
file - Modify
TestDLLdlg.cpp
to look like the Code Snippet 1, the code entered has no effect and the project at this point will not compile. (Basically we are calling a method of a class object, this object has not been defined as yet.) - Goto the file
TestDLLDlg.h
and include the header file for your class i.eMyClass.h
. - In the file
TestDLLDlg.h
declare an object of your classobjMyClass
present in the DLL - After steps 8 & 9 the code looks as in Code Snippet 2
- We need to modify the project settings to compile the project, Click on "Project->Settings->Link" and in the "Object/Library Modules" enter the complete or relative path to the DLL's library file. i.e. A .lib file is generated in the same folder as your DLL, I have entered "../MyDll/Debug/MyDll.lib" here.
- Compile the application and if it has compiled successfully. Run it.
Hey, Why are we getting this stupid box saying "Unable to located DLL" ?
Solution: Copy the MyDll.dll to the same folder as the TestDll.exe
Run the application, enter a name in the text box and click the okay button, this will show a message box containing the same text prefixed with a hello.
// TestDLLdlg.cpp
// ----------------- Code Snippet 1 ----------------------------
void CTestDLLDlg::OnOK()
... {
![]()
UpdateData(true);
CString strResult = objMyClass.SayHello(m_edit);
AfxMessageBox (strResult);
//CDialog::OnOK();
![]()
}
// ----------------- Code Snippet 1 ----------------------------
// ----------------- Code Snippet 2 ----------------------------
// TestDLLDlg.h
/**/ /////
// CTestDLLDlg dialog
![]()
#include " ..MyDllMyClass.h "
class CTestDLLDlg : public CDialog
... {
// Construction
public:
CTestDLLDlg(CWnd* pParent = NULL); // standard constructor
CMyClass objMyClass;
// Dialog Data
// ----------------- Code Snippet 2 ----------------------------
文章出处:http://www.codeproject.com/dll/beginnerdll.asp