当前位置:网站首页>VS2010编写动态链接库DLL和单元测试,转让DLL测试的正确性
VS2010编写动态链接库DLL和单元测试,转让DLL测试的正确性
2022-07-05 23:02:00 【全栈程序员站长】
大家好,又见面了,我是全栈君
本文将创建一个简单的动态库-link,谱写控制台应用程序使用该动态链接库,该动态链接库为“JAVA调用动态链接库DLL之JNative学习”中使用的DLL,仅仅是项目及文件名不同。
创建动态链接库项目: 1、打开Microsoft Visual Studio 2010,选择文件->新建->项目。
2、在新建项目窗体中选择其他语言->Visual C++->Win32。
3、选择Win32 项目,设置名称:simpleDLL。设置解决方式名:simpleDLL。 4、单击确定。在出现的Win32 应用程序向导的概述对话框中点击下一步。
5、在应用程序设置中,选择应用程序类型下的DLL。
6、勾选附加选项下的空项目。 7、单击完毕创建项目。 向动态链接库加入类: 1、加入新类头文件。右键单击simpleDLL项目,加入->新建项,选择头文件(.h),设置名称为simpleDLL,单击加入。
2、加入新类源文件。右键单击simpleDLL项目,加入->新建项,选择C++ 文件(.cpp),设置名称为simpleDLL。单击加入。
3、为新类加入内容。内容例如以下:
头文件simpleDLL.h:
//------------------ SimpleDLL.h ----------------
#pragma once;
//该宏完毕在dll项目内部使用__declspec(dllexport)导出
//在dll项目外部使用时。用__declspec(dllimport)导入
//宏DLL_IMPLEMENT在SimpleDLL.cpp中定义
#ifdef DLL_IMPLEMENT
#define DLL_API __declspec(dllexport)
#else
#define DLL_API __declspec(dllimport)
#endif
DLL_API int add(int x, int y); //简单方法
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);
源文件simpleDLL.cpp:
//------------------ SimpleDLL.cpp ----------------
//注意此处的宏定义须要写在#include "SimpleDLL.h"之前
//以完毕在dll项目内部使用__declspec(dllexport)导出
//在dll项目外部使用时,用__declspec(dllimport)导入
#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://中文");
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://中文");
strcat_s(url, mgrIp);
strcat_s(url, ":");
char szPort[20] = { 0 };
_ltoa_s(mgrPort, szPort, 10);
strcat_s(url, szPort);
return url;
}
创建引用动态链接库的应用程序: 1、在解决方式上单击鼠标右键->加入->新建项目。
2、在加入新项目中选择其他语言->Visual C++->Win32。
3、选择Win32 控制台应用程序。设置名称:simpleDLLTest。
4、单击确定。在出现的Win32 应用程序向导的概述对话框中点击下一步。
5、在应用程序设置中。选择应用程序类型下的控制台应用程序。
6、单击完毕创建项目。
在控制台应用程序中使用类库的功能: 1、为SimpleDLLTest.cpp加入内容。
例如以下所看到的:
// SimpleDLLTest.cpp : 定义控制台应用程序的入口点。
#include "stdafx.h"
#include "../SimpleDLL/SimpleDLL.h" //加入头文件引用
#pragma comment(lib, "..\\..\\SimpleDLL\\Release\\SimpleDLL.lib") //加入lib文件引用
#include <process.h>
#include <locale.h>
int _tmain(int argc, _TCHAR* argv[])
{
setlocale(LC_ALL, "chs"); //配置地域化信息为中文简体,否则打印出来的中文是乱码
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、引用simpleDLL项目。
右键单击SimpleDLLTest项目。选择项目依赖项。
3、依赖于窗体中勾选SimpleDLL。单击确定。
4、设置SimpleDLLTest项目为活动项目。右键单击SimpleDLLTest项目,选择设为启动项目。
6、生成解决方式。
Debug执行结果例如以下:
注意:如今创建的DLL仅仅能由c++调用,C语言等其他语言是调用不了的!
我们来用工具看一下,在解决方式上单击鼠标右键。在Windows资源管理器中打开目录
我编译的是Release版本号,所以打开Release目录,找到SimpleDLL.dll文件。用Depends工具打开它
眼下编译的版本号对MSVCR100.DLL还有依赖。这样拷贝到其他没有安装VS2010的电脑上是用不了的;
在项目上单击鼠标右键,属性:
在左側的配置属性中选择常规。在右側找到MFC的使用,选择在静态库中使用MFC
这里是改动MFC的使用。还有还有一种方法:vs2010公布时去除msvcp100.dll和msvcr100.dll图讲解明
再看函数,带有一些特殊字符。这样c++之外其他语言是调用不了的;我们须要在头文件的函数声明中加入extern “C”标志,代码例如以下:
//------------------ SimpleDLL.h ----------------
#pragma once;
//该宏完毕在dll项目内部使用__declspec(dllexport)导出
//在dll项目外部使用时。用__declspec(dllimport)导入
//宏DLL_IMPLEMENT在SimpleDLL.cpp中定义
#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); //简单方法
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);
又一次编译,再 用 Depends工具打开它,能够发现依赖项已经没有了。函数名称也正常了:
解决方式源代码下载:http://download.csdn.net/detail/testcs_dn/7411383
版权声明:本文博客原创文章。博客,未经同意,不得转载。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/117539.html原文链接:https://javaforall.cn
边栏推荐
- 3D reconstruction of point cloud
- [digital signal denoising] improved wavelet modulus maxima digital signal denoising based on MATLAB [including Matlab source code 1710]
- The method and principle of viewing the last modification time of the web page
- (4)UART应用设计及仿真验证2 —— RX模块设计(无状态机)
- Un article traite de la microstructure et des instructions de la classe
- Go language implementation principle -- lock implementation principle
- Mathematical formula screenshot recognition artifact mathpix unlimited use tutorial
- LeetCode145. Post order traversal of binary tree (three methods of recursion and iteration)
- 2022 R2 mobile pressure vessel filling review simulation examination and R2 mobile pressure vessel filling examination questions
- C Primer Plus Chapter 9 question 9 POW function
猜你喜欢
Go language implementation principle -- lock implementation principle
2022.02.13 - SX10-30. Home raiding II
2022 registration examination for safety management personnel of hazardous chemical business units and simulated reexamination examination for safety management personnel of hazardous chemical busines
LeetCode102. Sequence traversal of binary tree (output by layer and unified output)
3: Chapter 1: understanding JVM specification 2: JVM specification, introduction;
数据库基础知识(面试)
视频标准二三事
Sum of two numbers, sum of three numbers (sort + double pointer)
openresty ngx_ Lua request response
LabVIEW打开PNG 图像正常而 Photoshop打开得到全黑的图像
随机推荐
Practice of concurrent search
(4)UART应用设计及仿真验证2 —— RX模块设计(无状态机)
从 1.5 开始搭建一个微服务框架——日志追踪 traceId
Krypton Factor-紫书第七章暴力求解
(4)UART应用设计及仿真验证2 —— TX模块设计(无状态机)
leecode-学习笔记
实现反向代理客户端IP透传
判斷二叉樹是否為完全二叉樹
Multi camera stereo calibration
openresty ngx_lua正则表达式
Un article traite de la microstructure et des instructions de la classe
Go language implementation principle -- lock implementation principle
数据库基础知识(面试)
audiopolicy
如何快速理解复杂业务,系统思考问题?
Thoroughly understand JVM class loading subsystem
Roman numeral to integer
Ultrasonic sensor flash | LEGO eV3 Teaching
Error when LabVIEW opens Ni instance finder
Element positioning of Web Automation