创建DLL详解(2)
来源:互联网 发布:苹果6韩版支持什么网络 编辑:程序博客网 时间:2024/06/16 16:36
上一篇中我们讲到DLL导出函数的使用,这一篇我们讲下DLL导出类的使用
首先创建工程
选择DLL类型
创建工程完成后
这里DLLClass.h是我手动添加的
其实这里导出类的创建也很简单
直接再类的前面加上_declspec(dllexport)
例如class _declspec(dllexport) People
{
public:
People();
~People();
}
DLLClass.h
#ifndef _DLLCLASS_H#define _DLLCLASS_H#ifdef DLLEXPORT#define AMB _declspec(dllexport)#else#define AMB#endif#include <string>#include <iostream>using namespace std;class AMB People{public: People(char* name, int age); ~People(); bool SetName(char* name); char* GetName(); bool SetAge(int age); int GetAge(); void SelfIntroduction();private: char m_name[50]; int m_age;};#endif
DLLClass.cpp
#include "stdafx.h"#define DLLEXPORT#include "DLLClass.h"People::People(char* name, int age){ strcpy_s(m_name, 50, name); m_age = age;}People::~People(){}bool People::SetName(char* name){ strcpy_s(m_name, 50, name); return true;}char* People::GetName(){ return m_name;}bool People::SetAge(int age){ m_age = age; return true;}int People::GetAge(){ return m_age;}void People::SelfIntroduction(){ cout << "I am " << m_name << endl; cout << "I am " << m_age << " years old" << endl;}
好了DLLClass.dll创建完成,我们用个工程来测试下
DLLTest.cpp
#include "stdafx.h"#include "DLLClass.h"#pragma comment(lib,"DLLClass.lib")int _tmain(int argc, _TCHAR* argv[]){ People s("lihua",20); s.SelfIntroduction(); cout << s.GetName() << " " << s.GetAge()<<endl; s.SetName("lilei"); s.SetAge(21); cout << s.GetName() << " " << s.GetAge() << endl; s.SelfIntroduction(); system("pause"); return 0;}
有一点要说明的是也可以像下面这样写
#ifdef DLLEXPORT#define AMB _declspec(dllexport)#else#define AMB _declspec(dllimport)#endif
如果不定义dllimport就无法使用静态成员变量,不过静态变量一般不常用,所以,一般也不写
运行
OK,类可以使用
阅读全文
0 0
- 创建DLL详解(2)
- 创建DLL详解(1)
- 创建DLL详解(3)
- Delphi Dll 创建和使用(2)
- DLL创建与使用方法浅谈(2)
- DLL-创建DLL
- DLL-创建DLL
- 创建DLL
- 静态dll和动态dll(创建、区别、调用方式)
- VS2010中创建DLL和(静态)调用DLL
- VS2010中创建DLL和(动态)调用DLL
- VS2010中创建DLL和(静态)调用DLL
- dll详解
- dll详解
- DLL 详解
- DLL详解
- DLL详解
- DLL详解
- ros海龟例子
- Python平台下调用阿里云API参数实现DDNS功能
- netty(六) 为什么选择netty
- Linux与Shell脚本应用
- 【机器学习】为什么你编程学得越久,就越难入门人工智能?
- 创建DLL详解(2)
- 恶意样本分析手册-虚拟机检测篇(下)
- Ubuntu安装NVIDIA驱动补充
- java并发工具CountDownLatch的理解以及使用
- 2017.08.14工作日记
- 第二章 身份验证 (一) 登录、退出
- poj 3061 Subsequence(尺取)
- 归并排序
- 矩阵在游戏开发中的应用