当前位置:网站首页>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
边栏推荐
- Debian 10 installation configuration
- Practice of concurrent search
- LabVIEW打开PNG 图像正常而 Photoshop打开得到全黑的图像
- 2022.02.13 - SX10-30. Home raiding II
- 两数之和、三数之和(排序+双指针)
- Alibaba Tianchi SQL training camp task4 learning notes
- CorelDRAW plug-in -- GMS plug-in development -- new project -- macro recording -- VBA editing -- debugging skills -- CDR plug-in (2)
- Douban scoring applet Part-2
- The method and principle of viewing the last modification time of the web page
- Leetcode daily question 1189 The maximum number of "balloons" simple simulation questions~
猜你喜欢
Non rigid / flexible point cloud ICP registration
[untitled]
Error when LabVIEW opens Ni instance finder
[speech processing] speech signal denoising based on Matlab GUI Hanning window fir notch filter [including Matlab source code 1711]
LabVIEW打开PNG 图像正常而 Photoshop打开得到全黑的图像
Yiwen gets rid of the garbage collector
Practice of concurrent search
Go语言实现原理——锁实现原理
Leetcode daily question 1189 The maximum number of "balloons" simple simulation questions~
【Note17】PECI(Platform Environment Control Interface)
随机推荐
CorelDRAW plug-in -- GMS plug-in development -- new project -- macro recording -- VBA editing -- debugging skills -- CDR plug-in (2)
Data type, variable declaration, global variable and i/o mapping of PLC programming basis (CoDeSys)
Week 17 homework
【Note17】PECI(Platform Environment Control Interface)
Element positioning of Web Automation
Thoroughly understand JVM class loading subsystem
Activate function and its gradient
Mathematical formula screenshot recognition artifact mathpix unlimited use tutorial
2022 G3 boiler water treatment simulation examination and G3 boiler water treatment simulation examination question bank
Go language implementation principle -- lock implementation principle
一文搞定垃圾回收器
使用rewrite规则实现将所有到a域名的访问rewrite到b域名
一文搞定class的微觀結構和指令
Selenium+pytest automated test framework practice
2022 R2 mobile pressure vessel filling review simulation examination and R2 mobile pressure vessel filling examination questions
LeetCode——Add Binary
Multi sensor fusion of imu/ optical mouse / wheel encoder (nonlinear Kalman filter)
Go语言实现原理——锁实现原理
Non rigid / flexible point cloud ICP registration
February 13, 2022 -5- maximum depth of binary tree