以下函数将通过命令行的方式查询计算机主板的序列号:
#include <iostream>
#include <string>
#include <windows.h>
BOOL GetMainBoardInfoByCmd(char* & lpszBaseBoard)
{
const long COMMAND_SIZE = 1020; // Command line output buffer
const DWORD WAIT_TIME = 500; // INFINITE
// The command to get mainboard serial number
TCHAR szFetCmd[] = _T("wmic BaseBoard get SerialNumber");
// Pre- information of mainboard serial number
const std::string strEnSearch = "SerialNumber";
BOOL fReturnCode = FALSE;
HANDLE hReadPipe = NULL; // Pipeline for READ
HANDLE hWritePipe = NULL; // Pipeline for WRITE
PROCESS_INFORMATION pi; // Process information
STARTUPINFO si; // Control-command window info
SECURITY_ATTRIBUTES sa; // Security attributes
char szBuffer[COMMAND_SIZE + 1] = { 0 }; // Command line output buffer
std::string strBuffer;
DWORD count = 0;
size_t pos = 0;
size_t i = 0;
size_t j = 0;
lpszBaseBoard = (char*)malloc((COMMAND_SIZE + 1)*sizeof(char));
memset(lpszBaseBoard, 0x00, (COMMAND_SIZE + 1)*sizeof(char));
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;
// Step 1: Create pipeline
fReturnCode = CreatePipe(&hReadPipe, &hWritePipe, &sa, 0);
if (!fReturnCode)
{
goto EXIT;
}
// Step 2: Set command line window to be specific READ / WRITE pipeline
GetStartupInfo(&si);
si.hStdError = hWritePipe;
si.hStdOutput = hWritePipe;
si.wShowWindow = SW_HIDE; // hide command line window
si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
// Step 3: Create process to get command line handle
fReturnCode = CreateProcess(NULL, szFetCmd, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi);
if (!fReturnCode)
{
goto EXIT;
}
// Step 4: Get return back data
WaitForSingleObject(pi.hProcess, WAIT_TIME);
fReturnCode = ReadFile(hReadPipe, szBuffer, COMMAND_SIZE, &count, 0);
if (!fReturnCode)
{
goto EXIT;
}
// Step 5: Search for mainboard serial number
fReturnCode = FALSE;
strBuffer = szBuffer;
pos = strBuffer.find(strEnSearch);
if (pos < 0) // NOT FOUND
{
goto EXIT;
}
else
{
strBuffer = strBuffer.substr(pos + strEnSearch.length());
}
memset(szBuffer, 0x00, sizeof(szBuffer));
strcpy_s(szBuffer, strBuffer.c_str());
// Get ride of <space>, \r, \n
j = 0;
for (i = 0; i < strlen(szBuffer); i++)
{
if (szBuffer[i] != ' ' && szBuffer[i] != '\n' && szBuffer[i] != '\r')
{
lpszBaseBoard[j] = szBuffer[i];
j++;
}
}
fReturnCode = TRUE;
EXIT:
CloseHandle(hWritePipe);
CloseHandle(hReadPipe);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
return(fReturnCode);
}
调用例子:
int main()
{
char* lpszMainBoardSN = NULL;
GetMainBoardInfoByCmd(lpszMainBoardSN);
if (lpszMainBoardSN)
{
printf("%s\n", lpszMainBoardSN);
free(lpszMainBoardSN);
lpszMainBoardSN = NULL;
}
else
{
printf("N/A\n");
}
return 0;
}
一般情况下通过这个小程序可以得到主板序列号。例如,我的 ThinkPad 笔记本通过这个程序的输出结果为:VF26G9132DZ
但是如果OEM厂商没有在BIOS中写入主板序列号,那么我们可能得到的输出结果是:TobefilledbyO.E.M.