Introduction to C ++ conventional DLL Application

Source: Internet
Author: User

The DLL application in C ++ is a very important basic concept in actual programming. mastering this application plays an important role in program development. Here we will give a detailed introduction to C ++ conventional DLL, hoping to help you.

C ++ conventional DLL creation and use

1: export functions

 
 
  1. __declspec(dllexport) void ShowDlg(void)  
  2. {  
  3. CDllDialog dllDialog;  
  4. dllDialog.DoModal ();  

2: Modify the. def file and add the function name.

 
 
  1. ; DrawTestDll.def : Declares the module parameters for the DLL.  
  2.  
  3. LIBRARY "DrawTestDll"  
  4. DESCRIPTION 'DrawTestDll Windows Dynamic Link Library'  
  5.  
  6. EXPORTS  
  7. ; Explicit exports can go here 

3: copy the DLL file to the directory where the program is located.

4: Call the DLL in the program

 
 
  1. Void CDllTestView: OnLButtonDown (UINT nFlags, CPoint point)
  2. {
  3. Typedef void (* lpFun) (void );
  4. HINSTANCE hDll; // DLL handle
  5. HDll = LoadLibrary ("DrawTestDll. dll"); // load DLL Resources
  6. If (NULL = hDll) // search for the sequence, directory where EXE is located, directory where the project is located, and directory of System and windows
  7. {
  8. MessageBox ("DLL loading failed ");
  9. }
  10. LpFun addFun; // function pointer
  11. AddFun = (lpFun) GetProcAddress (hDll, "ShowDlg"); // obtain the address of the ShowDlg function.
  12. If (NULL = addFun)
  13. {
  14. MessageBox ("failed to search for functions in DLL ");
  15. }
  16. HINSTANCE exe_hinstance = AfxGetResourceHandle (); // obtain the instance handle of the main program
  17. AfxSetResourceHandle (hDll); // set the main program handle to a DLL instance to use resources in the DLL)
  18. AddFun (); // call a function
  19. AfxSetResourceHandle (exe_hinstance); // reset the instance handle of the main program
  20. FreeLibrary (hDll); // release DLL Resources
  21. }

C ++ common DLL class usage

1: Add a new class to the DLL

Class. h file:
 

 
 
  1. // Export a class (including its methods and attributes)
  2. Class _ declspec (dllexport) CDllClass {
  3. Public:
  4. CDllClass (void );
  5. Void MSG (const char * const str );
  6. };

Class. cpp file:

 
 
  1. #include "stdafx.h"  
  2. #include "DllClass.h"  
  3.  
  4. CDllClass::CDllClass()  
  5. {  
  6. return;  
  7. }  
  8. void CDllClass::MSG(const char * const str)  
  9. {  
  10. MessageBox(NULL,str,"",MB_OK);  

2: copy the compiled DLL files, Lib files, and Class header files to the project directory.

3: Modify the Class header file to convert the dll header file to dllexport and the application file to dllimport)

 
 
  1. // Import a class (including its methods and attributes)
  2. Class _ declspec (dllimport) CDllClass {
  3. Public:
  4. CDllClass (void );
  5. Void MSG (const char * const str );
  6. };

4: add the Class header file and the reference of the Lib file to the file that calls the class implementation function.

 
 
  1. # Include "DllClass. h" // introduce the header file, which is an object that can be defined by a program.
  2. # Pragma comment (lib, "DrawDll. lib") // define a function

It can be used normally. The above is the introduction to C ++ conventional DLL.

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.