利用Qt实现获取计算机的硬件信息
码农飞飞 人气:0获取的计算机信息包括:
1.计算机的名称
2.所有的IP地址
3.计算机的当前时间
4.CPU型号
5.显卡型号(win10不兼容)
6.内存大小
7.CPU的核数
10.获得显卡的内存(win10不兼容)
#include <QtCore/QCoreApplication> #include <Windows.h> #include <QtCore\QTime> #include <QtCore\qsettings.h> #include <dxgi.h> #include <QtNetwork\qhostinfo.h> #include <QtCore\qthread.h> //需要依赖dxgi.lib库文件 int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); //获得计算机的名称 QString hostName = QHostInfo::localHostName(); //获得所有的IP地址 QHostInfo hostInfo = QHostInfo::fromName(QHostInfo::localHostName()); QList<QHostAddress> listAdress = hostInfo.addresses(); QStringList ipv4List; for(int index=0; index<listAdress.size(); ++index) { if(listAdress.at(index).protocol() == QAbstractSocket::IPv4Protocol) { ipv4List.push_back(listAdress.at(index).toString()); } } //获得计算机的当前时间 QTime::currentTime().toString("hh::mm::ss"); //获得CPU型号 QSettings* cpu = new QSettings("HKEY_LOCAL_MACHINE\\HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0", QSettings::NativeFormat); QString cpuDescribe = cpu->value("ProcessorNameString").toString(); delete cpu; //获得显卡型号(win10不兼容) QSettings *videoCard = new QSettings("HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\nvlddmkm\\Device0",QSettings::NativeFormat); QString videoCardType = videoCard->value("Device Description").toString(); delete videoCard; //获得内存大小 MEMORYSTATUSEX statex; statex.dwLength = sizeof(statex); GlobalMemoryStatusEx(&statex); unsigned long memory_size = statex.ullTotalPhys; int memory_size_MB = static_cast<double>(statex.ullTotalPhys)/static_cast<double>(1024*1024); //获得CPU的核数 QThread::idealThreadCount(); //获得显卡的内存(win10不兼容) IDXGIFactory* pFactory; IDXGIAdapter* pAdapter; std::vector<IDXGIAdapter*> vAdapters; unsigned int total_Memory = 0; int iAdapterNum = 0; HRESULT hr = CreateDXGIFactory(__uuidof(IDXGIFactory),(void**)(&pFactory)); if(FAILED(hr)) return -1; while(pFactory->EnumAdapters(iAdapterNum, &pAdapter) != DXGI_ERROR_NOT_FOUND) { vAdapters.push_back(pAdapter); ++iAdapterNum; } for(size_t i = 0; i<vAdapters.size(); ++i) { DXGI_ADAPTER_DESC adpterDesc; vAdapters[i]->GetDesc(&adpterDesc); total_Memory += adpterDesc.SharedSystemMemory; } int destInfo = total_Memory /1024/1024; return a.exec(); }
补充
当然利用QT还可以获取一些其他信息,下面是小编为大家整理的一些方法,需要的可以参考一下
获取MAC地址
// 获取MAC地址 QString Registration::GetMac() { // 获取所有网络接口列表 QList<QNetworkInterface> nets = QNetworkInterface::allInterfaces(); // 遍历接口 int nCnt = nets.count(); QString strMacAddr = ""; for(int i = 0; i < nCnt; i++) { // 网络接口需被激活且正在运行且不是回环地址,才可能是要找的地址 if(nets[i].flags().testFlag(QNetworkInterface::IsUp) && nets[i].flags().testFlag(QNetworkInterface::IsRunning) && !nets[i].flags().testFlag(QNetworkInterface::IsLoopBack)) { // 该mac的ip地址不能是回环地址并且是ipv4地址 for(int j = 0; j < nets[i].addressEntries().size(); j++) { if(nets[i].addressEntries().at(j).ip() != QHostAddress::LocalHost && nets[i].addressEntries().at(j).ip().protocol() == QAbstractSocket::IPv4Protocol) { strMacAddr = nets[i].hardwareAddress(); } } } } return strMacAddr; }
获取处理器ID
通过wmic指令获取。
// 获取CPU处理器ID QString Registration::GetCPUProcessorid() { QString cmd = QString("wmic cpu get processorid"); return GetWMICInfo(cmd); }
获取主板序列号
通过wmic指令获取。
// 获取主板序列号 QString Registration::GetBaseboardSerialnumber() { QString cmd = QString("wmic baseboard get serialnumber"); return GetWMICInfo(cmd); }
获取BIOS序列号
通过wmic指令获取。
// 获取BIOS序列号 QString Registration::GetBIOSSerialnumber() { QString cmd = QString("wmic bios get serialnumber"); return GetWMICInfo(cmd); }
加载全部内容