实例介绍
【实例简介】
【实例截图】
【核心代码】
// MyReadNumber_C.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <iostream>
#include <string>
#include <string.h>
#include <winsock2.h> // include must before window.h
#include <iphlpapi.h>
#include <windows.h>
#include <intrin.h>
#pragma comment(lib, "iphlpapi.lib")
//警告
#pragma warning(disable: 4996) // avoid GetVersionEx to be warned
// ***** global macros ***** //
static const int kMaxInfoBuffer = 256; //信息缓冲区大小
#define GBYTES 1073741824
#define MBYTES 1048576
#define KBYTES 1024
#define DKBYTES 1024.0
//****** 获取操作系统信息 ******//
void getOsInfo()
{
// get os name according to version number
OSVERSIONINFO osver = { sizeof(OSVERSIONINFO) };
GetVersionEx(&osver);
std::string os_name;
if (osver.dwMajorVersion == 5 && osver.dwMinorVersion == 0)
os_name = "Windows 2000";
else if (osver.dwMajorVersion == 5 && osver.dwMinorVersion == 1)
os_name = "Windows XP";
else if (osver.dwMajorVersion == 6 && osver.dwMinorVersion == 0)
os_name = "Windows 2003";
else if (osver.dwMajorVersion == 5 && osver.dwMinorVersion == 2)
os_name = "windows vista";
else if (osver.dwMajorVersion == 6 && osver.dwMinorVersion == 1)
os_name = "windows 7";
else if (osver.dwMajorVersion == 6 && osver.dwMinorVersion == 2)
os_name = "windows 10";
std::cout << "OS name: " << os_name << std::endl;
std::cout << "OS version: " << osver.dwMajorVersion << '.' << osver.dwMinorVersion << std::endl;
}
//****** 获取CPU信息 ******//
#ifdef _WIN64
// method 2: usde winapi, works for x86 and x64
#include <intrin.h>
void getCpuInfo()
{
int cpuInfo[4] = { -1 };
char cpu_manufacture[32] = { 0 };
char cpu_type[32] = { 0 };
char cpu_freq[32] = { 0 };
__cpuid(cpuInfo, 0x80000002);
memcpy(cpu_manufacture, cpuInfo, sizeof(cpuInfo));
__cpuid(cpuInfo, 0x80000003);
memcpy(cpu_type, cpuInfo, sizeof(cpuInfo));
__cpuid(cpuInfo, 0x80000004);
memcpy(cpu_freq, cpuInfo, sizeof(cpuInfo));
std::cout << "CPU manufacture: " << cpu_manufacture << std::endl;
std::cout << "CPU type: " << cpu_type << std::endl;
std::cout << "CPU main frequency: " << cpu_freq << std::endl;
}
#else
// mothed 1: this kind asm embedded in code only works in x86 build
// save 4 register variables
DWORD deax;
DWORD debx;
DWORD decx;
DWORD dedx;
// init cpu in assembly language 汇编初始化CPU
void initCpu(DWORD veax)
{
__asm
{
mov eax, veax
cpuid
mov deax, eax
mov debx, ebx
mov decx, ecx
mov dedx, edx
}
}
//*** 获取CPU频率 ***//
long getCpuFreq()
{
int start, over;
_asm
{
RDTSC
mov start, eax
}
Sleep(50);
_asm
{
RDTSC
mov over, eax
}
return (over - start) / 50000;
}
//*** 获取生产批号 ***//
std::string getManufactureID()
{
char manuID[25];
memset(manuID, 0, sizeof(manuID));
initCpu(0);
memcpy(manuID 0, &debx, 4); // copy to array
memcpy(manuID 4, &dedx, 4);
memcpy(manuID 8, &decx, 4);
return manuID;
}
//*** 获取CPU类型 ***//
std::string getCpuType()
{
const DWORD id = 0x80000002; // start 0x80000002 end to 0x80000004
char cpuType[49];
memset(cpuType, 0, sizeof(cpuType));
for (DWORD t = 0; t < 3; t )
{
initCpu(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 cpuType;
}
//*** 获取CPU信息 ***//
void getCpuInfo()
{
std::cout << "CPU manufacture: " << getManufactureID() << std::endl;
std::cout << "CPU type: " << getCpuType() << std::endl;
std::cout << "CPU main frequency: " << getCpuFreq() << "MHz" << std::endl;
}
#endif
//****** 获取内存信息 ******//
void getMemoryInfo()
{
std::string memory_info;
MEMORYSTATUSEX statusex;
statusex.dwLength = sizeof(statusex);
if (GlobalMemoryStatusEx(&statusex))
{
unsigned long long total = 0, remain_total = 0, avl = 0, remain_avl = 0;
double decimal_total = 0, decimal_avl = 0;
remain_total = statusex.ullTotalPhys % GBYTES;
total = statusex.ullTotalPhys / GBYTES;
avl = statusex.ullAvailPhys / GBYTES;
remain_avl = statusex.ullAvailPhys % GBYTES;
if (remain_total > 0)
decimal_total = (remain_total / MBYTES) / DKBYTES;
if (remain_avl > 0)
decimal_avl = (remain_avl / MBYTES) / DKBYTES;
decimal_total = (double)total;
decimal_avl = (double)avl;
char buffer[kMaxInfoBuffer];
sprintf_s(buffer, kMaxInfoBuffer, "total %.2f GB (%.2f GB available)", decimal_total, decimal_avl);
memory_info.append(buffer);
}
std::cout << memory_info << std::endl;
}
// ---- get harddisk info ---- //
std::string execCmd(const char* cmd)
{
char buffer[128] = { 0 };
std::string result;
FILE* pipe = _popen(cmd, "r");
if (!pipe) throw std::runtime_error("_popen() failed!");
while (!feof(pipe))
{
if (fgets(buffer, 128, pipe) != NULL)
result = buffer;
}
_pclose(pipe);
return result;
}
//****** 获取硬盘信息 ******//
void getHardDiskInfo()
{
std::string hd_seiral = execCmd("wmic path win32_physicalmedia get SerialNumber");
std::cout << "HardDisk Serial Number: " << hd_seiral << std::endl;
}
//****** 获取网卡信息 ******//
void getNetworkInfo()
{
// PIP_ADAPTER_INFO struct contains network information
PIP_ADAPTER_INFO pIpAdapterInfo = new IP_ADAPTER_INFO();
unsigned long adapter_size = sizeof(IP_ADAPTER_INFO);
int ret = GetAdaptersInfo(pIpAdapterInfo, &adapter_size);
if (ret == ERROR_BUFFER_OVERFLOW)
{
// overflow, use the output size to recreate the handler
delete pIpAdapterInfo;
pIpAdapterInfo = (PIP_ADAPTER_INFO)new BYTE[adapter_size];
ret = GetAdaptersInfo(pIpAdapterInfo, &adapter_size);
}
if (ret == ERROR_SUCCESS)
{
int card_index = 0;
// may have many cards, it saved in linklist
while (pIpAdapterInfo)
{
std::cout << "---- " << "NetworkCard " << card_index << " ----" << std::endl;
std::cout << "Network Card Name: " << pIpAdapterInfo->AdapterName << std::endl;
std::cout << "Network Card Description: " << pIpAdapterInfo->Description << std::endl;
// get IP, one card may have many IPs
PIP_ADDR_STRING pIpAddr = &(pIpAdapterInfo->IpAddressList);
while (pIpAddr)
{
char local_ip[128] = { 0 };
strcpy(local_ip, pIpAddr->IpAddress.String);
std::cout << "Local IP: " << local_ip << std::endl;
pIpAddr = pIpAddr->Next;
}
char local_mac[128] = { 0 };
int char_index = 0;
for (int i = 0; i < pIpAdapterInfo->AddressLength; i )
{
char temp_str[10] = { 0 };
sprintf(temp_str, "%02X-", pIpAdapterInfo->Address[i]); // X for uppercase, x for lowercase
strcpy(local_mac char_index, temp_str);
char_index = 3;
}
local_mac[17] = '\0'; // remove tail '-'
std::cout << "Local Mac: " << local_mac << std::endl;
// here just need the first card info
break;
// iterate next
//pIpAdapterInfo = pIpAdapterInfo->Next;
}
}
if (pIpAdapterInfo)
delete pIpAdapterInfo;
}
//****** 获取进程PID ******//
void getProcessInfo()
{
int pid = GetCurrentProcessId();
// TODO: cpu and mem usage
printf("Current Pid: %d\n", pid);
}
int main()
{
std::cout << "=== os information ===" << std::endl;
getOsInfo();
std::cout << "=== cpu infomation ===" << std::endl;
getCpuInfo();
std::cout << "=== memory information ===" << std::endl;
getMemoryInfo();
std::cout << "=== harddisk information ===" << std::endl;
getHardDiskInfo();
std::cout << "=== network information ===" << std::endl;
getNetworkInfo();
printf("=== process information ===\n");
getProcessInfo();
system("pause");
return 0;
}
// 运行程序: Ctrl F5 或调试 >“开始执行(不调试)”菜单
// 调试程序: F5 或调试 >“开始调试”菜单
// 入门使用技巧:
// 1. 使用解决方案资源管理器窗口添加/管理文件
// 2. 使用团队资源管理器窗口连接到源代码管理
// 3. 使用输出窗口查看生成输出和其他消息
// 4. 使用错误列表窗口查看错误
// 5. 转到“项目”>“添加新项”以创建新的代码文件,或转到“项目”>“添加现有项”以将现有代码文件添加到项目
// 6. 将来,若要再次打开此项目,请转到“文件”>“打开”>“项目”并选择 .sln 文件
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论