QT(56)-动态链接库-windows-导出变量-导出类
1.导出变量
1.1不使用_declspec(dllimport)+ _declspec(dllexport)
使用_declspec(dllimport)+ _declspec(dllexport)
1.2win32 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);
}