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 #elifDefined (_msc_ver)Ten #if_msc_ver >= 1400 One#include <intrin.h> A #endif - #else - #erroronly supports MSVC or GCC the #endif - - qstringlist Get_mac (); - voidGetcpuid (unsignedintcpuinfo[4], unsignedintinfotype); + voidGetcpuidex (unsignedintcpuinfo[4], unsignedintInfoType, unsignedintecxvalue); - QString get_cpuid (); + A intMainintargcChar*argv[]) at { - qcoreapplication A (argc, argv); - //Test cpu_id -QString cpu_id =""; -cpu_id =Get_cpuid (); -Qdebug () <<cpu_id; in //Test your Mac - qstringlist mac_list; toMac_list =Get_mac (); + for(intI=0; I<mac_list.size (); i++) - { theQdebug () <<mac_list.at (i); * } $ returna.exec ();Panax Notoginseng } - the //Get machine MAC address + qstringlist Get_mac () A { the qstringlist mac_list; + QString Strmac; -Qlist<qnetworkinterface> ifaces =qnetworkinterface::allinterfaces (); $ for(intI=0; I<ifaces.count (); i++) $ { -Qnetworkinterface Iface =ifaces.at (i); - //filter out the local loopback address, no open address the if(Iface.flags (). Testflag (qnetworkinterface::isup) &&!iface.flags (). Testflag (qnetworkinterface::isloopback)) - {Wuyi //filter out virtual addresses the if(! (Iface.humanreadablename (). Contains ("VMware", qt::caseinsensitive ))) - { WuStrmac =iface.hardwareaddress (); - mac_list.append (STRMAC); About } $ } - } - returnmac_list; - } A + //get the machine cpuid the QString get_cpuid () - { $QString cpu_id =""; theUnsignedintdwbuf[4]; theUnsignedLong Longret; theGetcpuid (Dwbuf,1); theRET = dwbuf[3]; -RET = RET << +; incpu_id = Qstring::number (dwbuf[3], -). ToUpper (); thecpu_id = cpu_id + qstring::number (dwbuf[0], -). ToUpper (); the returncpu_id; About } the the voidGetcpuid (unsignedintcpuinfo[4], unsignedintInfoType) the { + #ifDefined (__gnuc__)//GCC -__cpuid (InfoType, cpuinfo[0], cpuinfo[1], cpuinfo[2], cpuinfo[3]); the #elifDefined (_msc_ver)//MSVCBayi #if_msc_ver >= 1400//VC2005 only supported __cpuid the__cpuid ((int*)(void*) CPUInfo, (int) (InfoType)); the #else //Other use Getcpuidex -Getcpuidex (CPUInfo, InfoType,0); - #endif the #endif the } the the voidGetcpuidex (unsignedintcpuinfo[4], unsignedintInfoType, unsignedintecxvalue) - { the #ifDefined (_msc_ver)//MSVC the #ifDefined (_win64)//Inline assembly is not supported under 64-bit. 1600:vs2010, it is said that VC2008 SP1 after the support of __cpuidex. the__cpuidex ((int*)(void*) CPUInfo, (int) InfoType, (int) ecxvalue);94 #else the if(Null==cpuinfo)return; the _asm{ the //load. Read the parameters to the register.98 mov edi, CPUInfo; About mov eax, infotype; - mov ecx, ecxvalue;101 //CPUID102 Cpuid;103 //save. Saving registers to Cpuinfo104 mov [edi], eax; themov [edi+4], ebx;106mov [edi+8], ecx;107mov [edi+ A], edx;108 }109 #endif the #endif111}
QT5 get MAC address and CPU serial number