MFC DLL Tutorial

CoderSource.net
MFC DLL Tutorial
Rating:

Date: 5/1/2004 12:00:00 AM

   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:

  1. Create an MFC AppWizard Dll. Choose the option of Regular MFC DLL - Dynamically Linked to MFC.
  2. 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.
  3. Enter a function declaration inside the header file utilities.h with the following syntax.
  4. __declspec(dllexport) int AddValues(int a, int b);

  5. Add the function definition inside the utilities.cpp file as follows.

  6. int AddValues(int a, int b)
    {
           return a+b;
    }


  7. 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:

  1. Create a new MFC AppWizard Executable application. Even a console application will be enough for using the MFC Dll.
  2. Copy the utilities.h into the project folder. Copy the ProjectName.lib and ProjectName.dll into debug folder of the project.
  3. In the source file of the application include the header file as #include "utilities.h".
  4. In Project Settings --> Link --> Library/Modules add Debug/ProjectName.lib. This will ensure linking of the necessary MFC Dll files.
  5. Call the AddValues function any where necesary.
  6. Build and run the application. The MFC Dll function will be used.

You Can Rate this Article, if you are Logged In      
 

More Links from CoderSource.net:

 
Refer to a Friend:

Your Details:

Name:     e-mail:

Friend Details:

Name:    e-mail:    


MENU
Home
MFC 
C++
.Net
WIN32
Programming
Forum
My Articles
Add to Google
Add to My Yahoo!
Welcome to Codersource.Net Login | Register | Faq  

SEARCH
Google
 

NOTES:


Thanks for visiting our CoderSource.net. This site will be improved with more articles. Interested visitors can also submit their articles through the Submit Article link.Your article will also be published after due consideration by the editor. 

© Copyright 2003. All rights on content reserved by CoderSource.net. Contact    About Us