Qt - Pro
QT += network
Qt - Class
QCoreApplication
QNetworkInterface
QProcess
代码
1、打印网卡Mac地址 (HardwareAddress)
void MAC()
{
foreach (QNetworkInterface netInterface, QNetworkInterface::allInterfaces())
{
//设备名
qDebug() << "Device:" << netInterface.name();
//MAC地址
qDebug() << "HardwareAddress:" << netInterface.hardwareAddress();
QList<QNetworkAddressEntry> entryList = netInterface.addressEntries();
//遍历每一个IP地址(每个包含一个IP地址,一个子网掩码和一个广播地址)
foreach(QNetworkAddressEntry entry, entryList)
{
//IP地址
qDebug() << "IP Address:" << entry.ip().toString();
//子网掩码
qDebug() << "Netmask:" << entry.netmask().toString();
//广播地址
qDebug() << "Broadcast:" << entry.broadcast().toString();
}
qDebug() << "\n\n";
}
}
2、打印CPU的ID - 纯C
(注意:在release下可能会出错)
/// CpuId
#include <arpa/inet.h>
#include <fstream>
#include <unistd.h>
#include <cstring>
static bool get_cpu_id_by_asm(std::string & cpu_id)
{
cpu_id.clear();
unsigned int s1 = 0;
unsigned int s2 = 0;
asm volatile
(
"movl $0x01, %%eax; \n\t"
"xorl %%edx, %%edx; \n\t"
"cpuid; \n\t"
"movl %%edx, %0; \n\t"
"movl %%eax, %1; \n\t"
: "=m"(s1), "=m"(s2)
);
if (0 == s1 && 0 == s2)
{
return(false);
}
char cpu[32] = {
0 };
snprintf(cpu, sizeof(cpu), "%08X%08X", htonl(s2), htonl(s1));
std::string(cpu).swap(cpu_id);
return(true);
}
static void parse_cpu_id(const char * file_name, const char * match_words, std::string & cpu_id)
{
cpu_id.c_str();
std::ifstream ifs(file_name, std::ios::binary);
if (!ifs.is_open())
{
return;
}
char line[4096] = {
0 };
while (!ifs.eof())
{
ifs.getline(line, sizeof(line));
if (!ifs.good())
{
break;
}
const char * cpu = strstr(line, match_words);
if (NULL == cpu)
{
continue;
}
cpu += strlen(match_words);
while ('\0' != cpu[0])
{
if (' ' != cpu[0])
{
cpu_id.push_back(cpu[0]);
}
++cpu;
}
if (!cpu_id.empty())
{
break;
}
}
ifs.close();
}
static bool get_cpu_id_by_system(std::string & cpu_id)
{
cpu_id.c_str();
const char * dmidecode_result = ".dmidecode_result.txt";
char command[512] = {
0 };
snprintf(command, sizeof(command), "dmidecode -t 4 | grep ID > %s", dmidecode_result);
if (0 == system(command))
{
parse_cpu_id(dmidecode_result, "ID:", cpu_id);
}
unlink(dmidecode_result);
return(!cpu_id.empty());
}
static bool get_cpu_id(std::string & cpu_id)
{
if (get_cpu_id_by_asm(cpu_id))
{
return(true);
}
if (0 == getuid())
{
if (get_cpu_id_by_system(cpu_id))
{
return(true);
}
}
return(false);
}
///
3、打印硬盘的ID
/// 获取硬盘Id
#include <stdio.h>
#include <sys/ioctl.h> //ioctl()的声明头文件
#include <linux/hdreg.h> //硬盘参数头文件, hd_driveid结构声明头文件
#include <sys/fcntl.h> //文件控制头文件
static void getDiskId()
{
qDebug() << endl << "------------------------begin------------------------" << endl;
QProcess process;
process.start("hdparm -i /dev/sda"); // 核心
process.waitForStarted();
process.waitForFinished();
QString str = process.readAllStandardOutput();
int NoPos = str.indexOf("SerialNo=");
int CoPos = str.indexOf("Config=");
if(NoPos != -1 && CoPos != -1)
{
QString nStr = str.mid(NoPos+9,CoPos-NoPos-9);
QString dStr;
for(short index=0;index<nStr.size();++index)
{
char chr = nStr[index].toLatin1();
if((chr >= '0' && chr <= '9') || (chr >= 'A' && chr <= 'Z') || (chr >= 'a' && chr <= 'z'))
{
dStr.append(chr);
}
}
qDebug() << "result:" << dStr << endl;
}
qDebug() << endl << "------------------------end------------------------" << endl;
}
///
运行截图
下载
关注
微信公众号搜索"Qt_io_"或"Qt开发者中心"了解更多关于Qt、C++开发知识.。
笔者 - jxd