QT5 获取mac地址和cpu序列号

时间:2016-01-05 23:54:58   收藏:0   阅读:1439
  1 #include <QCoreApplication>
  2 #include <QStringList>
  3 #include <QString>
  4 #include <qdebug.h>
  5 #include <QNetworkInterface>
  6 
  7 #ifdef __GNUC__
  8 #include <cpuid.h>
  9 #elif defined(_MSC_VER)
 10 #if _MSC_VER >= 1400
 11 #include <intrin.h>
 12 #endif
 13 #else
 14 #error Only supports MSVC or GCC
 15 #endif
 16 
 17 QStringList get_mac();
 18 void getcpuid(unsigned int CPUInfo[4], unsigned int InfoType);
 19 void getcpuidex(unsigned int CPUInfo[4], unsigned int InfoType, unsigned int ECXValue);
 20 QString get_cpuId();
 21 
 22 int main(int argc, char *argv[])
 23 {
 24     QCoreApplication a(argc, argv);
 25     //测试cpu_id
 26     QString cpu_id = "";
 27     cpu_id = get_cpuId();
 28     qDebug() << cpu_id;
 29     //测试mac
 30     QStringList mac_list;
 31     mac_list = get_mac();
 32     for (int i=0; i<mac_list.size(); i++)
 33     {
 34         qDebug() << mac_list.at(i);
 35     }
 36     return a.exec();
 37 }
 38 
 39 //获取机器mac地址
 40 QStringList get_mac()
 41 {
 42     QStringList mac_list;
 43     QString strMac;
 44     QList<QNetworkInterface> ifaces = QNetworkInterface::allInterfaces();
 45     for (int i=0; i<ifaces.count(); i++)
 46     {
 47         QNetworkInterface iface = ifaces.at(i);
 48         //过滤掉本地回环地址、没有开启的地址
 49         if (iface.flags().testFlag(QNetworkInterface::IsUp) && !iface.flags().testFlag(QNetworkInterface::IsLoopBack))
 50         {
 51             //过滤掉虚拟地址
 52             if (!(iface.humanReadableName().contains("VMware",Qt::CaseInsensitive)))
 53             {
 54                 strMac = iface.hardwareAddress();
 55                 mac_list.append(strMac);
 56             }
 57         }
 58     }
 59     return mac_list;
 60 }
 61 
 62 //获取机器cpuId
 63 QString get_cpuId()
 64 {
 65     QString cpu_id = "";
 66     unsigned int dwBuf[4];
 67     unsigned long long ret;
 68     getcpuid(dwBuf, 1);
 69     ret = dwBuf[3];
 70     ret = ret << 32;
 71     cpu_id = QString::number(dwBuf[3], 16).toUpper();
 72     cpu_id = cpu_id + QString::number(dwBuf[0], 16).toUpper();
 73     return cpu_id;
 74 }
 75 
 76 void getcpuid(unsigned int CPUInfo[4], unsigned int InfoType)
 77 {
 78 #if defined(__GNUC__)// GCC
 79     __cpuid(InfoType, CPUInfo[0], CPUInfo[1], CPUInfo[2], CPUInfo[3]);
 80 #elif defined(_MSC_VER)// MSVC
 81 #if _MSC_VER >= 1400 //VC2005才支持__cpuid
 82     __cpuid((int*)(void*)CPUInfo, (int)(InfoType));
 83 #else //其他使用getcpuidex
 84     getcpuidex(CPUInfo, InfoType, 0);
 85 #endif
 86 #endif
 87 }
 88 
 89 void getcpuidex(unsigned int CPUInfo[4], unsigned int InfoType, unsigned int ECXValue)
 90 {
 91 #if defined(_MSC_VER) // MSVC
 92 #if defined(_WIN64)    // 64位下不支持内联汇编. 1600: VS2010, 据说VC2008 SP1之后才支持__cpuidex.
 93     __cpuidex((int*)(void*)CPUInfo, (int)InfoType, (int)ECXValue);
 94 #else
 95     if (NULL==CPUInfo)    return;
 96     _asm{
 97         // load. 读取参数到寄存器.
 98         mov edi, CPUInfo;
 99         mov eax, InfoType;
100         mov ecx, ECXValue;
101         // CPUID
102         cpuid;
103         // save. 将寄存器保存到CPUInfo
104         mov    [edi], eax;
105         mov    [edi+4], ebx;
106         mov    [edi+8], ecx;
107         mov    [edi+12], edx;
108     }
109 #endif
110 #endif
111 }

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!