VS2010 writing dynamic-link library DLLs and unit tests, transferring the correctness of DLL tests

Source: Internet
Author: User
Tags visual studio 2010

This article will create a simple dynamic library-link to compose Control Desk Application The program uses the dynamic link library, which is the Java calls dll used in DLL jnative learning ", only the project and file name are different.

To create a dynamic-link library project:
1. Open Microsoft Visual Studio 2010, select New Project, File-


2. Select a different language->visual C++->win32 in the new project form.


3, select Win32 Project, set name: Simpledll. Set resolution Name:Simpledll.
4. Click OK. In the Overview dialog box of the Win32 Application Wizard that appears, click Next.


5. In the application settings, select the DLL under the application type.


6. Tick the empty item under Additional options.
7. Click Finish to create the project.
To add a class to a dynamic-link library:
1, add the new class header file. Right-click the Simpledll project, add New item, select the header file (. h), set the name to Simpledll, and click Join.

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvdgvzdgnzx2ru/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast "/>

2, add the new class source file. Right-click the Simpledll project, add New item, select C + + file (. cpp), and set the name to Simpledll. Click Join.



3. Add content to the new class. The contents are as follows:

header File simpleDLL.h:

------------------SimpleDLL.h----------------#pragma once;//This macro is finished using __declspec (dllexport) export within the DLL project// When used outside of a DLL project. Import//macros with __declspec (dllimport) dll_implement define #ifdef dll_implement#define Dll_api __declspec in SimpleDLL.cpp (dllexport ) #else # define DLL_API __declspec (dllimport) #endifDLL_API int Add (int x, int y); Simple method Dll_api Const wchar_t* getplayurl (const wchar_t* Mgrip, Long mgrport, long materialid);D Ll_api const char* GETURL (CO NST char* Mgrip, Long mgrport, long materialid);
source file SimpleDLL.cpp:

//----------- -------SimpleDLL.cpp----------------//Note that the macro definition here needs to be written before the # include "SimpleDLL.h"//To complete the use of __declspec within the DLL project (dllexport) Export//When used outside DLL projects, import the # define Dll_implement#include "SimpleDLL.h" #include <windows.h># with __declspec (dllimport) Include <intrin.h> #include <stdlib.h> #include <string.h>int dll_api Add (int x, int y) {return x+y;} Dll_api Const wchar_t* Getplayurl (const wchar_t* Mgrip, Long mgrport, long materialid) {static wchar_t url[260] = {0};wcs cpy_s (URL, l "/http//Chinese"); wcscat_s (URL, mgrip); wcscat_s (URL, l ":"); wchar_t szport[20] = {0};_ltow_s (mgrport, Szport, 10 ); wcscat_s (URL, szport); return URL;} Dll_api Const char* GETURL (const char* Mgrip, Long mgrport, long materialid) {static char url[260] = {0};strcpy_s (URL, "h ttp://Chinese "); strcat_s (URL, mgrip); strcat_s (URL,": "); char szport[20] = {0};_ltoa_s (mgrport, Szport, ten); strcat_s (URL, szport); return URL;} 

To create an application that references a dynamic-link library:
1. Click the right mouse button on the solution, and then join the new project.


2. Choose another language->visual C++->win32 to join the new project.


3. Select the Win32 console application. Setting Name: Simpledlltest.


4. Click OK. In the Overview dialog box of the Win32 Application Wizard that appears, click Next.


5. In the application settings. Select the console application under the application type.

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvdgvzdgnzx2ru/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast "/>
6. Click Finish to create the project .


To use the functionality of a class library in a console application:
1, add content for SimpleDLLTest.cpp.

For example, see the following:

SimpleDLLTest.cpp: Defines the entry point of the console application. #include "stdafx.h" #include ". /simpledll/simpledll.h "//Join header File Reference #pragma comment (lib,"). \\.. \\SimpleDLL\\Release\\SimpleDLL.lib ")//Join LIB file reference #include <process.h> #include <locale.h>int _tmain (int ARGC, _tchar* argv[]) {setlocale (Lc_all, "CHS");//Configure the localization information for Chinese Simplified, otherwise the printed Chinese is garbled wprintf (L "Getplayurl:%s\r\n", Getplayurl (L "127.0.0.1", 10087, 1)); printf ("GetUrl:%s\r\n", GetUrl ("127.0.0.1", 10087, 1)); System ("pause"); return 0;}
2, reference Simpledll project.

Right-click the simpledlltest project. Select the project Dependencies.


3 . Depending on the form, tick simpledll. Click OK.


4, set the Simpledlltest project as the active project. Right-click the simpledlltest project and select Set as Startup Project.


6, build the solution.

Debug execution results such as the following:


Note: DLLs created today can only be called by C + +, and other languages such as C are not callable!

Let's take a look at the tool and right-click on the solution. Open a directory in Windows Explorer


I compiled the release version number, so open the release directory and find SimpleDLL.dll file. Open it with the depends tool

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvdgvzdgnzx2ru/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast "/>

The version number compiled now has a dependency on MSVCR100.DLL. It is not available for copying to other computers that do not have VS2010 installed.

Right-click on the Item, properties:

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvdgvzdgnzx2ru/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast "/>

Select General in the configuration properties on the left side. On the right side, find MFC's use, choose to use MFC in the static library

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvdgvzdgnzx2ru/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast "/>

Here is the use of changes to MFC. There is also a way: Vs2010 released when the removal of Msvcp100.dll and Msvcr100.dll diagram explained

Look at the function again, with some special characters. In this way, other languages other than C + + are not callable; we need to include the extern "C" flag in the function declaration of the header file, such as the following code:

------------------SimpleDLL.h----------------#pragma once;//This macro is finished using __declspec (dllexport) export within the DLL project// When used outside of a DLL project. Import//macros with __declspec (dllimport) dll_implement define #ifdef dll_implement#define Dll_api __declspec in SimpleDLL.cpp (dllexport ) #else # define DLL_API __declspec (dllimport) #endifextern "C" Dll_api int Add (int x, int y);  Simple method extern "C" Dll_api const wchar_t* getplayurl (const wchar_t* Mgrip, Long mgrport, long materialid); extern "C" Dll_api Const char* GETURL (const char* Mgrip, Long mgrport, long materialid);
Compile again, and thenwithThe depends tool opens it to discover that the dependencies are gone. The function name is also normal:


References: Walkthrough: Creating and Using a dynamic-link library (c + +)

Solution source code Download: http://download.csdn.net/detail/testcs_dn/7411383

Copyright notice: This article blog original article. Blogs, without consent, may not be reproduced.

VS2010 Writing dynamic-link library DLLs and unit tests, transferring the correctness of DLL tests

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.