当前位置:网站首页>VS2010 writes DLL and unit test of dynamic link library, and transfers the correctness of DLL test
VS2010 writes DLL and unit test of dynamic link library, and transfers the correctness of DLL test
2022-07-05 23:15:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm the king of the whole stack
This article will create a simple dynamic library -link, Compose console applications use the DLL , The DLL is “JAVA Call DLL DLL And JNative Study ” Used in DLL, It's just that the project and the file name are different .
Create a dynamically linked library project : 1、 open Microsoft Visual Studio 2010, Select File -> newly build -> project .
2、 Select another language in the new project form ->Visual C++->Win32.
3、 choice Win32 project , Set the name :simpleDLL. Set the solution name :simpleDLL. 4、 Click OK . In the presence of Win32 Click next in the overview dialog box of the application wizard .
5、 In application settings , Select DLL.
6、 Check the blank item under the additional option . 7、 Click Finish to create the project . Add classes to the dynamic link library : 1、 Add a new class header . Right click simpleDLL project , Join in -> New item , Select header file (.h), Set the name to simpleDLL, Click join .
2、 Add a new class source file . Right click simpleDLL project , Join in -> New item , choice C++ file (.cpp), Set the name to simpleDLL. Click join .
3、 Add content to the new class . The content is as follows :
The header file simpleDLL.h:
//------------------ SimpleDLL.h ----------------
#pragma once;
// The macro ends in dll Project internal use __declspec(dllexport) export
// stay dll When used outside the project . use __declspec(dllimport) Import
// macro DLL_IMPLEMENT stay SimpleDLL.cpp In the definition of
#ifdef DLL_IMPLEMENT
#define DLL_API __declspec(dllexport)
#else
#define DLL_API __declspec(dllimport)
#endif
DLL_API int add(int x, int y); // Simple method
DLL_API const wchar_t* getPlayUrl(const wchar_t* mgrIp, long mgrPort, long materialId);
DLL_API const char* getUrl(const char* mgrIp, long mgrPort, long materialId);Source file simpleDLL.cpp:
//------------------ SimpleDLL.cpp ----------------
// Note that the macro definition here needs to be written in #include "SimpleDLL.h" Before
// End in dll Project internal use __declspec(dllexport) export
// stay dll When used outside the project , use __declspec(dllimport) Import
#define DLL_IMPLEMENT
#include "SimpleDLL.h"
#include<Windows.h>
#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 };
wcscpy_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, "http:// chinese ");
strcat_s(url, mgrIp);
strcat_s(url, ":");
char szPort[20] = { 0 };
_ltoa_s(mgrPort, szPort, 10);
strcat_s(url, szPort);
return url;
}Create applications that reference dynamic link libraries : 1、 Right click the solution -> Join in -> New projects .
2、 Choose another language when adding a new project ->Visual C++->Win32.
3、 choice Win32 Console Application . Set the name :simpleDLLTest.
4、 Click OK . In the presence of Win32 Click next in the overview dialog box of the application wizard .
5、 In application settings . Select console application under application type .
6、 Click Finish to create the project .
Use the functions of the class library in the console application : 1、 by SimpleDLLTest.cpp Add content .
As you can see below :
// SimpleDLLTest.cpp : Defines the entry point for the console application .
#include "stdafx.h"
#include "../SimpleDLL/SimpleDLL.h" // Add header file reference
#pragma comment(lib, "..\\..\\SimpleDLL\\Release\\SimpleDLL.lib") // Join in lib File reference
#include <process.h>
#include <locale.h>
int _tmain(int argc, _TCHAR* argv[])
{
setlocale(LC_ALL, "chs"); // Configure localized information in simplified Chinese , 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、 quote simpleDLL project .
Right click SimpleDLLTest project . Select project dependencies .
3、 Depend on the check in the form SimpleDLL. Click OK .
4、 Set up SimpleDLLTest The project is an active project . Right click SimpleDLLTest project , Select set as startup project .
6、 Generate solutions .
Debug The execution results are as follows :
Be careful : Created today DLL Only by c++ call ,C Language and other languages cannot be called !
Let's take a look with tools , Right click the solution . stay Windows Open the directory in Explorer
What I compiled is Release Version number , So open Release Catalog , find SimpleDLL.dll file . use Depends Tools To open it
The currently compiled version number is correct MSVCR100.DLL And dependence . In this way, it is copied to other devices without installation VS2010 You can't use it on your computer ;
Right click the item , attribute :
Select general in the configuration properties on the left . Find... On the right MFC Use , Choose to use... In a static library MFC
Here are the changes MFC Use . There is another way :vs2010 Remove when publishing msvcp100.dll and msvcr100.dll Picture explanation
Let's look at functions , With some special characters . such c++ Other languages cannot be called ; We need to add extern “C” sign , The code is as follows :
//------------------ SimpleDLL.h ----------------
#pragma once;
// The macro ends in dll Project internal use __declspec(dllexport) export
// stay dll When used outside the project . use __declspec(dllimport) Import
// macro DLL_IMPLEMENT stay SimpleDLL.cpp In the definition of
#ifdef DLL_IMPLEMENT
#define DLL_API __declspec(dllexport)
#else
#define DLL_API __declspec(dllimport)
#endif
extern "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 , Again use Depends Tool open it , It can be found that the dependencies have disappeared . The function name is also normal :
Refer to : rehearse : Create and use dynamic link libraries (C++)
Solution: download the source code :http://download.csdn.net/detail/testcs_dn/7411383
Copyright notice : This article is an original blog article . Blog , Without consent , Shall not be reproduced .
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/117539.html Link to the original text :https://javaforall.cn
边栏推荐
- Use the rewrite rule to rewrite all accesses to the a domain name to the B domain name
- Global and Chinese markets for children's amusement facilities 2022-2028: Research Report on technology, participants, trends, market size and share
- What is the process of building a website
- Selenium+Pytest自动化测试框架实战
- asp.net弹出层实例
- [speech processing] speech signal denoising and denoising based on Matlab GUI low-pass filter [including Matlab source code 1708]
- The maximum happiness of the party
- 一文搞定垃圾回收器
- poj 2762 Going from u to v or from v to u? (推断它是否是一个薄弱环节图)
- Go language implementation principle -- lock implementation principle
猜你喜欢

Use of metadata in golang grpc

Detailed explanation of pointer and array written test of C language

【Note17】PECI(Platform Environment Control Interface)

查看网页最后修改时间方法以及原理简介

LeetCode102. Sequence traversal of binary tree (output by layer and unified output)

The PNG image is normal when LabVIEW is opened, and the full black image is obtained when Photoshop is opened

Data type, variable declaration, global variable and i/o mapping of PLC programming basis (CoDeSys)

Codeforces Global Round 19

audiopolicy

Go语言实现原理——Map实现原理
随机推荐
两数之和、三数之和(排序+双指针)
LeetCode102. Sequence traversal of binary tree (output by layer and unified output)
Judge whether the binary tree is a complete binary tree
Hcip day 12 (BGP black hole, anti ring, configuration)
派对的最大快乐值
C Primer Plus Chapter 9 question 10 binary conversion
14种神笔记方法,只需选择1招,让你的学习和工作效率提高100倍!
Un article traite de la microstructure et des instructions de la classe
Composition of interface
npm ELECTRON_ Mirror is set as domestic source (npmmirror China mirror)
Non rigid / flexible point cloud ICP registration
判断二叉树是否为完全二叉树
Realize reverse proxy client IP transparent transmission
Registration and skills of hoisting machinery command examination in 2022
poj 2762 Going from u to v or from v to u? (推断它是否是一个薄弱环节图)
C Primer Plus Chapter 9 question 9 POW function
LeetCode——Add Binary
Go language implementation principle -- lock implementation principle
[untitled]
UART Application Design and Simulation Verification 2 - TX Module Design (Stateless machine)