打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Qt: 生成最简单的dll示例-搜狗网页
########################### 生成DLL的工程: ####################### 
修改pro文件: TEMPLATE=lib 
########################### .h文件 ####################### 
#ifndef DLLTEST_H 
#define DLLTEST_H 
#ifdef Q_WS_WIN 
#define MY_EXPORT __declspec(dllexport) 
#else 
#define MY_EXPORT 
#endif 
class DllTest { 
public: 
  DllTest();
  int getAge() {
  return 10;
  }
}; 
extern 'C' MY_EXPORT int add(int a, int b) { 
  return a + b;

extern 'C' MY_EXPORT DllTest* getDllTest(); // 使用类 
#endif // DLLTEST_H 
########################### .cpp文件 ####################### 
#include 'dlltest.h' 
#include <qDebug> 
DllTest::DllTest() { 
  qDebug() << 'Shared Dll Test';

DllTest* getDllTest() { 
  return new DllTest();

// 如果是C++编译的函数, 要使用extern 'C'来包装成C函数(导出函数, 给外部提供服务). 
########################### 使用DLL的工程: ####################### 
pro文件中加入: LIBS += 'DllTest.dll' 
########################### 测试.cpp文件 ####################### 
#include 'dlltest.h' 
#include <QLibrary> 
#include <qDebug> 
#include <QApplication> 
typedef int (*AddFunc)(int, int); 
typedef DllTest* (*GetFunc)(); 
int main(int argc, char* argv[]) { 
  QApplication app(argc, argv, false);
  QLibrary lib('DllTest');
  if (lib.load()) {
  qDebug() << 'Load dll successfully.';
  AddFunc func = (AddFunc)lib.resolve('add');
  if (func) {
  qDebug() << func(1, 3);
  }
  GetFunc g = (GetFunc)lib.resolve('getDllTest');
  if (g) {
  DllTest *t = g(); // 使用DLL中的类
  qDebug() << t->getAge();
  delete t;
  }
  } else {
  qDebug() << 'Load dll Failed';
  }
  return app.exec();
}[喝小酒的网摘]http://blog.const.net.cn/a/10954.htm
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
生成dll文件以及python对DLL中函数的调用(参数类型以及返回值)
VC 动态链接库(DLL)编程深入浅出
VC知识库文章 - DLL初学者指南(非MFC)
VC dll的制作和使用
静态链接库与动态链接库导出函数详解(本文系转载)
将 动态链接库文件添加到VC程序中
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服