从网上搜集了一些获取系统信息的方法,结合自己的需要进行修改和整理下,特此记录。文件在VS2008下编译通过。
1、SystemInfo.h
#ifndef _SYSTEM_INFO_H__
#define _SYSTEM_INFO_H__
#include <windows.h>
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<String> &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<DiskInfo_T> &ResultInfo);
typedef struct NetInfo
{
String NetName_;
String NetDesc_;
std::vector<String> NetIP_;
String Mac_;
}NetInfo_T;
void GetNetInfo(std::vector<NetInfo_T> &ResultInfo);
void WriteSystemInfo();
#endif
2、SystemInfo.cpp
#include "stdafx.h"
#include "SystemInfo.h"
#include "AudioDeviceInfo.h"
#include <tchar.h>
#define _WIN32_DCOM
#include <comdef.h>
#include <Wbemidl.h>
#pragma comment(lib, "wbemuuid.lib")
#include <WinSock2.h>
#include <Iphlpapi.h>
#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);
<