QT5 get MAC address and CPU serial number

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.