windows系统下如何获取唯一硬件识别号(cpuid/mac地址)

版权声明:本文为博主原创文章,自己总结难免有出错或是不周全的地方,望指出...... https://blog.csdn.net/qq849635649/article/details/78190238

这是我的qt工程的配置,需要注意的是:必须添加network模块

QT += core
QT -= gui
QT += network

TARGET = hardware_id

CONFIG += c++11
CONFIG += console
CONFIG -= app_bundle

TEMPLATE = app

SOURCES += main.cpp



这是我的代码部分,已经格式化好了

#include <QCoreApplication>
#include <QStringList>
#include <QString>
#include <qdebug.h>
#include <QNetworkInterface>

#ifdef __GNUC__
    #include <cpuid.h>
#elif defined(_MSC_VER)
    #if _MSC_VER >= 1400
        #include <intrin.h>
    #endif
#else
    #error Only supports MSVC or GCC
#endif

QStringList get_mac();
void getcpuid(unsigned int CPUInfo[4], unsigned int InfoType);
void getcpuidex(unsigned int CPUInfo[4], unsigned int InfoType, unsigned int ECXValue);
QString get_cpuId();

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    //测试cpu_id
    QString cpu_id = "";
    cpu_id = get_cpuId();
    qDebug() << cpu_id;

    //测试mac
    QStringList mac_list;
    mac_list = get_mac();
    for (int i=0; i<mac_list.size(); i++)
    {
        qDebug() << mac_list.at(i);
    }
    return a.exec();
}

//获取机器mac地址
QStringList get_mac()
{
    QStringList mac_list;
    QString strMac;
    QList<QNetworkInterface> ifaces = QNetworkInterface::allInterfaces();
    for (int i=0; i<ifaces.count(); i++)
    {
        QNetworkInterface iface = ifaces.at(i);
        //过滤掉本地回环地址、没有开启的地址
        if (iface.flags().testFlag(QNetworkInterface::IsUp) && !iface.flags().testFlag(QNetworkInterface::IsLoopBack))
        {
            //过滤掉虚拟地址
            if (!(iface.humanReadableName().contains("VMware",Qt::CaseInsensitive)))
            {
                strMac = iface.hardwareAddress();
                mac_list.append(strMac);
            }
        }
    }
    return mac_list;
}

//获取机器cpuId
QString get_cpuId()
{
    QString cpu_id = "";
    unsigned int dwBuf[4];
    unsigned long long ret;
    getcpuid(dwBuf, 1);
    ret = dwBuf[3];
    ret = ret << 32;
    cpu_id = QString::number(dwBuf[3], 16).toUpper();
    cpu_id = cpu_id + QString::number(dwBuf[0], 16).toUpper();
    return cpu_id;
}

void getcpuid(unsigned int CPUInfo[4], unsigned int InfoType)
{
#if defined(__GNUC__)// GCC
    __cpuid(InfoType, CPUInfo[0], CPUInfo[1], CPUInfo[2], CPUInfo[3]);
#elif defined(_MSC_VER)// MSVC
    #if _MSC_VER >= 1400 //VC2005才支持__cpuid
        __cpuid((int*)(void*)CPUInfo, (int)(InfoType));
    #else //其他使用getcpuidex
        getcpuidex(CPUInfo, InfoType, 0);
    #endif
#endif
}

void getcpuidex(unsigned int CPUInfo[4], unsigned int InfoType, unsigned int ECXValue)
{
#if defined(_MSC_VER) // MSVC
    #if defined(_WIN64)	// 64位下不支持内联汇编. 1600: VS2010, 据说VC2008 SP1之后才支持__cpuidex.
        __cpuidex((int*)(void*)CPUInfo, (int)InfoType, (int)ECXValue);
    #else
        if (NULL==CPUInfo)
            return;
        _asm{
            // load. 读取参数到寄存器.
            mov edi, CPUInfo;
            mov eax, InfoType;
            mov ecx, ECXValue;
            // CPUID
            cpuid;
            // save. 将寄存器保存到CPUInfo
            mov	[edi], eax;
            mov	[edi+4], ebx;
            mov	[edi+8], ecx;
            mov	[edi+12], edx;
        }
    #endif
#endif
}

我的代码在qt中试验过了,因为我不是用vs工具,如果看过这篇博客,使用vs工具的麻烦给我个回复

猜你喜欢

转载自blog.csdn.net/qq849635649/article/details/78190238
今日推荐