以下内容,我看了多篇文章,整合在一起,写的一个例子,关于dll工程的创建,请参考博客里另一篇文章:http://www.cnblogs.com/pingge/articles/3153571.html

有什么不对的欢迎指正!!!

1.头文件

 //testdll.h
#ifndef _TESTDLL_H_
#define _TESTDLL_H_ #ifdef TESTDLL_EXPORTS
#define TESTDLL_API __declspec(dllexport) //这个修饰符使得函数能都被DLL输出,所以他能被其他应用函数调用
#else
#define TESTDLL_API __declspec(dllimport) //这个修饰符使得编译器优化DLL中的函数接口以便于被其他应用程序调用
#endif #ifdef __cplusplus
extern "C"
{
#endif namespace MathFuncs
{
// This class is exported from the testdll.dll
// Returns a + b
extern TESTDLL_API double _stdcall Add(double a, double b); // Returns a - b
extern TESTDLL_API double _stdcall Subtract(double a, double b); // Returns a * b
extern TESTDLL_API double _stdcall Multiply(double a, double b); // Returns a / b
// Throws const std::invalid_argument& if b is 0
extern TESTDLL_API double _stdcall Divide(double a, double b);
} #ifdef __cplusplus
}
#endif #endif

使用__declspec(dllexport)导出DLL中的函数,extern “C”标志规范导出函数的修饰名称,是C++工程也能调用dll函数。

 // testdll.cpp : 定义 DLL 应用程序的导出函数。

 #include "stdafx.h"
#include "testdll.h"
#include <stdexcept>
using namespace std; namespace MathFuncs
{
double _stdcall Add(double a, double b)
{
return a + b;
} double _stdcall Subtract(double a, double b)
{
return a - b;
} double _stdcall Multiply(double a, double b)
{
return a * b;
} double _stdcall Divide(double a, double b)
{
if (b == )
{
throw invalid_argument("b cannot be zero!");
}
return a / b;
}
}

以上是导出函数的定义。

 //demo.cpp
#include <iostream>
#include "testdll.h"
#pragma comment(lib,"testdll.lib")
using namespace std; int main()
{
double a = 7.4;
int b = ; cout << "a + b = " <<
MathFuncs::Add(a, b) << endl;
cout << "a - b = " <<
MathFuncs::Subtract(a, b) << endl;
cout << "a * b = " <<
MathFuncs::Multiply(a, b) << endl;
cout << "a / b = " <<
MathFuncs::Divide(a, b) << endl;
return ;
}

这是一个测试的demo,隐式连接调用dll函数,一定要三件套.h .lib .dll

欢迎指正!!!

DLL模块例2:使用__declspec(dllexport)导出函数,extern "C"规范修饰名称,隐式连接调用dll中函数的更多相关文章

  1. DLL模块例1:使用.def模块导出函数,规范修饰名称,显示连接调用dll中函数

    以下内容,我看了多篇文章,整合在一起,写的一个例子,关于dll工程的创建,请参考博客里另一篇文章:http://www.cnblogs.com/pingge/articles/3153571.html ...

  2. 【1】基于OpenCV的DLL动态库隐式连接

    1DLL的作用 DLL是一个包含可由多个程序同时使用的代码和数据的库.例如:在Windows操作系统中,Comdlg32 DLL执行与对话框有关的常见函数.因此,每个程序都可以使用该DLL中包含的功能 ...

  3. DLL导出函数和类 之 __declspec(dllexport)

    可利用__declspec(dllexport)导出函数或类. 若要指定C类型约定导出,则需在前面加extern “C”. 若要导出函数,__declspec(dllexport) 关键字必须出现在调 ...

  4. VC DLL方法的__declspec导入导出

    https://msdn.microsoft.com/zh-cn/library/a90k134d.aspx https://msdn.microsoft.com/zh-cn/library/ms23 ...

  5. DLL进一步讲解:extern "C" __declspec(dllexport)

    一.__declspec(dllexport): 将一个函数声名为导出函数,就是说这个函数要被其他程序调用,即作为DLL的一个对外函数接口. 通常它和extern    "C"   ...

  6. 动态链接库(dll) __declspec(dllimport) __declspec(dllexport)

    一. __declspec(dllexport) Microsoft 在 Visual C++ 的 16 位编译器版本中引入了 __export,使编译器得以自动生成导出名并将它们放到一个 .lib ...

  7. __declspec(dllexport)和__declspec(dllimport) (——declspec方法创建dll的方法已验证ok)

    转载:https://www.cnblogs.com/chengbing2011/p/4084125.html __declspec(dllimport)和__declspec(dllexport)经 ...

  8. __declspec(dllimport)与__declspec(dllexport)作用总结

    参考自:http://bbs.csdn.net/topics/330169671 __declspec(dllexport):导出符号,也就是定义需要导出函数的dll中给导出函数的函数声明前面加上导出 ...

  9. __declspec(dllexport) & __declspec(dllimport)

    __declspec(dllexport) 声明一个导出函数,是说这个函数要从本DLL导出.我要给别人用.一般用于dll中 省掉在DEF文件中手工定义导出哪些函数的一个方法.当然,如果你的DLL里全是 ...

随机推荐

  1. VelocityTracker简单介绍

    翻译自:http://developer.android.com/reference/android/view/VelocityTracker.html 參照自: http://blog.jrj.co ...

  2. <微软的软件测试之道>读书笔记3

    一.自动化的标准步骤: 1.环境初始化,并检查环境是否处于正确的状态,能否开始测试 2.执行步骤 3.判断结果,并将结果保存到其它地方以供检查分析 4.环境清理,清理本用例产生的垃圾(临时文件.环境变 ...

  3. [Javascript] Array methods in depth - slice

    Array slice creates a shallow copy of an array. In this lesson we cover, in detail, exactly what a ' ...

  4. 【解决】hive动态添加partitions不能超过100的问题

    Author: kwu [解决]hive动态添加partitions不能超过100的问题,全量动态生成partitions超过100会出现例如以下异常: The maximum number of d ...

  5. Linux 字符集

    摘抄自网络--/etc/sysconfig/i18n 文件:LANG="zh_CN.GB18030"SUPPORTED="zh_CN.GB18030:zh_CN:zh:e ...

  6. css3 3D变换和动画

    3D变换和动画 建立3D空间,transform-style: preserve-3d perspective: 100px; 景深 perspective-origin:center center ...

  7. mongodb的java客户端的设计思想

    链接见http://api.mongodb.org/java/current/?_ga=1.111551751.200271495.1409034486 整体结构分为

  8. Activity的学习

    安卓的四大组件分别是 Activity ,Service服务, BroadcastReceiver广播接收器,ContentProvide内容提供器 . Activity: Activity是应用程序 ...

  9. String or binary data would be truncated

    在使用Typed Dataset进行数据的插入时,会报这样的错:String or binary data would be truncated. 我碰到的原因是 数据库中字段的长度过段,插入时内容被 ...

  10. iOS9中请求出现App Transport Security has blocked a cleartext HTTP (http://)

    错误描述: App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecu ...