当前位置:网站首页>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
边栏推荐
- Week 17 homework
- Three.JS VR看房
- 6-axis and 9-axis IMU attitude estimation
- MoCo: Momentum Contrast for Unsupervised Visual Representation Learning
- LeetCode145. Post order traversal of binary tree (three methods of recursion and iteration)
- Simple and beautiful method of PPT color matching
- Un article traite de la microstructure et des instructions de la classe
- Leetcode sword finger offer brush questions - day 21
- grafana工具界面显示报错influxDB Error
- 14种神笔记方法,只需选择1招,让你的学习和工作效率提高100倍!
猜你喜欢
Practice of concurrent search
Go语言实现原理——锁实现原理
Tensor attribute statistics
Sum of two numbers, sum of three numbers (sort + double pointer)
基于脉冲神经网络的物体检测
Realize reverse proxy client IP transparent transmission
Selenium+pytest automated test framework practice
Negative sampling
Three. JS VR house viewing
3:第一章:认识JVM规范2:JVM规范,简介;
随机推荐
Negative sampling
Week 17 homework
Object detection based on impulse neural network
Design and implementation of secsha system
Finally understand what dynamic planning is
CJ mccullem autograph: to dear Portland
Use of shell:for loop
Selenium+pytest automated test framework practice
Simple and beautiful method of PPT color matching
二叉树递归套路总结
Alibaba Tianchi SQL training camp task4 learning notes
The maximum happiness of the party
判斷二叉樹是否為完全二叉樹
February 13, 2022 -5- maximum depth of binary tree
3D point cloud slam
Leecode learning notes
openresty ngx_lua请求响应
2:第一章:认识JVM规范1:JVM简介;
Detailed explanation of pointer and array written test of C language
regular expression