C/C++ 获取硬件唯一标识 (CPU序列号/硬盘号)

您所在的位置:网站首页 喜马拉雅转码工具apk C/C++ 获取硬件唯一标识 (CPU序列号/硬盘号)

C/C++ 获取硬件唯一标识 (CPU序列号/硬盘号)

2022-12-31 00:47| 来源: 网络整理| 查看: 265

调用控制台来获取CPU序列号/硬盘号 不同PC的硬盘数量可能不同 若有多个硬盘,此处的输出将多个硬盘号拼接到了一起 若需拆分可自行修改。 注:插拔硬盘U盘等会造成获取到的硬盘号不同,如果想以此为机器码的同学可用BIOS序列号替代硬盘号。命令行调用改为wmic bios get serialnumber 即可

#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 #include #include //#define CPUSIZE 17 using namespace std; //-------------------------------------------------------------- // CPU序列号 //-------------------------------------------------------------- BOOL GetCpuByCmd(string &ider, int len = 128); BOOL GetDiskByCmd(string &ider, int len = 128); #endif // !defined(AFX_14BEC153_17B9_47BE_845F_71A27BF26B59__INCLUDED_) //-------------------------------------------------------------- // CPU序列号 //-------------------------------------------------------------- BOOL GetCpuByCmd(string &ider, int len/*=128*/) { //CPU序列 const long MAX_COMMAND_SIZE = 10000; // 命令行输出缓冲大小 WCHAR szFetCmd[] = L"wmic cpu get processorid"; // 获取CPU序列号命令行 const string strEnSearch = "ProcessorId"; // CPU序列号的前导信息 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; //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 查找CPU序列号 bret = FALSE; strBuffer = szBuffer; ipos = strBuffer.find(strEnSearch); if (ipos strBuffer = strBuffer.substr(ipos + strEnSearch.length()); } memset(szBuffer, 0x00, sizeof(szBuffer)); strcpy_s(szBuffer, strBuffer.c_str()); //modify here //去掉中间的空格 \r \n char temp[512]; memset(temp, 0, sizeof(temp)); int index = 0; for (size_t i = 0; i temp[index] = strBuffer[i]; index++; } } ider = temp; bret = TRUE; END: //关闭所有的句柄 CloseHandle(hWritePipe); CloseHandle(hReadPipe); CloseHandle(pi.hProcess); CloseHandle(pi.hThread); return(bret); } //-------------------------------------------------------------- // 硬盘编号 //-------------------------------------------------------------- BOOL GetDiskByCmd(string &ider, int len/*=128*/) { //diskdrive const long MAX_COMMAND_SIZE = 10000; // 命令行输出缓冲大小 WCHAR szFetCmd[] = L"wmic diskdrive get serialnumber"; // 获取DiskDrive命令行 const string strEnSearch = "SerialNumber"; // DiskDrive序列号的前导信息 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; //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 查找CPU序列号 bret = FALSE; strBuffer = szBuffer; ipos = strBuffer.find(strEnSearch); if (ipos strBuffer = strBuffer.substr(ipos + strEnSearch.length()); } memset(szBuffer, 0x00, sizeof(szBuffer)); strcpy_s(szBuffer, strBuffer.c_str()); //modify here //去掉中间的空格 \r \n char temp[512]; memset(temp, 0, sizeof(temp)); int index = 0; for (size_t i = 0; i temp[index] = strBuffer[i]; index++; } } ider = temp; bret = TRUE; END: //关闭所有的句柄 CloseHandle(hWritePipe); CloseHandle(hReadPipe); CloseHandle(pi.hProcess); CloseHandle(pi.hThread); return(bret); } int main() { string cpuid; if (!GetCpuByCmd(cpuid)) { cout


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3