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

这篇博客详细介绍了如何使用C++编程获取系统信息,包括CPU频率、制造商、型号,以及操作系统版本、显卡、内存、硬盘和网络信息。通过示例代码展示了如何调用系统API和使用WMI来获取这些数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

从网上搜集了一些获取系统信息的方法,结合自己的需要进行修改和整理下,特此记录。文件在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);
<

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值