Make a C language DLL file with VS2010 and invoke the DLL file in other programs

Source: Internet
Author: User

First, why do I need DLL

Code reuse is an important way to improve the efficiency of software development. In general, as long as a part of the code is generic, it can be constructed as a relatively independent function module and reused in subsequent projects. A more common example is a variety of application frameworks, such as ATL, MFC, and so on, which are published in the form of source code. Since this reuse is "source level", the source code is completely exposed to the programmer, so called "white box Reuse". "White box reuse" of the shortcomings are more, summed up there are 4 points.

    1. exposing the source code;
    2. Easy to name conflict with the programmer's "normal" code;
    3. Multiple copies, resulting in wasted storage;
    4. Updating the function module is difficult.

In fact, the above 4 points are summed up as "exposed source code" resulting in "severe coding coupling." To compensate for these shortcomings, a "binary level" code reuse is proposed. The use of binary-level code reuse to some extent hides the source code, to alleviate the code coupling phenomenon played a role. This reuse is referred to as "black box multiplexing".

There are two types of executables in the Windows operating system, with the following suffix. exe and. dll. The difference is that the. exe file can be run independently of the memory, and the. dll file cannot, it can only be called by other processes. However, in any format, they are binary files. The above-mentioned "binary level" code reuse can be implemented using. dll.

Compared to white-box multiplexing, the. dll largely compensates for these 4 major flaws.. dll is a binary file, so the source code is hidden, and if you take an "explicit call" (which is mentioned in the back), there is no naming conflict; The. dll is dynamically linked to the application, it is not copied into the program when the link is delivered, and the. dll file is relatively independent, so it is possible to update the functional module.

Description: The way to implement "black box multiplexing" is not only a DLL, but also a static link library and even more advanced COM components. This article discusses only DLLs.

Second, C language production DLL file

1. Start vs2010,

2. Create a DLL project

A. Win32 Console application, new project, file--

B. Enter the project name, e.g. DLL, click OK

C. Click Next, tick the DLL and the empty project in the "Application Settings screen", click the Finish button

D. View, Solution Explorer, right-click Header file, add New item, here with Dll.h,

E. Right-click on "source file", add New item, here we add dll.c, to this DLL construction completed.

3. dll.h

#ifndef axlplugin_h   #define Axlplugin_h  /**/  #ifdef _windows  #define dll_declare __declspec (dllexport)   #else  #define Dll_declare    #endifint Min (intint  b);   /*  */  #endif  

4. dll.c

#include"dll.h"#include<stdio.h>/*Add the appropriate header file as needed*/Dll_declareintMin (intAintb) {if(A >=b)returnb; Else           returnA; }  /*all the declared functions are implemented here.*/  

5. Generate DLL lib file

Click Build, Build DLL

In this way, the DLL and LIB files are generated.

5. Use of DLL files

Test proves that the Dll.dll file is to be used with Dll.lib and dll.h files

Third, load the DLL in the program

    1. Create a new "Win32 application" for testing
    2. Operation:

A. Project->win32 console application, file-new.

B. Enter the project name, here we use Test_dll, click OK button.

C. Click Next, in the "Application Settings Screen Settings" Console Application and "Empty Project", click the Finish button.

D. Copy the Dll.h/dll.dll/dll.lib in the project DLL directory into the project Test_dll directory.

E. View, Solution Explorer, right click on "header file", add New item, here we use dll.h

Right click "source File", add New item, here we add test_dll.c, right click "Resource File",

Add,"Existing Item", select Dll.lib, to this Test_dll project is completed.

3. Edit the Test_dll.c file with the following content

 #include   "  #include  <stdio.h> int  Span style= "color: #000000;" > main () {printf (  min (2, 4) =%d\n  , Min (2 , 4         min (5, 2) =%d\n   ", Min (5 , 2  ));  return  0   

four, DLL and Test_dll project directory structure

.. /test_dll/│test_dll.sdf│test_dll.sln│test_dll_dir.txt├─debug│test_dll.exe│test_dll.ilk│tes T_dll.pdb├─ipch│└─test_dll-Eb5063a1│test_dll-C06C53E7.IPCH└─TEST_DLL│DLL.DLL│DLL.H│DLL.LIB│TEST_DLL.C│TEST_DLL.V Cxproj│test_dll.vcxproj.filters│test_dll.vcxproj.user└─debug Cl.command.1. TLog Cl.read.1. TLog Cl.write.1. TLog Link-cvtres.read.1. TLog Link-cvtres.write.1. TLog link.3004-cvtres.read.1. TLog link.3004-cvtres.write.1. TLog link.3004. read.1. TLog link.3004. Write.1. TLog Link.command.1. TLog Link.read.1. TLog Link.write.1. TLog Mt.command.1. TLog Mt.read.1. TLog Mt.write.1. TLog Rc.command.1. TLog Rc.read.1. TLog Rc.write.1. TLog Test_dll. Build.CppClean.log test_dll.exe.embed.manifest test_dll.exe.embed.manifest.res Test              _dll.exe.intermediate.manifest test_dll.lastbuildstate Test_dll.log Test_dll.obj Test_dll_manifest.rc vc100.idb vc100.pdb

Make a C language DLL file with VS2010 and invoke the DLL file in other programs

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.