赞
踩
1、编译环境:
本程序使用的编译环境是QT5.12.0和VS2017,如果你安装环境不用,你可以通过阅读里面的程序代码,进行代码有效的提取,然后你再自己创建工程进行编译使用。
2、基本描述:
通过获取计算机的硬件信息,然后通过加密组成,生成每个计算机的唯一机器码,类似的方式,按照内部定义的规则,生成相应的组册码。
获取本地计算机的一些基本信息,这样来构成每个机器码的唯一性。
// 获取计算机的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;
}
外部应用层进行逻辑功能的操作
#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"));
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。