当前位置:网站首页>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
边栏推荐
- Common JVM tools and optimization strategies
- [speech processing] speech signal denoising and denoising based on MATLAB low-pass filter [including Matlab source code 1709]
- 14种神笔记方法,只需选择1招,让你的学习和工作效率提高100倍!
- 数学公式截图识别神器Mathpix无限使用教程
- CorelDRAW plug-in -- GMS plug-in development -- new project -- macro recording -- VBA editing -- debugging skills -- CDR plug-in (2)
- Three.js-01 入门
- openresty ngx_ Lua regular expression
- 派对的最大快乐值
- Simple and beautiful method of PPT color matching
- 【Note17】PECI(Platform Environment Control Interface)
猜你喜欢

Hcip day 12 (BGP black hole, anti ring, configuration)

Three. Js-01 getting started

Error when LabVIEW opens Ni instance finder

Go language implementation principle -- lock implementation principle

2: Chapter 1: understanding JVM specification 1: introduction to JVM;

Basic knowledge of database (interview)

2:第一章:认识JVM规范1:JVM简介;

Three.JS VR看房

Masked Autoencoders Are Scalable Vision Learners (MAE)

3: Chapter 1: understanding JVM specification 2: JVM specification, introduction;
随机推荐
audiopolicy
2:第一章:认识JVM规范1:JVM简介;
Alibaba Tianchi SQL training camp task4 learning notes
证明 poj 1014 模优化修剪,部分递归 有错误
Yiwen gets rid of the garbage collector
Use of metadata in golang grpc
Three.js-01 入门
使用rewrite规则实现将所有到a域名的访问rewrite到b域名
Matlab smooth curve connection scatter diagram
Activate function and its gradient
Practice of concurrent search
Element operation and element waiting in Web Automation
Starting from 1.5, build a micro Service Framework -- log tracking traceid
【Note17】PECI(Platform Environment Control Interface)
SPSS analysis of employment problems of college graduates
3: Chapter 1: understanding JVM specification 2: JVM specification, introduction;
从 1.5 开始搭建一个微服务框架——日志追踪 traceId
Hcip day 11 (BGP agreement)
数学公式截图识别神器Mathpix无限使用教程
C Primer Plus Chapter 9 question 9 POW function