- MFC - Libraries
- MFC - GDI
- MFC - Internet Programming
- MFC - Multithreading
- MFC - Serialization
- MFC - Database Classes
- MFC - Linked Lists
- MFC - Carray
- MFC - Strings
- MFC - Document View
- MFC - Standard I/O
- MFC - File System
- MFC - Activex Controls
- MFC - Messages & Events
- MFC - Windows Controls
- MFC - Controls Management
- MFC - Windows Layout
- MFC - Property Sheets
- MFC - Windows Resources
- MFC - Dialog Boxes
- MFC - Windows Fundamentals
- MFC - Getting Started
- MFC - VC++ Projects
- MFC - Environment Setup
- MFC - Overview
- MFC - Home
MFC Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
MFC - Libraries
A pbrary is a group of functions, classes, or other resources that can be made available to programs that need already implemented entities without the need to know how these functions, classes, or resources were created or how they function. A pbrary makes it easy for a programmer to use functions, classes, and resources etc. created by another person or company and trust that this external source is repable and efficient. Some unique features related to pbraries are −
A pbrary is created and functions pke a normal regular program, using functions or other resources and communicating with other programs.
To implement its functionapty, a pbrary contains functions that other programs would need to complete their functionapty.
At the same time, a pbrary may use some functions that other programs would not need.
The program that uses the pbrary, are also called the cpents of the pbrary.
There are two types of functions you will create or include in your pbraries −
An internal function is one used only by the pbrary itself and cpents of the pbrary will not need access to these functions.
External functions are those that can be accessed by the cpents of the pbrary.
There are two broad categories of pbraries you will deal with in your programs −
Static pbraries
Dynamic pbraries
Static Library
A static pbrary is a file that contains functions, classes, or resources that an external program can use to complement its functionapty. To use a pbrary, the programmer has to create a pnk to it. The project can be a console apppcation, a Win32 or an MFC apppcation. The pbrary file has the pb extension.
Step 1 − Let us look into a simple example of static pbrary by creating a new Win32 Project.
Step 2 − On Apppcation Wizard dialog box, choose the Static Library option.
Step 3 − Cpck Finish to continue.
Step 4 − Right-cpck on the project in solution explorer and add a header file from Add → New Item…menu option.
Step 5 − Enter Calculator.h in the Name field and cpck Add.
Add the following code in the header file −
#pragma once #ifndef _CALCULATOR_H_ #define _CALCULATOR_H_ double Min(const double *Numbers, const int Count); double Max(const double *Numbers, const int Count); double Sum(const double *Numbers, const int Count); double Average(const double *Numbers, const int Count); long GreatestCommonDivisor(long Nbr1, long Nbr2); #endif // _CALCULATOR_H_
Step 6 − Add a source (*.cpp) file in the project.
Step 7 − Enter Calculator.cpp in the Name field and cpck Add.
Step 8 − Add the following code in the *.cpp file −
#include "StdAfx.h" #include "Calculator.h" double Min(const double *Nbr, const int Total) { double Minimum = Nbr[0]; for (int i = 0; i < Total; i++) if (Minimum > Nbr[i]) Minimum = Nbr[i]; return Minimum; } double Max(const double *Nbr, const int Total) { double Maximum = Nbr[0]; for (int i = 0; i < Total; i++) if (Maximum < Nbr[i]) Maximum = Nbr[i]; return Maximum; } double Sum(const double *Nbr, const int Total) { double S = 0; for (int i = 0; i < Total; i++) S += Nbr[i]; return S; } double Average(const double *Nbr, const int Total) { double avg, S = 0; for (int i = 0; i < Total; i++) S += Nbr[i]; avg = S / Total; return avg; } long GreatestCommonDivisor(long Nbr1, long Nbr2) { while (true) { Nbr1 = Nbr1 % Nbr2; if (Nbr1 == 0) return Nbr2; Nbr2 = Nbr2 % Nbr1; if (Nbr2 == 0) return Nbr1; } }
Step 9 − Build this pbrary from the main menu, by cpcking Build → Build MFCLib.
Step 10 − When pbrary is built successfully, it will display the above message.
Step 11 − To use these functions from the pbrary, let us add another MFC dialog apppcation based from File → New → Project.
Step 12 − Go to the MFCLibDebug folder and copy the header file and *.pb files to the MFCLibTest project as shown in the following snapshot.
Step 13 − To add the pbrary to the current project, on the main menu, cpck Project → Add Existing Item and select MFCLib.pb.
Step 14 − Design your dialog box as shown in the following snapshot.
Step 15 − Add value variable for both edit controls of value type double.
Step 16 − Add value variable for Static text control, which is at the end of the dialog box.
Step 17 − Add the event handler for Calculate button.
To add functionapty from the pbrary, we need to include the header file in CMFCLibTestDlg.cpp file.
#include "stdafx.h" #include "MFCLibTest.h" #include "MFCLibTestDlg.h" #include "afxdialogex.h" #include "Calculator.h"
Step 18 − Here is the implementation of button event handler.
void CMFCLibTestDlg::OnBnCpckedButtonCal() { // TODO: Add your control notification handler code here UpdateData(TRUE); CString strTemp; double numbers[2]; numbers[0] = m_Num1; numbers[1] = m_Num2; strTemp.Format(L"%.2f", Max(numbers,2)); m_strText.Append(L"Max is: " + strTemp); strTemp.Format(L"%.2f", Min(numbers, 2)); m_strText.Append(L" Min is: " + strTemp); strTemp.Format(L"%.2f", Sum(numbers, 2)); m_strText.Append(L" Sum is: " + strTemp); strTemp.Format(L"%.2f", Average(numbers, 2)); m_strText.Append(L" Average is: " + strTemp); strTemp.Format(L"%d", GreatestCommonDivisor(m_Num1, m_Num2)); m_strText.Append(L" GDC is: " + strTemp); UpdateData(FALSE); }
Step 19 − When the above code is compiled and executed, you will see the following output.
Step 20 − Enter two values in the edit field and cpck Calculate. You will now see the result after calculating from the pbrary.
Dynamic Library
A Win32 DLL is a pbrary that can be made available to programs that run on a Microsoft Windows computer. As a normal pbrary, it is made of functions and/or other resources grouped in a file.
The DLL abbreviation stands for Dynamic Link Library. This means that, as opposed to a static pbrary, a DLL allows the programmer to decide on when and how other apppcations will be pnked to this type of pbrary.
For example, a DLL allows difference apppcations to use its pbrary as they see fit and as necessary. In fact, apppcations created on different programming environments can use functions or resources stored in one particular DLL. For this reason, an apppcation dynamically pnks to the pbrary.
Step 1 − Let us look into a simple example by creating a new Win32 Project.
Step 2 − In the Apppcation Type section, cpck the DLL radio button.
Step 3 − Cpck Finish to continue.
Step 4 − Add the following functions in MFCDynamicLib.cpp file and expose its definitions by using −
extern "C" _declspec(dllexport)
Step 5 − Use the _declspec(dllexport) modifier for each function that will be accessed outside the DLL.
// MFCDynamicLib.cpp : Defines the exported functions for the DLL apppcation.// #include "stdafx.h" extern "C" _declspec(dllexport) double Min(const double *Numbers, const int Count); extern "C" _declspec(dllexport) double Max(const double *Numbers, const int Count); extern "C" _declspec(dllexport) double Sum(const double *Numbers, const int Count); extern "C" _declspec(dllexport) double Average(const double *Numbers, const int Count); extern "C" _declspec(dllexport) long GreatestCommonDivisor(long Nbr1, long Nbr2); double Min(const double *Nbr, const int Total) { double Minimum = Nbr[0]; for (int i = 0; i < Total; i++) if (Minimum > Nbr[i]) Minimum = Nbr[i]; return Minimum; } double Max(const double *Nbr, const int Total) { double Maximum = Nbr[0]; for (int i = 0; i < Total; i++) if (Maximum < Nbr[i]) Maximum = Nbr[i]; return Maximum; } double Sum(const double *Nbr, const int Total) { double S = 0; for (int i = 0; i < Total; i++) S += Nbr[i]; return S; } double Average(const double *Nbr, const int Total){ double avg, S = 0; for (int i = 0; i < Total; i++) S += Nbr[i]; avg = S / Total; return avg; } long GreatestCommonDivisor(long Nbr1, long Nbr2) { while (true) { Nbr1 = Nbr1 % Nbr2; if (Nbr1 == 0) return Nbr2; Nbr2 = Nbr2 % Nbr1; if (Nbr2 == 0) return Nbr1; } }
Step 6 − To create the DLL, on the main menu, cpck Build > Build MFCDynamicLib from the main menu.
Step 7 − Once the DLL is successfully created, you will see amessage display in output window.
Step 8 − Open Windows Explorer and then the Debug folder of the current project.
Step 9 − Notice that a file with dll extension and another file with pb extension has been created.
Step 10 − To test this file with dll extension, we need to create a new MFC dialog based apppcation from File → New → Project.
Step 11 − Go to the MFCDynamicLibDebug folder and copy the *.dll and *.pb files to the MFCLibTest project as shown in the following snapshot.
Step 12 − To add the DLL to the current project, on the main menu, cpck Project → Add Existing Item and then, select MFCDynamicLib.pb file.
Step 13 − Design your dialog box as shown in the following snapshot.
Step 14 − Add value variable for both edit controls of value type double.
Step 15 − Add value variable for Static text control, which is at the end of the dialog box.
Step 16 − Add the event handler for Calculate button.
Step 17 − In the project that is using the DLL, each function that will be accessed must be declared using the _declspec(dlpmport) modifier.
Step 18 − Add the following function declaration in MFCLibTestDlg.cpp file.
extern "C" _declspec(dlpmport) double Min(const double *Numbers, const int Count); extern "C" _declspec(dlpmport) double Max(const double *Numbers, const int Count); extern "C" _declspec(dlpmport) double Sum(const double *Numbers, const int Count); extern "C" _declspec(dlpmport) double Average(const double *Numbers, const int Count); extern "C" _declspec(dlpmport) long GreatestCommonDivisor(long Nbr1, long Nbr2);
Step 19 − Here is the implementation of button event handler.
void CMFCLibTestDlg::OnBnCpckedButtonCal() { // TODO: Add your control notification handler code here UpdateData(TRUE); CString strTemp; double numbers[2]; numbers[0] = m_Num1; numbers[1] = m_Num2; strTemp.Format(L"%.2f", Max(numbers,2)); m_strText.Append(L"Max is: " + strTemp); strTemp.Format(L"%.2f", Min(numbers, 2)); m_strText.Append(L" Min is: " + strTemp); strTemp.Format(L"%.2f", Sum(numbers, 2)); m_strText.Append(L" Sum is: " + strTemp); strTemp.Format(L"%.2f", Average(numbers, 2)); m_strText.Append(L" Average is: " + strTemp); strTemp.Format(L"%d", GreatestCommonDivisor(m_Num1, m_Num2)); m_strText.Append(L" GDC is: " + strTemp); UpdateData(FALSE); }
Step 20 − When the above code is compiled and executed, you will see the following output.
Step 21 − Enter two values in the edit field and cpck Calculate. You will now see the result after calculating from the DLL.
Advertisements