C ++ creates and calls dll under

Source: Internet
Author: User

Code reuse is an important way to improve software development efficiency. Generally, as long as some code is universal, it can be constructed into a relatively independent functional module and reused in subsequent projects. A common example is various application frameworks, such as ATL and MFC, which are released in the form of source code. Because the reuse is "Source code level", the source code is completely exposed to programmers, so it is called "white box reuse ". There are many disadvantages of "white box reuse", which are summarized as 4 points.
Exposed source code; multiple copies, resulting in a waste of storage;
It is easy to name conflicts with the programmer's "common" code;
It is difficult to update functional modules, which is not conducive to modular implementation of problems;
In fact, the above four points are summarized as "exposed source code" resulting in "severe code coupling ". In order to make up for these shortcomings, we propose code reuse at the binary level. Code reuse at the binary level hides the source code to some extent, which plays a role in mitigating code coupling. Such reuse is called "black box reuse ".
: The "black box reuse" approach is not only dll, but also static Link Library and even more advanced COM components.

Reference Program Original: http://msdn.microsoft.com/zh-cn/library/ms235636.aspx
Create a Win32 project and select "DLL" as the application type. Add the header file testdll. h

 TESTDLL_API __declspec(dllexport)      TESTDLL_API __declspec(dllimport)                  TESTDLL_API  Add( a,          TESTDLL_API  Subtract( a,          TESTDLL_API  Multiply( a,          TESTDLL_API  Divide( a, 

When TESTDLL_EXPORTS is defined, TESTDLL_API is set to the _ declspec (dllexport) modifier ,. If not defined, TESTDLL_API is set to _ declspec (dllimport ),. When a DLL project is generated, TESTDLL_EXPORTS is defined by default, so the _ declspec (dllexport) modifier is set by default.
Add cpp File

#include <stdexcept>    MyMathFuncs::Add( a,  a + MyMathFuncs::Subtract( a,  a - MyMathFuncs::Multiply( a,  a * MyMathFuncs::Divide( a,  (b ==  invalid_argument( a /

After compilation, the corresponding dll file is generated, and the corresponding lib file is also generated.
: A. DLL exports the function declaration in two ways: Add _ declspec (dllexport) in the function declaration; Use the module definition (. def) file declaration. See: http://www.cnblogs.com/enterBeijingThreetimes/archive/2010/08/04/1792099.html
B. When creating a dll in the C file or using the C compiler to create a dll, we recommend that you use the extern "C" flag. For more information, see simple parsing of extern "C ".

Applications can use DLL in two ways: implicit link (CALL) and explicit link. Before using the DLL, you must first know the structure information of the function in the DLL. VS stores a small program named dumpbin.exe in the VC \ bindirectory. You can use it to view the function structure in the DLL file. For the comparison of the two types, see: http://blog.sina.com.cn/s/blog_53004b4901009h3b.html
Static Loading is adopted, which is relatively simple and requires three sets of. h,. lib, And. dll. Create a console application or an empty project ". The configuration is as follows:
Project-> properties-> Configuration properties-> VC ++ directory-> Add the directory where the header file testdll. h is located in "include directory"
Project-> properties-> Configuration properties-> VC ++ directory-> Add the directory where the header file testdll. lib is located in "library directory"
Project-> properties-> Configuration properties-> linker-> input-> Add "testdll. lib" to "additional dependencies" (Separate multiple lib by spaces)
Add cpp File

#include <iostream>    a =  b = <<  <<<<<<  <<<<<<  <<<<<<  <<<<<<  <<) << ( invalid_argument &<<  << e.what() << 

Now the compilation is successful, but an error is reported when the program runs. You also need to copy testdll. dll to the directory where the executable file generated by the current project is located.
The application can load DLL files at any time during execution, or unload DLL files at any time, which is not implemented by implicit links, so explicit links have better flexibility, it is more suitable for explanatory languages.
Create a project without special configuration. Add the cpp file.

<Windows.h> #include<iostream>  (*pAdd)( a,  (*pSubtract)( a, = LoadLibrary();     (hDLL !== pAdd(GetProcAddress(hDLL, MAKEINTRESOURCE()));         (fp1 !=<<fp1(, )<<<<<<<<= pSubtract(GetProcAddress(hDLL, ));         (fp2 !=<<fp2(, )<<<<<<<<<<<<<< 

In the DLL file, the function name in the dll project has changed (C ++ compiler). In the DLL file, the character after the change is called "name ". You can view the tracing applet. If you want to make the function name more standard under the C ++ Compiler (the same as in the original project), see http://blog.csdn.net/btwsmile/article/details/6676802.
Of course, to make the function name more standardized, the most common method is to use the C compiler to compile the function during dll creation, in this way, the function names in the DLL file are the same as those in the original dll project.

To solve the last part of the problem, you can use extern "C" to establish a C connection for the function in the dll project. A simple example project is as follows.
In the project created by DLL, add the cpp File

#include   {                   addfun( a,  a+

Compile to generate the DLL file. In the dll call Project, add the cpp File

<windows.h><iostream> (*FUNA)(,= LoadLibrary();    = (FUNA)GetProcAddress(hMod, TEXT(          (addfun !=<<addfun(, )<<<<<<<<<<

Run, so that you can call the dll function. Further, if the above dll file is called implicitly, the call function should be

#include <iostream> comment(lib,"cdll.lib")   _declspec(dllimport)  addfun( a,<<addfun(,)<<

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.