C++ 硬件信息 获取CPU序列号
来源:互联网 发布:防蹭网软件怎么用 编辑:程序博客网 时间:2024/05/01 03:16
获取计算机CPU序列号的方法很多,这里采用的是命令行的形式获取,可以获取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;
-
-
-
-
- BOOL GetCpuByCmd(char *lpszCpu, int len=128);
-
-
- #endif // !defined(AFX_14BEC153_17B9_47BE_845F_71A27BF26B59__INCLUDED_)
【2】CPP
- #include "stdafx.h"
- #include "CpuID.h"
-
-
-
-
-
- BOOL GetCpuByCmd(char *lpszCpu, int len)
- {
- const long MAX_COMMAND_SIZE = 10000;
- WCHAR szFetCmd[] = L"wmic cpu get processorid";
- const string strEnSearch = "ProcessorId";
-
- BOOL bret = FALSE;
- HANDLE hReadPipe = NULL;
- HANDLE hWritePipe = NULL;
- PROCESS_INFORMATION pi;
- STARTUPINFO si;
- SECURITY_ATTRIBUTES sa;
-
- char szBuffer[MAX_COMMAND_SIZE + 1] = { 0 };
- string strBuffer;
- unsigned long count = 0;
- long ipos = 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;
-
-
- bret = CreatePipe(&hReadPipe, &hWritePipe, &sa, 0);
- if (!bret)
- {
- goto END;
- }
-
-
- GetStartupInfo(&si);
- si.hStdError = hWritePipe;
- si.hStdOutput = hWritePipe;
- si.wShowWindow = SW_HIDE;
- si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
-
-
- bret = CreateProcess(NULL, szFetCmd, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi);
- if (!bret)
- {
- goto END;
- }
-
-
- WaitForSingleObject(pi.hProcess, 500);
- bret = ReadFile(hReadPipe, szBuffer, MAX_COMMAND_SIZE, &count, 0);
- if (!bret)
- {
- goto END;
- }
-
-
- 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());
-
-
- int 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);
- }
http://blog.csdn.net/jamesliulyc/article/details/2028958
0 0