当前位置: 首页 > news >正文

QT(56)-动态链接库-windows-导出变量-导出类

 1.导出变量
     1.1不使用_declspec(dllimport)+ _declspec(dllexport)
     
      使用_declspec(dllimport)+ _declspec(dllexport)
      1.2
win32 mydll+win32 myexe
      1.3win32 mydll+qt myexe
 2.导出类
   使用_declspec(dllimport)+ _declspec(dllexport)
   
2.1不用关键字
    2.2 用关键字win32 mydll+ QT myexe
    2.3 用关键字QT mydll+ QT myexe


1.导出变量( 全局变量+ 静态变量)
   方式:.def +dll
   1.1不使用_declspec(dllimport)+ _declspec(dllexport) 

     1.111mydll 编写 项目创建: DLL
     1.112win32 mydll.cpp

int gdllvar=888;

   1.113win32 mydll.def

LIBRARY
EXPORTS
gdllvar CONSTANT

    1.121myexe编写
     1.1211配置dll,lib,h
   配置dll路径:链接器->附加库目录

配置lib文件:链接器->输入

 1.1212myexe.cpp编写
   取的是DLL中变量的地址,而非值。
   不能 gdllvar=100; 这样修改的是地址。

extern int gdllvar; //DLL中的全局变量的地址
int main()
{int myvar=gdllvar; //调用DLL中的全局变量 不行int myvar= *(int *)gdllvar;
}

   1.12 使用 _declspec(dllimport)+ _declspec(dllexport)
       取的是DLL中变量,改的也是值。
      myexe.cpp

# pragma comment(lib,"..\\Debug\\mydll.lib")
extern int _declspec(dllimport) gdllvar;
int main()
{int myvar =gdllvar;   //这就是对原dll文件中的值进行操作。
}

  1.2win32 mydll+win32 myexe
     1.211 win32 mydll 新建项目设置为DLL
     
1.212 win32 mydll h文件  

#ifdef  INDLL
#define  SPEC  _declspec(dllexport)
#else
#define  SPEC  _declspec(dllimport)
#endif 
extern  "C"
{SPEC  extern int gdllvar1;  //声明要导出的全局变量SPEC  extern  int gdllvar2;  //声明要导出的全局变量
}

 1.213 win32 mydll cpp文件

#include "stdafx.h"
#ifndef INDLL
#define INDLL
#endif#include "mydll.h"
int gdllvar1=88,gdllvar2=99;

1.22 win32 myexe cpp文件

# include "../mydll/mydll.h"
#pragma comment(lib,"../debug/mydll.lib")int main()
{
int myvar1=gdllvar1;
int myvar2=gdllvar2;
}

1.3 win32 mydll+qt myexe
   
1.31 win32 mydll
        1.311  win32 mydll h文件
        1.312  win32 mydll cpp文件
    1.32 qt myexe
        1.321 qt myexe win32API函数调用       

void Dialog::on_pushButton_clicked()
{DWORD dw;HINSTANCE hDLL; // 1.Handle to DLLint n;int *pgdllvar; // Function pointerQString str;hDLL = ::LoadLibrary(L"mydll.dll"); //注意有个Lif (hDLL){pgdllvar = (int*)GetProcAddress(hDLL,"gdllvar");//得到导出变量的实际地址if (!pgdllvar){// 处理错误情况dw = ::GetLastError();FreeLibrary(hDLL);str.sprintf("GetProcAddress failed:%d",dw);QMessageBox::information(this,"Error code",str);}else{n = *pgdllvar;//存放到整型变量n中str.sprintf("%d",n);QMessageBox::information(this,"result",str);FreeLibrary(hDLL); //释放句柄}}else{dw = ::GetLastError();str.sprintf("Load dll failed:%d",dw);QMessageBox::information(this,"Error",str);}
}

   1.322 qt myexe QLibrary函数调用

void Dialog::on_pushButton_clicked()
{int n,*pn;QString str;QLibrary lib("Test");if(lib.load()){pn = (int *)lib.resolve("gdllvar");if (!pn){// 处理错误情况QMessageBox::information(this,"Error","resolve failed");}else{n=*pn;str.sprintf("%d",n);QMessageBox::information(this,"result",str);}}else  QMessageBox::information(this,"Error","load failed");
}

1.4  win32 mydll
       1.411新建项目DLL
       1.412 mydll.h         

#pragma once
#ifdef  INDLL
#define  SPEC  _declspec(dllexport) 
#else
#define  SPEC  _declspec(dllimport)
#endif class SPEC CMath
{
public:CMath();virtual ~CMath();
public:static double  PI; //定义一个类静态变量
};

   1.413mydll.cpp

#include "stdafx.h"#define INDLL //这样定义后,Test.h中的SPEC为_declspec(dllexport)
#include "mydll.h"CMath::CMath() {}
CMath::~CMath() {};double CMath::PI = 3.14; //对类静态变量赋值

   1.5win32 myexe
    1.51 myexe.cpp

#include "pch.h"
#include <stdio.h>
#include "../mydll/mydll.h"
#pragma comment(lib,"../debug/mydll.lib")
int main()
{printf("%f\n", ++CMath::PI); //先让类静态变量自加,然后打印结果return 0;
}

2.导出类  用关键字和不用关键字两种

2.1不用关键字:
     设置:项目->属性->生成MAP文件
     生成lib,dll,map  
map中类函数

  

win32 myexe.cpp

#include "stdafx.h"
#include "../mydll/mydll.h"
#pragma comment(lib,"../debug/mydll.lib")
int _tmain(int argc, _TCHAR* argv[])
{CMath math;printf("%d,%d\n", math.Add(10, 8), math.sub(20,3));return 0;
}

 2.2用关键字 win32 mydll+ QT myexe
   
2.211 win32:mydll.h
    
2.212 win32 :mydll.cpp

#include "stdafx.h"
#include "Test.h"int CMath::Add(int a, int b)
{return a + b;
}
int CMath::sub(int a, int b)
{return a - b;
}
CMath::CMath(){}
CMath::~CMath(){}

  2.213 QT :myexe.cpp

class  CMath
{
public:int Add(int a, int b);int sub(int a, int b);~CMath();CMath();
};
void Dialog::on_pushButton_clicked()
{CMath math;QString str;int c= math.Add(2,3);str.sprintf("%d",c);QMessageBox::information(this,"result",str);
}

 2.214 QT  :myexe.pro

LIBS += -L$$PWD/ ./ -lmydll

2.3用关键字 QT mydll+ QT myexe
   
创建项目 C++库:

   2.31QT mydll.h

#ifndef MYDLL_H
#define MYDLL_H#include "mydll_global.h"
extern "C"
{MYDLL_EXPORT int add(int a ,int b);  //宏定义表示是导出函数
}class MYDLL_EXPORT Mydll
{
public:Mydll();
};#endif // MYDLL_H


   2.32QT mydll.cpp
 

#include "mydll.h"Mydll::Mydll()
{
}
int add(int a,int b)
{return a+b;
}




   2.33 QT myexe.h
   2.33 QT myexe.cpp

 

#include "dialog.h"
#include "ui_dialog.h"
#include <QMessageBox>extern "C"
{int add(int a ,int b);
}Dialog::Dialog(QWidget *parent) :QDialog(parent),ui(new Ui::Dialog)
{ui->setupUi(this);
}Dialog::~Dialog()
{delete ui;
}void Dialog::on_pushButton_clicked()
{QString str;int sum = add(2,3);str.sprintf("sum=%d",sum);QMessageBox::information(this,"rsult",str);
}


  

 

 

 

http://www.lryc.cn/news/6258.html

相关文章:

  • TCP传输文件
  • vue3:加载本地图片等静态资源
  • 工作记录------数据库group_concat函数长度问题
  • Python基础语法
  • windows环境下安装Nginx及常用操作命令
  • python excel数据处理?
  • Hudi-集成Flink
  • 重新认识 React Hooks useContext
  • 数据库(2)--加深对统计查询的理解,熟练使用聚合函数
  • stm32f407探索者开发板(十五)——NVIC中断优先级管理
  • 【Azure 架构师学习笔记】-Azure Logic Apps(6)- Logic Apps调用ADF
  • python随机获取列表中某一元素
  • Nacos微服务笔记
  • MAC文件误删怎么办?mac数据恢复,亲测很好用的方法
  • 机械革命z2黑苹果改造计划第二番-MacOS实用软件渗透工具
  • 【LeetCode】每日一题(4)
  • Linux内核移植:内核的启动过程分析、启动配置与rootfs必要文件
  • 【代码随想录训练营】【Day14】第六章|二叉树|理论基础|递归遍历|迭代遍历|统一迭代
  • AXI-Stream 学习笔记
  • 【Linux】程序进程地址空间
  • 电压放大器在液滴微流控芯片的功能研究中的应用
  • Linux操作系统学习(进程地址空间)
  • 【排序】快速排序实现
  • YOLOv5/v7 Flask Web 车牌识别 | YOLOv7 + EasyOCR 实现车牌识别
  • 【Opencv实战】几十年前的Vlog火了:黑白老照片如何上色?这黑科技操作一定要知道,复原度超高,竟美的出奇~(图像修复神级代码)
  • React源码分析(一)Fiber
  • 小樽 C++指针—— (壹) 指针变量
  • java 代码块 万字详解
  • 杂项-图片隐写
  • 【高性价比】初学者入门吉他值得推荐购买的民谣单板吉他品牌—VEAZEN费森吉他