C++程序获取系统信息方法

从网上搜集了一些获取系统信息的方法,结合自己的需要进行修改和整理下,特此记录。文件在VS2008下编译通过。

1、SystemInfo.h

#ifndef  _SYSTEM_INFO_H__
#define _SYSTEM_INFO_H__


#include


typedef unsigned long DWORD;


void ExeCPUID(DWORD veax);  //初始化CPU


//CPU 主 频
long GetCPUFreq();       //获取CPU频率,单位: MHZ


//CPU 制造商
String GetManID();   //获取制造商信息


//CPU 型 号
String GetCPUType();




typedef struct CPUInfo
{
String Manufacturer_;
String Type_;
long Freq_;


CPUInfo()
{
Freq_ = 0;
}
}CPUInfo_T;


void GetCPUInfo(CPUInfo_T &ci);




//操作系统版本
typedef struct OSInfo
{
String OSName_;
long bits_;
OSInfo()
{
bits_ = 0;
}
}OSInfo_T;


void SafeGetNativeSystemInfo(__out LPSYSTEM_INFO lpSystemInfo);
int GetSystemBits();
void GetVersionInfo(String &systeminfo);
void GetOSInfo(OSInfo_T & os);


//显卡
void GetDisplayCardInfo(std::vector &ResultStr);


//内存
void GetMemoryInfo(DWORD &dwTotalPhys, DWORD &dwAvailPhys);


//硬盘
typedef struct DiskInfo
{
String DiskName_;
DWORD TotalSize_;
DWORD RestSize_;


DiskInfo()
{
TotalSize_ = 0;
RestSize_ = 0;
}
}DiskInfo_T;


void GetDiskInfo(std::vector &ResultInfo);


typedef struct NetInfo
{
String NetName_;
String NetDesc_;
std::vector NetIP_;
String Mac_;
}NetInfo_T;


void GetNetInfo(std::vector &ResultInfo);


void WriteSystemInfo();


#endif



2、SystemInfo.cpp

#include "stdafx.h"
#include "SystemInfo.h"
#include "AudioDeviceInfo.h"


#include


#define _WIN32_DCOM
#include
#include
#pragma comment(lib, "wbemuuid.lib")


#include
#include
#pragma comment(lib,"Iphlpapi.lib")


DWORD deax;
DWORD debx;
DWORD decx;
DWORD dedx;


void ExeCPUID(DWORD veax)  //初始化CPU
{
__asm
{
mov eax,veax
cpuid
mov deax,eax
mov debx,ebx
mov decx,ecx
mov dedx,edx
}
}


//CPU 主 频
long GetCPUFreq()       //获取CPU频率,单位: MHZ
{
int start,over;
_asm 
{
RDTSC
mov start,eax
}
Sleep(50);
_asm 
{
RDTSC
mov over,eax
}
return (over-start)/50000;
}


//CPU 制造商
String GetManID()   //获取制造商信息
{
char ID[25];        
memset(ID,0,sizeof(ID));


ExeCPUID(0);          //初始化
memcpy(ID+0,&debx,4); //制造商信息复制到数组
memcpy(ID+4,&dedx,4);
memcpy(ID+8,&decx,4);


return String(ID);
}


//CPU 型 号
String GetCPUType()
{
const DWORD id = 0x80000002; //从0x80000002开始,到0x80000004结束
char CPUType[49];//用来存储CPU型号信息
memset(CPUType,0,sizeof(CPUType));//初始化数组


for(DWORD t = 0 ; t < 3 ; t++ )
{
ExeCPUID(id+t);
//每次循环结束,保存信息到数组
memcpy(CPUType+16*t+ 0,&deax,4);
memcpy(CPUType+16*t+ 4,&debx,4);
memcpy(CPUType+16*t+ 8,&decx,4);
memcpy(CPUType+16*t+12,&dedx,4);
}


return String(CPUType);
}


void GetCPUInfo(CPUInfo_T &ci)
{
ci.Manufacturer_ = GetManID();
ci.Type_ = GetCPUType();
ci.Freq_ = GetCPUFreq();
}


void GetDisplayCardInfo(std::vector &ResultStr)
{
HKEY keyServ;
HKEY keyEnum;
HKEY key;
HKEY key2;
LONG lResult;//LONG型变量-保存函数返回值


//查询"SYSTEM\\CurrentControlSet\\Services"下的所有子键保存到keyServ
lResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE,TEXT("SYSTEM\\CurrentControlSet\\Services"),0,KEY_READ,&keyServ);
if (ERROR_SUCCESS != lResult)
return;




//查询"SYSTEM\\CurrentControlSet\\Enum"下的所有子键保存到keyEnum
lResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE,TEXT("SYSTEM\\CurrentControlSet\\Enum"),0,KEY_READ,&keyEnum);
if (ERROR_SUCCESS != lResult)
return;


int i = 0,count = 0;
DWORD size = 0,type = 0;
for (;;++i)
{
Sleep(5);
size = 512;
TCHAR name[512] = {0};//保存keyServ下各子项的字段名称


//逐个枚举keyServ下的各子项字段保存到name中
lResult = RegEnumKeyEx(keyServ,i,name,&size,NULL,NULL,NULL,NULL);


//要读取的子项不存在,即keyServ的子项全部遍历完时跳出循环
if(lResult == ERROR_NO_MORE_ITEMS)
break;


//打开keyServ的子项字段为name所标识的字段的值保存到key
lResult = RegOpenKeyEx(keyServ,name,0,KEY_READ,&key);
if (lResult != ERROR_SUCCESS)
{
RegCloseKey(keyServ);
return;
}




size = 512;
//查询key下的字段为Group的子键字段名保存到name
lResult = RegQueryValueEx(key,TEXT("Group"),0,&type,(LPBYTE)name,&size);
if(lResult == ERROR_FILE_NOT_FOUND)
{
RegCloseKey(key);
continue;
};


//如果查询到的name不是Video则说明该键不是显卡驱动项
if(_tcscmp(TEXT("Video"),name)!=0)
{
RegCloseKey(key);
continue;     //返回for循环
};


//如果程序继续往下执行的话说明已经查到了有关显卡的信息,所以在下面的代码执行完之后要break第一个for循环,函数返回
lResult = RegOpenKeyEx(key,TEXT("Enum"),0,KEY_READ,&key2);
RegCloseKey(key);
key = key2;
size = sizeof(count);
lResult = RegQueryValueEx(key,TEXT("Count"),0,&type,(LPBYTE)&count,&size);//查询Count字段(显卡数目)


for(int j=0;j {
TCHAR sz[512] = {0};
TCHAR name[64] = {0};
wsprintf(name,TEXT("%d"),j);
size = sizeof(sz);
lResult  = RegQueryValueEx(key,name,0,&type,(LPBYTE)sz,&size);




lResult = RegOpenKeyEx(keyEnum,sz,0,KEY_READ,&key2);
if (ERROR_SUCCESS)
{
RegCloseKey(keyEnum);
return;
}


size = sizeof(sz);
lResult = RegQueryValueEx(key2,TEXT("FriendlyName"),0,&type,(LPBYTE)sz,&size);
if(lResult == ERROR_FILE_NOT_FOUND)
{
size = sizeof(sz);
lResult = RegQueryValueEx(key2,TEXT("DeviceDesc"),0,&type,(LPBYTE)sz,&size);
ResultStr.push_back(String(sz));
};
RegCloseKey(key2);
key2 = NULL;
};
RegCloseKey(key);
key = NULL;
break;
}
}


void GetDiskInfo(std::vector &ResultInfo)
{
DWORD DiskCount = 0;


//利用GetLogicalDrives()函数可以获取系统中逻辑驱动器的数量,函数返回的是一个32位无符号整型数据。
DWORD DiskInfo = GetLogicalDrives();


//通过循环操作查看每一位数据是否为1,如果为1则磁盘为真,如果为0则磁盘不存在。
while(DiskInfo)
{
//通过位运算的逻辑与操作,判断是否为1
Sleep(10);
if(DiskInfo&1)
{
DiskCount++;
}
DiskInfo = DiskInfo >> 1;//通过位运算的右移操作保证每循环一次所检查的位置向右移动一位。*/
}


//-------------------------------------------------------------------//
//通过GetLogicalDriveStrings()函数获取所有驱动器字符串信息长度
int DSLength = GetLogicalDriveStrings(0,NULL);


CHAR* DStr = new CHAR[DSLength];
memset(DStr,0,DSLength);


//通过GetLogicalDriveStrings将字符串信息复制到堆区数组中,其中保存了所有驱动器的信息。
GetLogicalDriveStrings(DSLength,DStr);


int DType;
int si=0;
BOOL fResult;
unsigned _int64 i64FreeBytesToCaller;
unsigned _int64 i64TotalBytes;
unsigned _int64 i64FreeBytes;


//读取各驱动器信息,由于DStr内部数据格式是A:\NULLB:\NULLC:\NULL,所以DSLength/4可以获得具体大循环范围
for(int i=0;i{
Sleep(10);
String strdriver = DStr+i*4;
String strTmp,strTotalBytes,strFreeBytes;
DType = GetDriveType(strdriver);//GetDriveType函数,可以获取驱动器类型,参数为驱动器的根目录
if (DRIVE_FIXED == DType)
{
DiskInfo_T diskinfo;
//GetDiskFreeSpaceEx函数,可以获取驱动器磁盘的空间状态,函数返回的是个BOOL类型数据
fResult = GetDiskFreeSpaceEx (strdriver.c_str(),
(PULARGE_INTEGER)&i64FreeBytesToCaller,
(PULARGE_INTEGER)&i64TotalBytes,
(PULARGE_INTEGER)&i64FreeBytes);


if(fResult)
{
diskinfo.DiskName_ = strdriver;
diskinfo.TotalSize_ = (DWORD)(i64TotalBytes/1024/1024);
diskinfo.RestSize_ = (DWORD)(i64FreeBytesToCaller/1024/1024);


ResultInfo.push_back(diskinfo);
}
si+=4;
}
}
}


void GetMemoryInfo(DWORD &dwTotalPhys, DWORD &dwAvailPhys)
{
MEMORYSTATUSEX statex;
statex.dwLength = sizeof (statex);
GlobalMemoryStatusEx(&statex);


dwTotalPhys = (DWORDLONG)statex.ullTotalPhys/(1024*1024); 
dwAvailPhys = (DWORDLONG)statex.ullAvailPhys/(1024*1024);


return;
}


// 安全的取得真实系统信息  
void SafeGetNativeSystemInfo(__out LPSYSTEM_INFO lpSystemInfo)  
{  
if (NULL==lpSystemInfo) return;  
typedef VOID (WINAPI *LPFN_GetNativeSystemInfo)(LPSYSTEM_INFO lpSystemInfo);  
LPFN_GetNativeSystemInfo fnGetNativeSystemInfo = (LPFN_GetNativeSystemInfo)GetProcAddress( GetModuleHandle(_T("kernel32")), "GetNativeSystemInfo");;  
if (NULL != fnGetNativeSystemInfo)  
{  
fnGetNativeSystemInfo(lpSystemInfo);  
}  
else  
{  
GetSystemInfo(lpSystemInfo);  
}  
}  


// 获取操作系统位数  
int GetSystemBits()  
{  
SYSTEM_INFO si;  
SafeGetNativeSystemInfo(&si);  
if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64 ||  
si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_IA64 )  
{  
return 64;  
}  
return 32;  
}


//获取操作系统名字,如windows 7
//成功返回系统信息,失败返回NULL
void GetVersionInfo(String &systeminfo)  
{  
OSVERSIONINFO osvi;  
ZeroMemory(&osvi, sizeof(OSVERSIONINFO));  
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);  
if (!GetVersionEx(&osvi)) {  
return;  
}  


//判断版本  
if (osvi.dwMajorVersion == 5) {  
switch (osvi.dwMinorVersion) {  
case 0:  
//wcscpy_s(systeminfo,_T("Windows 2000"));  
systeminfo = "Windows 2000"; 
break;  
case 1:  
systeminfo = "Windows XP";
break;  
case 2:  
systeminfo = "Windows Server 2003";
break;  
default:  
systeminfo = "Unknown";
break;  
}  


}else if (osvi.dwMajorVersion == 6) {  


switch (osvi.dwMinorVersion) {  
case 0:  
systeminfo = "Windows Vista";
break;  
case 1:  
systeminfo = "Windows 7";
break;  
case 2:  
systeminfo = "Windows 8";
break;  
default:  
systeminfo = "Unknown";
break;  
}  
}else {  
systeminfo = "Unknown";
}  
return;  
}


void GetOSInfo(OSInfo_T & os)
{
GetVersionInfo(os.OSName_);
os.bits_ = GetSystemBits();
}


void WriteSystemInfo()
{
String info;
OSInfo_T os;
GetOSInfo(os);


info += "系统位数为: ";
info += String::ToString((uint32)os.bits_);
info += "\r\n系统名称为: " + os.OSName_ + "\r\n";


CPUInfo_T ci;
GetCPUInfo(ci);


info += "CPU型号为: " + ci.Type_ + "\r\n";


std::vector ResultDisplay;
GetDisplayCardInfo(ResultDisplay);
for (int i = 0; i < ResultDisplay.size(); i++)
{
info += "显卡型号为: " + ResultDisplay.at(i) + ".\r\n";
}


DWORD dwTotalPhys = 0;
DWORD dwAvailPhys = 0;
GetMemoryInfo(dwTotalPhys, dwAvailPhys);
info += "内存总量为: ";
info += String::ToString((uint32)dwTotalPhys);
info += "(MB), 剩余内存为: ";
info += String::ToString((uint32)dwAvailPhys);
info += "(MB).\r\n";


std::vector ResultInfo;
GetDiskInfo(ResultInfo);
for(int i = 0; i < ResultInfo.size(); i++)
{
info += "磁盘名称为: " + ResultInfo.at(i).DiskName_ + ".总大小: ";
info += String::ToString((uint32)ResultInfo.at(i).TotalSize_);
info += "(MB).剩余容量为: ";
info += String::ToString((uint32)ResultInfo.at(i).RestSize_);
info += "(MB).\r\n";
}


vector Audiodevices;
AudioDeviceInfo::GetDevciesAudio(Audiodevices);
for (int i = 0; i < Audiodevices.size(); i++)
{
info += "声卡设备为: " + Audiodevices.at(i).strName
+ ".ID为: " + Audiodevices.at(i).strID
+ ".\r\n";
}


String name, id;
AudioDeviceInfo::GetDefaultAudioDeviceInfo(name, id);
info += "默认声卡设备为: " + name + ".ID为: " + id + ".\r\n";


std::vector NetInfo;
GetNetInfo(NetInfo);
for (int i = 0; i < NetInfo.size(); i++)
{
String ip;
for (int j = 0; j < NetInfo.at(i).NetIP_.size(); j++)
{
ip += "该网卡IP地址为: " + NetInfo.at(i).NetIP_.at(j) + ".\r\n";
}
info += "网卡设备为: " + NetInfo.at(i).NetName_ 
+ "名称为: " + NetInfo.at(i).NetDesc_
+ "Mac地址为: " + NetInfo.at(i).Mac_ 
+ "IP地址: " + ip;
}


String Dir = Platform::getUserDataDirectory();
Dir += "/Live99/system.txt";
FILE *fp = fopen(Dir.c_str(), "wb");
if(fp != NULL)
{
fwrite(info, info.size(), 1, fp);
fclose(fp);
}
}


void GetNetInfo(std::vector &ResultInfo)
{
PIP_ADAPTER_INFO pIpAdapterInfo = new IP_ADAPTER_INFO();
unsigned long stSize = sizeof(IP_ADAPTER_INFO);
int nRel = GetAdaptersInfo(pIpAdapterInfo,&stSize);
if (ERROR_BUFFER_OVERFLOW == nRel)
{
delete pIpAdapterInfo;
pIpAdapterInfo = (PIP_ADAPTER_INFO)new BYTE[stSize];
nRel = GetAdaptersInfo(pIpAdapterInfo,&stSize);    
}


if (ERROR_SUCCESS == nRel)
{
while (pIpAdapterInfo)
{
NetInfo_T netInfo;
netInfo.NetName_ = pIpAdapterInfo->AdapterName;
netInfo.NetDesc_ = pIpAdapterInfo->Description;

for (DWORD i = 0; i < pIpAdapterInfo->AddressLength; i++)
{
if (i < pIpAdapterInfo->AddressLength-1)
{
printf("%02X-", pIpAdapterInfo->Address[i]);
netInfo.Mac_ += String::ToString("%02X-", pIpAdapterInfo->Address[i]);
}
else
{
printf("%02X\n", pIpAdapterInfo->Address[i]);
netInfo.Mac_ += String::ToString("%02X\n", pIpAdapterInfo->Address[i]);
}
}


IP_ADDR_STRING *pIpAddrString = &(pIpAdapterInfo->IpAddressList);
do
{
netInfo.NetIP_.push_back(String(pIpAddrString->IpAddress.String));
pIpAddrString=pIpAddrString->Next;
} while (pIpAddrString);


ResultInfo.push_back(netInfo);
pIpAdapterInfo = pIpAdapterInfo->Next;
}
}


if (pIpAdapterInfo)
{
delete pIpAdapterInfo;
pIpAdapterInfo = NULL;
}
}




你可能感兴趣的:(工具)