Visual C++ Tips: 查询计算机主板的序列号

以下函数将通过命令行的方式查询计算机主板的序列号:


#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.



  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值