在使用QT 和 C++编程过程中,我们来讲讲怎样获取计算机软件和硬件信息,包括计算机名称、cpu、显卡、内存、操作系统、几个屏幕以及每个屏幕的分辨率、硬盘信息、IP、MAC地址、公网IP、以及是否能够联网等信息。
头文件包含
#include
#include
#include
#include
#include
#include
#include "QSettings"
#include "QDebug"
#include
#include
#include
#include
#include
#include
1.获取计算机名称
const QString Info::localmachineName()
{
QString machineName = QHostInfo::localHostName();
return machineName;
}
2.本地IP地址
const QString Info::ip()
{
QString ip="";
QList interFaceList = QNetworkInterface::allInterfaces();
for(int i=0; i< interFaceList.size(); i++)
{
QNetworkInterface interface = interFaceList.at(i);
if(interface.flags().testFlag(QNetworkInterface::IsRunning))
{
QList entryList = interface.addressEntries();
foreach(QNetworkAddressEntry entry, entryList)
{
if(QAbstractSocket::IPv4Protocol == entry.ip().protocol() &&
entry.ip() != QHostAddress::LocalHost && entry.ip().toString().startsWith("192.168."))
{
ip = entry.ip().toString();
break;
}
}
}
}
return ip;
}
3.公网IP
const QString Info::publicIp()
{
QString strIp;
QString webCode = getHtml("http://whois.pconline.com.cn/");
if(!webCode.isEmpty())
{
QString web = webCode.replace(" ", "");
web = web.replace("\r", "");
web = web.replace("\n", "");
QStringList list = web.split("
");
QString tar = list[3];
QStringList ip = tar.split("=");
strIp = ip[1];
}
else
strIp="无法获取公网ip";
return strIp;
}
4.获取计算机MAC地址
const QString Info::mac()
{
QString strMac;
QList netList = QNetworkInterface::allInterfaces();
foreach(QNetworkInterface item, netList)
{
if((QNetworkInterface::IsUp & item.flags()) && (QNetworkInterface::IsRunning & item.flags()))
{
if(strMac.isEmpty() || strMac < item.hardwareAddress())
{
strMac = item.hardwareAddress();
}
}
}
return strMac;
}
5.获取计算机CPU信息
const QString& Info::cpuType()
{
QSettings *CPU = new QSettings("HKEY_LOCAL_MACHINE\\HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0",QSettings::NativeFormat);
m_cpuDescribe = CPU->value("ProcessorNameString").toString();
delete CPU;
return m_cpuDescribe;
}
6.获取计算机显卡信息
const QString Info::dCard()
{
QString dcard;
QSettings *DCard = new QSettings("HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\services\\nvlddmkm\\Device0",QSettings::NativeFormat);
QString type = DCard->value("Device Description").toString();
delete DCard;
QString dType = type;
dType.trimmed();
if(!dType.isEmpty())
dcard = dType;
DCard = new QSettings("HKEY_LOCAL_MACHINE\\SYSTEM\\ControlSet001\\Services\\igfx\\Device0",QSettings::NativeFormat);
type = DCard->value("Device Description").toString();
delete DCard;
dType = type;
dType.trimmed();
if(!dType.isEmpty())
dcard = dcard + "\n" +dType;
DCard = new QSettings("HKEY_LOCAL_MACHINE\\SYSTEM\\ControlSet001\\Services\\amdkmdap\\Device0",QSettings::NativeFormat);
type = DCard->value("Device Description").toString();
delete DCard;
dType = type;
dType.trimmed();
if(!dType.isEmpty())
dcard = dcard + "\n" +dType;
dcard.trimmed();
return dcard;
}
7.获取计算机内存
#include
const QString& Info::memory()
{
MEMORYSTATUSEX statex;
statex.dwLength = sizeof (statex);
GlobalMemoryStatusEx(&statex);
m_totalMem = statex.ullTotalPhys * 1.0/ GB;
m_freeMem = statex.ullAvailPhys * 1.0 / GB;
m_memDescribe = QString("可用 %1 GB / 共 %2 GB" ).
arg(QString::asprintf("%.2f", m_freeMem)).arg(QString::asprintf("%.2f", m_totalMem));
return m_memDescribe;
}
8.获取计算机操作系统
const QString& Info::osVersion()
{
typedef BOOL (WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);
LPFN_ISWOW64PROCESS fnIsWow64Process;
BOOL bIsWow64 = FALSE;
fnIsWow64Process = (LPFN_ISWOW64PROCESS)GetProcAddress( GetModuleHandle(TEXT("kernel32")),"IsWow64Process");
if (NULL != fnIsWow64Process)
{
fnIsWow64Process(GetCurrentProcess(),&bIsWow64);
}
QString sysBit = "unknown";
if(bIsWow64)
sysBit = "64位";
else
sysBit = "32位";
m_osDescirbe = QSysInfo::prettyProductName() + " " + sysBit;
return m_osDescirbe;
}
9.获取计算机屏幕个数以及像素
const QString& Info::screen()
{
m_screenDescribe = "";
QList screenSizeList;
QList screenCountList;
for(int i=0; iscreenCount(); i++)
{
QRect screenRect = QApplication::desktop()->screenGeometry(i);
QSize size(screenRect.width(), screenRect.height());
bool bExist = false;
for(int j=0; j m_maxScreenSize.width() && height > m_maxScreenSize.height())
m_maxScreenSize = screenSizeList.at(i);
m_screenDescribe += QString ("(%1像素 x %2像素) x %3个").arg(width).arg(height).arg(screenCountList.at(i));
if(i!= screenSizeList.length()-1)
m_screenDescribe += "、 ";
}
return m_screenDescribe;
}
10.获取计算机硬盘信息
const QString& Info::disk()
{
m_diskDescribe = "";
QFileInfoList list = QDir::drives();
foreach (QFileInfo dir, list)
{
QString dirName = dir.absolutePath();
dirName.remove("/");
LPCWSTR lpcwstrDriver = (LPCWSTR)dirName.utf16();
ULARGE_INTEGER liFreeBytesAvailable, liTotalBytes, liTotalFreeBytes;
if(GetDiskFreeSpaceEx(lpcwstrDriver, &liFreeBytesAvailable, &liTotalBytes, &liTotalFreeBytes) )
{
QString free = QString::number((double) liTotalFreeBytes.QuadPart / GB, 'f', 1);
free += "G";
QString all = QString::number((double) liTotalBytes.QuadPart / GB, 'f', 1);
all += "G";
QString str = QString("%1 %2/%3 ").arg(dirName, free, all);
m_diskDescribe += str;
double freeMem = (double) liTotalFreeBytes.QuadPart / GB;
if(freeMem > m_maxFreeDisk)
m_maxFreeDisk = freeMem;
}
}
return m_diskDescribe;
}
11.获取计算机安装的office版本
void Info::checkOffice(QString &describe)
{
QSettings settings("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\excel.exe", QSettings::NativeFormat);
QString path = settings.value("Path").toString();
if(!path.isEmpty())
{
QSettings versionSettings("HKEY_LOCAL_MACHINE\\SOFTWARE\\Classes\\Excel.Application\\CurVer", QSettings::NativeFormat);
QString version = versionSettings.value(".").toString();
if(!version.isEmpty())
{
version = version.split(".").last();
int versionNum = version.toInt();
switch (versionNum)
{
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
m_officeDecsribe = "Office 95以前版本";
break;
case 6:
m_officeDecsribe = "Office 95";
break;
case 8:
m_officeDecsribe = "Office 97";
break;
case 9:
m_officeDecsribe = "Office 2000";
break;
case 10:
m_officeDecsribe = "Office XP";
break;
case 11:
m_officeDecsribe = "Office 2003";
break;
case 12:
m_officeDecsribe = "Office 2007";
break;
case 13:
case 14:
m_officeDecsribe = "Office 2010";
break;
case 15:
m_officeDecsribe = "Office 2013";
break;
case 16:
case 17:
case 18:
case 19:
m_officeDecsribe = "Office 2013以后版本";
break;
default:
break;
}
if(versionNum >= 13)//office 2010
{
describe = "已安装Office 版本:" + m_officeDecsribe;
}
else
{
describe = "建议安装Microsoft Offce2010以上版本。";
}
}
else
{
describe = "建议安装Microsoft Offce2010以上版本。";
}
}
else
{
describe = "建议安装Microsoft Offce2010以上版本。";
}
}
12.判断计算机是否可以联网
bool Info::ipLive()
{
//能接通百度IP说明可以通外网
QTcpSocket tcpClient;
tcpClient.abort();
tcpClient.connectToHost("202.108.22.5", 80);
//100毫秒没有连接上则判断不在线
return tcpClient.waitForConnected(100);
}
13.获取网页所有源码
//获取网页所有源代码
QString Info::getHtml(QString url)
{
QNetworkAccessManager *manager = new QNetworkAccessManager();
QNetworkReply *reply = manager->get(QNetworkRequest(QUrl(url)));
QByteArray responseData;
QEventLoop eventLoop;
QTimer *timer = new QTimer(this);
timer->setSingleShot(true);
connect(manager, SIGNAL(finished(QNetworkReply *)), &eventLoop, SLOT(quit()));
connect(timer, SIGNAL(timeout()), &eventLoop, SLOT(quit()));
timer->start(300);
eventLoop.exec();
responseData = reply->readAll();
delete timer;
return QString(responseData);
}
搞定,我做了个demo想要看源码的可以去看看https://download.csdn.net/download/xiezhongyuan07/10402206