標籤:硬體 主板 序號
擷取電腦網卡主板序號的方法很多,這裡採用的是命令列的形式擷取,可以擷取主板序號,很簡單,直接上代碼。
【1】標頭檔
#if !defined(AFX_DD71A3C0_A52F_4C38_B062_8E05785607DC__INCLUDED_)#define AFX_DD71A3C0_A52F_4C38_B062_8E05785607DC__INCLUDED_#if _MSC_VER > 1000#pragma once#endif // _MSC_VER > 1000#include <iostream>#include <string>#include <windows.h>using namespace std;//--------------------------------------------------------------//主板序號//--------------------------------------------------------------BOOL GetBaseBoardByCmd(char *lpszBaseBoard, int len=128);#endif // !defined(AFX_DD71A3C0_A52F_4C38_B062_8E05785607DC__INCLUDED_)
【2】CPP
#include "stdafx.h"#include "BaseBoard.h"//--------------------------------------------------------------//主板序號 -- 擷取不到時為 None//--------------------------------------------------------------BOOL GetBaseBoardByCmd(char *lpszBaseBoard, int len/*=128*/){const long MAX_COMMAND_SIZE = 10000; // 命令列輸出緩衝大小WCHAR szFetCmd[]= L"wmic BaseBoard get SerialNumber"; // 擷取主板序號命令列const string strEnSearch = "SerialNumber"; // 主板序號的前置資訊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 尋找主板序號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 \n int j = 0; for (int i = 0; i < strlen(szBuffer); i++) { if (szBuffer[i] != ' ' && szBuffer[i] != '\n' && szBuffer[i] != '\r') { lpszBaseBoard[j] = szBuffer[i]; j++; } }bret = TRUE;END://關閉所有的控制代碼CloseHandle(hWritePipe);CloseHandle(hReadPipe);CloseHandle(pi.hProcess);CloseHandle(pi.hThread);return(bret);}
C++ 硬體資訊 擷取主板序號