C++ 硬體資訊 擷取主板序號

來源:互聯網
上載者:User

標籤:硬體   主板   序號   

    擷取電腦網卡主板序號的方法很多,這裡採用的是命令列的形式擷取,可以擷取主板序號,很簡單,直接上代碼。


【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++ 硬體資訊 擷取主板序號

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.