VC + +: Create, call Win32 dynamic link library

Source: Internet
Author: User

VC + +: Create, call Win32 dynamic link libraryOverview DLL (dynamic linkable library) DLL, which can be considered a warehouse, contains variables, functions, or classes that can be used directly.
The history of the warehouse has gone through "no Library"---> "Static link library"---> "Dynamic link library".
Static link library and dynamic link library can implement shared code, if the use of static link library, the instructions in the compiled Lib will be included in the generated EXE file, if the use of dynamic link library, it will not be included in the EXE file, exe file execution dynamic loading and unloading DLL files. Classification of the Library 1. Win32 Library (Non-MFC) Library
Win32 Library is also divided into: Dynamic link library and static link library
The Win32 library does not adopt the MFC class library structure, and the exported function is the standard C interface, which can be called by MFC or Non-MFC-written application.

2.MFC Library
MFC libraries are divided into: MFC rules DLLs and MFC extension DLLs
MFC Rule DLL: Contains a class that is integrated from CWinApp, but has no message loops.
MFC extension DLL: Created using MFC's dynamic link library, it can only be called by an application written by MFC. Create a Win32 dynamic link library using VS2010 to create 1. " New Project project named Wn32dll---> select Win32 Project---> Next tick "Dll" and "export symbol" such as: Wn32dll.cpp Add Custom Function: [HTML]View plain copy
  1. Wn32dll.cpp: Defines an export function for a DLL application.
  2. //
  3. #include "stdafx.h"
  4. #include "wn32dll.h"
  5. This is an example of exporting a variable
  6. Wn32dll_api int nwn32dll=0;
  7. This is an example of an exported function.
  8. Wn32dll_api int Fnwn32dll (void)
  9. {
  10. Return 42;
  11. }
  12. This is the constructor for the exported class.
  13. For information about class definitions, see Wn32dll.h
  14. Cwn32dll::cwn32dll ()
  15. {
  16. Return
  17. }
  18. /************* Custom Function ******************/
  19. WN32DLL_API int Wn_add (int a, int b)
  20. {
  21. return a + B;
  22. }
Functions for declaring a custom in wn32dll.h [HTML]View plain copy
  1. The following ifdef blocks are created to make it easier to export from a DLL
  2. The standard method for macros. All files in this DLL are defined on the command line Wn32dll_exports
  3. Symbols compiled. In the use of this DLL
  4. This symbol should not be defined on any other item. In this way, any other items in the source file that contain this file will
  5. The WN32DLL_API function is considered to be imported from a DLL, and this DLL will be defined with this macro
  6. Symbols are considered to be exported.
  7. #ifdef Wn32dll_exports
  8. #define WN32DLL_API __declspec (dllexport)
  9. #else
  10. #define WN32DLL_API __declspec (dllimport)
  11. #endif
  12. This class is derived from Wn32dll.dll.
  13. Class Wn32dll_api Cwn32dll {
  14. Public
  15. Cwn32dll (void);
  16. TODO: Add your method here.
  17. };
  18. extern Wn32dll_api int nwn32dll;
  19. Wn32dll_api int fnwn32dll (void);
  20. Custom Function **********************/
  21. WN32DLL_API int Wn_add (int a, int b);

Compile the project, generate the Wn32dll.dll and Wn32dll.lib files in the debug directory, and use the Dependency Walker tool to see if the Win32 Dynamic link library Mouse is successfully invoked in the solution right-click---> Add---> New Project ", the project name is" Dlltest ", select the Win32 Console project to add the following code in the DllTest.cpp source file: [HTML]View plain copy
  1. DllTest.cpp: Defines the entry point of the console application.
  2. //
  3. #include "stdafx.h"
  4. #include ". /wn32dll/wn32dll.h "
  5. #ifdef _DEBUG
  6. #pragma comment (lib, "... /debug/wn32dll.lib ")
  7. #else
  8. #pragma comment (lib, "... /release/wn32dll.lib ")
  9. #endif
  10. int _tmain (int argc, _tchar* argv[])
  11. {
  12. int result = Wn_add (1, 5);
  13. printf ("result =%d\n", result);
  14. GetChar ();
  15. return 0;
  16. }

Compile the connection to generate the DllTest.exe file with the following results:

VC + +: Create, call Win32 dynamic link library

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.