DLL编程——Win32导出DLL类

1、新建一个Win32 DLL 工程

添加头文件到工程中,编写自定义DLL导出类:

#ifndef DLLCLASS_H_
#define DLLCLASS_H_
#include <iostream>
using namespace std;
#define DLL_EXPORT _declspec(dllexport)
const float PI=3.1415926;//或者C语言的#define PI 3.1415926
inline float GetSquare(const float& n){ return n*n;}
inline float GetCubic(const float& n){ return n*n*n;}
class DLL_EXPORT CBall//DLL封装球
{
public://接口
	CBall(float f=1.0);
	~ CBall();
	void ChangeRadius(const float& value);//改变球的半径
	float GetArea()const;//计算球面积
	float GetVolume()const;//计算球体积
private:
	float m_fRadius;//球半径
};
#endif;

添加源文件到工程中,实现DLL类的功能:

#include "stdafx.h"
#include "DllClass.h"
CBall::CBall(float f):m_fRadius(f)
{
}
CBall::~CBall()
{
}
void CBall::ChangeRadius(const float& value)
{
	m_fRadius=value;
}
float CBall::GetArea() const
{
	return 4*PI*GetSquare(m_fRadius);
}
float CBall::GetVolume() const
{
	return 4/3.0*PI*GetCubic(m_fRadius);
}

2、新建一个win32控制台应用程序:

准备工作:将DLL工程中生成的后缀名为lib的文件复制到的当前工程的CPP\H文件所在文件夹下,将后缀名为dll的文件复制到生成的exe程序的Debug文件夹下

#include "stdafx.h"
#include "DllClass.h"
#pragma comment(lib,"Win32_Dll.lib")//连接到DLL中
using std::cout;
using std::endl;
int _tmain(int argc, _TCHAR* argv[])
{
	CBall b;
	cout<<"球的面积是:"<<b.GetArea()<<endl;;
	cout<<"体积是:"<<b.GetVolume()<<endl;
	return 0;
}

一个很简单的DLL开发小实例,希望可以抛砖引玉


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值