QT5 get MAC address and CPU serial number

Source: Internet
Author: User

#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);
    Test cpu_id
    QString cpu_id = "";
    cpu_id = Get_cpuid ();
    Qdebug () << cpu_id;
    Test your 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 ();
}
Get Machine MAC Address
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);
        Filter out the local loopback address, no open address
        if (Iface.flags (). Testflag (Qnetworkinterface::isup) &&!iface.flags (). Testflag (qnetworkinterface::i Sloopback))
        {
            Filter out virtual addresses
            if (! ( Iface.humanreadablename (). Contains ("VMware", qt::caseinsensitive)))
            {
                Strmac = Iface.hardwareaddress ();
                Mac_list.append (STRMAC);
            }
        }
    }
    return mac_list;
}
Get the Machine 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], +). ToUpper ();
    cpu_id = cpu_id + qstring::number (dwbuf[0], +). 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 support __cpuid
    __cpuid ((int*) (void*) CPUInfo, (int) (infotype));
#else//other use Getcpuidex
    Getcpuidex (CPUInfo, InfoType, 0);
#endif
#endif
}
void Getcpuidex (unsigned int cpuinfo[4], unsigned int infotype, unsigned int ecxvalue)
{
#if defined (_msc_ver)//MSVC
Inline assembly is not supported #if defined (_WIN64)//64-bit. 1600:vs2010, said VC2008 SP1 after the support __cpuidex.
    __cpuidex ((int*) (void*) CPUInfo, (int) infotype, (int) ecxvalue);
#else
    if (null==cpuinfo) return;
    _asm{
        Load. Read the parameter to the register.
        mov edi, CPUInfo;
        mov eax, infotype;
        mov ecx, ecxvalue;
        CPUID
        Cpuid
        Save. Save Register to Cpuinfo
        Mov[edi], eax;
        MOV[EDI+4], ebx;
        Mov[edi+8], ecx;
        MOV[EDI+12], edx;
    }
#endif
#endif
}

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.