码迷,mamicode.com
首页 > 系统相关 > 详细

QT5 获取mac地址和cpu序列号

时间:2016-01-05 21:00:16      阅读:408      评论:0      收藏:0      [点我收藏+]

标签:

#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
}

QT5 获取mac地址和cpu序列号

标签:

原文地址:http://www.cnblogs.com/LeoVim/p/5103597.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!