|
1.获取计算机硬件信息
获取本地计算机的一些基本信息,这样来构成每个机器码的唯一性。
// 获取计算机的CPU的ID
QString HardwareInfo::getCPUId()
{
QString strCpuId = "";
unsigned int dwBuf[4] = { 0 };
unsigned long long ret = 0;
__cpuid((int*)(void*)dwBuf, 1);
ret = dwBuf[3];
ret = ret << 32;
QString str0 = QString::number(dwBuf[3], 16).toUpper();
QString str0_1 = str0.rightJustified(8, '0');
QString str1 = QString::number(dwBuf[0], 16).toUpper();
QString str1_1 = str1.rightJustified(8, '0');
strCpuId = str0_1 + str1_1;
return strCpuId;
}
// 获取计算机的MAC地址
QString HardwareInfo::getHostMacAddress()
{
// 获取所有网络接口列表
QList<QNetworkInterface> nets = QNetworkInterface::allInterfaces();
int nCnt = nets.count();
QString strMacAddr = "";
for (int i = 0; i < nCnt; i++)
{
// 如果此网络接口被激活并且正在运行并且不是回环地址,则就是我们需要找的Mac地址
if (nets[i].flags().testFlag(QNetworkInterface::IsUp) && nets[i].flags().testFlag(QNetworkInterface::IsRunning) && !nets[i].flags().testFlag(QNetworkInterface::IsLoopBack))
{
strMacAddr = nets[i].hardwareAddress();
break;
}
}
return strMacAddr;
}
// 获取计算机的IP地址
QString HardwareInfo::getHostIpAddress()
{
QString strIpAddress;
QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses();
// 获取第一个本主机的IPv4地址
int nListSize = ipAddressesList.size();
for (int i = 0; i < nListSize; ++i)
{
if (ipAddressesList.at(i) != QHostAddress::LocalHost &&
ipAddressesList.at(i).toIPv4Address())
{
strIpAddress = ipAddressesList.at(i).toString();
break;
}
}
// 如果没有找到,则以本地IP地址为IP
if (strIpAddress.isEmpty())
strIpAddress = QHostAddress(QHostAddress::LocalHost).toString();
return strIpAddress;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
2.应用层调用
外部应用层进行逻辑功能的操作
#include "RegisterMachine.h"
#include "Md5Encode.h"
#include "HardwareInfo.h"
#include <string.h>
#include <QClipboard>
#include <QSettings>
#include <QMessageBox>
#include <QInputDialog>
RegisterMachine::RegisterMachine(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
initEvent();
initUI();
}
void RegisterMachine::initEvent()
{
connect(ui.btnMachineCopy, &QPushButton::clicked, this, [&]()
{
QClipboard *clip = QApplication::clipboard();
clip->setText(ui.lineEditMachine->text());
});
connect(ui.btnRegisterCopy, &QPushButton::clicked, this, [&]()
{
QClipboard *clip = QApplication::clipboard();
clip->setText(ui.lineEditRegister->text());
});
connect(ui.btnCreateNumber, &QPushButton::clicked, this, [&]()
{
// 生成注册码
QString strMachineNum = ui.lineEditMachine->text();
bool bForver = ui.checkBoxForever->checkState();
if (strMachineNum.isEmpty())
return;
QDate endDate = ui.dateEdit->date();
QString strDateTime("");
if (bForver)
strDateTime = QString("3022-01-01");// 一千年以后吧
else
strDateTime = endDate.toString("yyyy-MM-dd");
QString strSplit = QString(";");
QString strBefore = strMachineNum + strSplit + strDateTime;
// 加个密转成注册码
QString strAfter = HardwareInfo::crypto(strBefore);
ui.lineEditRegister->setText(strAfter);
});
connect(ui.btnLocalMachineCopy, &QPushButton::clicked, this, [&]()
{
QClipboard *clip = QApplication::clipboard();
clip->setText(ui.lineEditLocalMachine->text());
});
}
void RegisterMachine::initUI()
{
QString strMachineCode = HardwareInfo::createMachineCode();
ui.lineEditLocalMachine->setText(strMachineCode);
ui.dateEdit->setDateTime(QDateTime::currentDateTime());
ui.dateEdit->setCalendarPopup(true);
this->setWindowIcon(QIcon(":/Resource/logo.png"));
}
。
[code]原文链接:https://blog.csdn.net/u013083044/article/details/125774009[/code] |
|