C++ 硬件信息 获取CPU序列号

【1】头文件

#if !defined(AFX_14BEC153_17B9_47BE_845F_71A27BF26B59__INCLUDED_)#define AFX_14BEC153_17B9_47BE_845F_71A27BF26B59__INCLUDED_#if _MSC_VER > 1000#pragma once#endif // _MSC_VER > 1000#include <iostream>#include <string>#include <windows.h>using namespace std;//————————————————————–//CPU序列号//————————————————————–BOOL GetCpuByCmd(char *lpszCpu, int len=128);#endif // !defined(AFX_14BEC153_17B9_47BE_845F_71A27BF26B59__INCLUDED_)

【2】CPP

#include "stdafx.h"#include "CpuID.h"//————————————————————–//CPU序列号//————————————————————–BOOL GetCpuByCmd(char *lpszCpu, int len/*=128*/){const long MAX_COMMAND_SIZE = 10000; // 命令行输出缓冲大小WCHAR szFetCmd[] = L"wmic cpu get processorid"; // 获取CPU序列号命令行const string strEnSearch = "ProcessorId"; // CPU序列号的前导信息BOOL bret = FALSE;HANDLE hReadPipe = NULL; //读取管道HANDLE hWritePipe = NULL; //写入管道PROCESS_INFORMATION pi; //进程信息STARTUPINFOsi; //控制命令行窗口信息SECURITY_ATTRIBUTES sa; //安全属性charszBuffer[MAX_COMMAND_SIZE + 1] = { 0 }; // 放置命令行结果的输出缓冲区stringstrBuffer;unsigned longcount = 0;longipos = 0;memset(&pi, 0, sizeof(pi));memset(&si, 0, sizeof(si));memset(&sa, 0, sizeof(sa));pi.hProcess = NULL;pi.hThread = NULL;si.cb = sizeof(STARTUPINFO);sa.nLength = sizeof(SECURITY_ATTRIBUTES);sa.lpSecurityDescriptor = NULL;sa.bInheritHandle = TRUE;//1.0 创建管道bret = CreatePipe(&hReadPipe, &hWritePipe, &sa, 0);if (!bret){goto END;}//2.0 设置命令行窗口的信息为指定的读写管道GetStartupInfo(&si);si.hStdError = hWritePipe;si.hStdOutput = hWritePipe;si.wShowWindow = SW_HIDE; //隐藏命令行窗口si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;//3.0 创建获取命令行的进程bret = CreateProcess(NULL, szFetCmd, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi);if (!bret){goto END;}//4.0 读取返回的数据WaitForSingleObject(pi.hProcess, 500/*INFINITE*/);bret = ReadFile(hReadPipe, szBuffer, MAX_COMMAND_SIZE, &count, 0);if (!bret){goto END;}//5.0 查找CPU序列号bret = FALSE;strBuffer = szBuffer;ipos = strBuffer.find(strEnSearch);if (ipos < 0) // 没有找到{goto END;}else{strBuffer = strBuffer.substr(ipos + strEnSearch.length());}memset(szBuffer, 0x00, sizeof(szBuffer));strcpy_s(szBuffer, strBuffer.c_str());//去掉中间的空格 \r \nint j = 0;for (int i = 0; i < strlen(szBuffer); i++){if (szBuffer[i] != ' ' && szBuffer[i] != '\n' && szBuffer[i] != '\r'){lpszCpu[j] = szBuffer[i];j++;}}bret = TRUE;END://关闭所有的句柄CloseHandle(hWritePipe);CloseHandle(hReadPipe);CloseHandle(pi.hProcess);CloseHandle(pi.hThread);return(bret);}

,会让你的心态更平和更坦然,也会让你心无旁骛,更会让你的心灵得到解脱和抚慰。

C++ 硬件信息 获取CPU序列号

相关文章:

你感兴趣的文章:

标签云: