Use of Windows dynamic link library DLL

Source: Internet
Author: User

Windows program design uses a dynamic link library to effectively separate modules of large projects. dll mainly provides the function call interface (function name) for other external reference programs to call, the caller can still call the function based on the provided function name, function type, and function parameters without knowing how to implement the dynamic link library. When creating a DLL in a Windows program, you can select Win32 dynamic-Link Library and MFC Appwizard [DLL]. The differences between the two types of DLL are as follows:

A. Non-mfc dll: Refers to a DLL written directly in C language without the use of the MFC class library structure. The output function generally uses the Standard C interface, it can be called by applications not compiled by MFC or MFC.

B. regular DLL: similar to the following extension DLLs, it is written in the MFC class library. Obviously, there is a class that inherits cwinapp in the source file. It can be subdivided into static connection to MFC and dynamic connection to MFC.

C. extension DLL: Used to reuse the classes inherited from MFC. That is to say, a dynamic Connection Library of this type can be used to output a class inherited from MFC. Its output functions can only be used by applications that use MFC and dynamically link to MFC. You can inherit the classes you want from MFC that are more suitable for your use and provide them to your applications. You can also provide the object pointer of the MFC or MFC inheritance class to your application at will. Extension DLL is created using the dynamic connection version of MFC, and is called only by applications written in the MFC class library.

 

The procedure for creating a DLL in Win32 dynamic-Link Library is as follows:

1. Create a project based on Win32 dynamic-Link Library

2. Create the. cpp and. H files respectively, and define the export function in the. h file, that is, the function called by other programs. cpp implements the export function defined in. h.

For example:

Define two export functions maxyueshu and minbeishu in the. h file.

# Ifndef adddll_h
# Define adddll_h
Extern "C" int _ declspec (dllexport) maxyueshu (INT num1, int num2); // defines the export Function
Extern "C" int _ declspec (dllexport) minbeishu (INT num1, int num2 );
# Endif

Implement the export function in the. cpp file. Here we want to calculate the maximum common divisor and the lowest common multiple of two numbers.

# Include "adddll. H"
Int maxyueshu (INT num1, int num2)
{
Int temp;
While (num1% num2)
{
Temp = num2;
Num2 = num1 % num2;
Num1 = temp;
}
Return num2;
}
Int minbeishu (INT num1, int num2)
{
Int temp;
If (num1 <num2)
{
Temp = num1;
Num1 = num2;
Num2 = temp;
}
For (INT I = 1; I <= num2; I ++)
{
If (! (Num1 * I) % num2 ))
{
Return num1 * I;
}
}
}

Then, compile the connection and generate the corresponding DLL file in the DEBUG directory. This completes the creation of a DLL file. Next, reference it and recreate a project, copy the DLL file generated above to the DEBUG directory of the project, and then add the Code where it needs to be referenced.

Typedef int (* lpaddfun) (INT, INT); // declare a function pointer
Hinstance hdll; // defines the handle of the received DLL file
Lpaddfun addfun;
Hdll = loadlibrary (".. \ debug \ adddll2.dll"); // load the DLL
If (hdll! = NULL)
{
Addfun = (lpaddfun) getprocaddress (hdll, "minbeishu"); // obtain the address of the minbeishu function in the DLL
If (addfun! = NULL)
{
Int result = addfun (2, 3); // call the Function
Cstring STR;
Str. Format ("% d", result );
M_result.setwindowtext (STR );
}
Freelibrary (hdll); // release the DLL
}

 

This completes the use of a Win32 dynamic-Link Library DLL.

 

Next, we will introduce how to use MFC Appwizard [DLL] (conventional DLL). Conventional dll can be divided into static connection, dynamic connection, and extended DLL, because the conventional DLL that dynamically connects to MFC is relatively small, this section describes how to use conventional DLL for dynamic connection to MFC;

1. Create the DLL of the MFC Appwizard [DLL Project

2. Add the export function to the. Def file. The following two export functions are declared as the same as above,

For example:

; Maxyueshuandminbeishu. Def: Declares the module parameters for the DLL.

Library "maxyueshuandminbeishu" // DLL identifier (name)
Description 'maxyueshuandminbeishu Windows dynamic link library' // DLL description

Exports
; Explicit exports can go here
Maxyueshu; // export the Function
Minbeishu;

3. Declare the export function in the. h file.

// Maxyueshuandminbeishu. h: Main header file for the maxyueshuandminbeishu DLL
//

# If! Defined (partition _)
# Define DEFINE _

# If _ msc_ver> 1000
# Pragma once
# Endif // _ msc_ver> 1000

# Ifndef _ afxwin_h __
# Error include 'stdafx. H' before including this file for PCH
# Endif

# Include "resource. H" // Main Symbols

//////////////////////////////////////// /////////////////////////////////////
// Cmaxyueshuandminbeishuapp
// See maxyueshuandminbeishu. cpp for the implementation of this class
//

Class cmaxyueshuandminbeishuapp: Public cwinapp
{
Public:
Cmaxyueshuandminbeishuapp ();

// Overrides
// Classwizard generated virtual function overrides
// {Afx_virtual (cmaxyueshuandminbeishuapp)
//} Afx_virtual

// {Afx_msg (cmaxyueshuandminbeishuapp)
// Note-The classwizard will add and remove member functions here.
// Do not edit what you see in these blocks of generated code!
//} Afx_msg
Declare_message_map ()
};

Extern "C" int Pascal maxyueshu (INT num1, int num2 );
Extern "C" int Pascal minbeishu (INT num1, int num2 );
//////////////////////////////////////// /////////////////////////////////////

// {Afx_insert_location }}
// Microsoft Visual C ++ will insert additional declarations immediately before the previous line.

# Endif //! Defined (partition _)

 

4. Then implement it in the. cpp File

// Maxyueshuandminbeishu. cpp: defines the initialization routines for the DLL.
//

# Include "stdafx. H"
# Include "maxyueshuandminbeishu. H"

# Ifdef _ debug
# Define new debug_new
# UNDEF this_file
Static char this_file [] = _ file __;
# Endif

//
// Note!
//
// If this DLL is dynamically linked against the MFC
// DLLs, any functions exported from this dll which
// Call into MFC must have the afx_manage_state macro
// Added at the very beginning of the function.
//
// For example:
//
// Extern "C" bool Pascal export exportedfunction ()
//{
// Afx_manage_state (afxgetstaticmodulestate ());
/// Normal function body here
//}
//
// It is very important that this macro appear in each
// Function, prior to any callinto MFC. This means that
// It must appear as the first statement within
// Function, even before any object variable declarations
// As their constructors may generate callinto the MFC
// DLL.
//
// Please see MFC technical notes 33 and 58 for additional
// Details.
//

//////////////////////////////////////// /////////////////////////////////////
// Cmaxyueshuandminbeishuapp

Begin_message_map (cmaxyueshuandminbeishuapp, cwinapp)
// {Afx_msg_map (cmaxyueshuandminbeishuapp)
// Note-The classwizard will add and remove mapping macros here.
// Do not edit what you see in these blocks of generated code!
//} Afx_msg_map
End_message_map ()

//////////////////////////////////////// /////////////////////////////////////
// Cmaxyueshuandminbeishuapp Construction

Cmaxyueshuandminbeishuapp: cmaxyueshuandminbeishuapp ()
{
// Todo: Add construction code here,
// Place all significant initialization in initinstance
}

//////////////////////////////////////// /////////////////////////////////////
// The one and only cmaxyueshuandminbeishuapp object

Cmaxyueshuandminbeishuapp theapp;

// Implement the export Function
Extern "C" int Pascal maxyueshu (INT num1, int num2)
{
Afx_manage_state (afxgetstaticmodulestate ());
 
Int temp;
While (num1% num2)
{
Temp = num2;
Num2 = num1 % num2;
Num1 = temp;
}
Return num2;
}

Extern "C" int Pascal minbeishu (INT num1, int num2)
{
Afx_manage_state (afxgetstaticmodulestate ());
Int temp;
If (num1 <num2)
{
Temp = num1;
Num1 = num2;
Num2 = temp;
}
For (INT I = 1; I <= num2; I ++)
{
If (! (Num1 * I) % num2 ))
{
Return num1 * I;
}
}
}

 

5. Compile and connect. The corresponding DLL file will also be generated in the DEBUG directory, which can be called by other programs.

Create an MFC project and call the Code as follows:

Updatedata (true );
INT (_ stdcall * lpfn) (INT, INT); // declare the function pointer
Hinstance hinst; // declare the DLL handle
Hinst = loadlibrary ("maxyueshuandminbeishu. dll"); // load the DLL
If (hinst = NULL)
{
Afxmessagebox ("loading DLL failed! ");
Return;
}
(Farproc &) lpfn = getprocaddress (hinst, "maxyueshu"); // obtain the maxyueshu function address. The getprocaddress function returns a farproc
If (lpfn = NULL)
{
Afxmessagebox ("An error occurred while reading the function address! ");
Return;
}
Int result = lpfn (m_num1, m_num2 );
Cstring STR;
Str. Format ("% d", result );
M_result.setwindowtext (STR );
Freelibrary (hinst );

This completes a call.

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.