linux
一、cpuid 有可能会重复
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <arpa/inet.h>
#include <fstream>
#include <unistd.h>
#include <sys/types.h>
bool get_cpu_id_by_asm(char** cpu_id)
{
unsigned int s1 = 0;
unsigned int s2 = 0;
asm volatile
(
"movl $0x01, %%eax; \n\t"
"xorl %%edx, %%edx; \n\t"
"cpuid; \n\t"
"movl %%edx, %0; \n\t"
"movl %%eax, %1; \n\t"
: "=m"(s1), "=m"(s2)
);
if (0 == s1 && 0 == s2)
{
return(false);
}
char cpu[32] = { 0 };
snprintf(cpu, sizeof(cpu), "%08X%08X", htonl(s2), htonl(s1));
strncpy(*cpu_id, cpu, strlen(cpu));
return(true);
}
void parse_cpu_id(const char * file_name, const char * match_words, char** cpu_id)
{
std::string tempCPUID = "";
std::ifstream ifs(file_name, std::ios::binary);
if (!ifs.is_open())
{
return;
}
char line[4096] = { 0 };
while (!ifs.eof())
{
ifs.getline(line, sizeof(line));
if (!ifs.good())
{
break;
}
const char * cpu = strstr(line, match_words);
if (NULL == cpu)
{
continue;
}
cpu += strlen(match_words);
while ('\0' != cpu[0])
{
if (' ' != cpu[0])
{
tempCPUID.push_back(cpu[0]);
}
++cpu;
}
if (!tempCPUID.empty())
{
break;
}
}
strncpy(*cpu_id, tempCPUID.c_str(), strlen(tempCPUID.c_str()));
ifs.close();
}
bool get_cpu_id_by_system(char** cpu_id)
{
const char * dmidecode_result = ".dmidecode_result.txt";
char command[512] = { 0 };
snprintf(command, sizeof(command), "dmidecode -t 4 | grep ID > %s", dmidecode_result);
if (0 == system(command))
{
parse_cpu_id(dmidecode_result, "ID:", cpu_id);
}
unlink(dmidecode_result);
return(strlen(*cpu_id)>0);
}
bool getSystemInfo(char** sid)
{
if (get_cpu_id_by_asm(sid))
{
return(true);
}
if (0 == getuid())
{
if (get_cpu_id_by_system(sid))
{
return(true);
}
}
return(false);
}
二、主板 序列号,有可能会取不到;
static void parse_board_serial(const char * file_name, const char * match_words, std::string & board_serial)
{
board_serial.c_str();
std::ifstream ifs(file_name, std::ios::binary);
if (!ifs.is_open())
{
return;
}
char line[4096] = { 0 };
while (!ifs.eof())
{
ifs.getline(line, sizeof(line));
if (!ifs.good())
{
break;
}
const char * board = strstr(line, match_words);
if (NULL == board)
{
continue;
}
board += strlen(match_words);
while ('\0' != board[0])
{
if (' ' != board[0])
{
board_serial.push_back(board[0]);
}
++board;
}
if ("None" == board_serial)
{
board_serial.clear();
continue;
}
if (!board_serial.empty())
{
break;
}
}
ifs.close();
}
static bool get_board_serial_by_system(std::string & board_serial)
{
board_serial.c_str();
const char * dmidecode_result = ".dmidecode_result.txt";
char command[512] = { 0 };
snprintf(command, sizeof(command), "dmidecode -t 2 | grep Serial > %s", dmidecode_result);
if (0 == system(command))
{
parse_board_serial(dmidecode_result, "Serial Number:", board_serial);
}
unlink(dmidecode_result);
return(!board_serial.empty());
}
static bool get_board_serial_number(std::string & board_serial)
{
if (0 == getuid())
{
if (get_board_serial_by_system(board_serial))
{
return(true);
}
}
return(false);
}
三、MAC 地址
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <unistd.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <string>
#include <fstream>
bool get_mac_address_by_ioctl(std::string & mac_address)
{
mac_address.clear();
int sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0)
{
return(false);
}
struct ifreq ifr = { 0 };
strncpy(ifr.ifr_name, "eth0", sizeof(ifr.ifr_name) - 1);
bool ret = (ioctl(sock, SIOCGIFHWADDR, &ifr) >= 0);
close(sock);
const char hex[] =
{
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
};
char mac[16] = { 0 };
for (int index = 0; index < 6; ++index)
{
size_t value = ifr.ifr_hwaddr.sa_data[index] & 0xFF;
mac[2 * index + 0] = hex[value / 16];
mac[2 * index + 1] = hex[value % 16];
}
std::string(mac).swap(mac_address);
return(ret);
}
static void parse_mac_address(const char * file_name, const char * match_words, std::string & mac_address)
{
mac_address.c_str();
std::ifstream ifs(file_name, std::ios::binary);
if (!ifs.is_open())
{
return;
}
char line[4096] = { 0 };
while (!ifs.eof())
{
ifs.getline(line, sizeof(line));
if (!ifs.good())
{
break;
}
const char * mac = strstr(line, match_words);
if (NULL == mac)
{
continue;
}
mac += strlen(match_words);
while ('\0' != mac[0])
{
if (' ' != mac[0] && ':' != mac[0])
{
mac_address.push_back(mac[0]);
}
++mac;
}
if (!mac_address.empty())
{
break;
}
}
ifs.close();
}
static bool get_mac_address_by_system(std::string & mac_address)
{
mac_address.c_str();
const char * lshw_result = ".lshw_result.txt";
char command[512] = { 0 };
snprintf(command, sizeof(command), "lshw -c network | grep serial | head -n 1 > %s", lshw_result);
if (0 == system(command))
{
parse_mac_address(lshw_result, "serial:", mac_address);
}
unlink(lshw_result);
return(!mac_address.empty());
}
static bool get_mac_address(std::string & mac_address)
{
if (get_mac_address_by_ioctl(mac_address))
{
return(true);
}
if (get_mac_address_by_system(mac_address))
{
return(true);
}
return(false);
}
static void test_1()
{
std::string mac_address;
if (get_mac_address(mac_address))
{
printf("mac_address: [%s]\n", mac_address.c_str());
}
else
{
printf("can not get mac address\n");
}
}
static void test_2()
{
{
std::string mac_address;
if (get_mac_address_by_ioctl(mac_address))
{
printf("mac_address: [%s]\n", mac_address.c_str());
}
else
{
printf("can not get mac address\n");
}
}
{
std::string mac_address;
if (get_mac_address_by_system(mac_address))
{
printf("mac_address: [%s]\n", mac_address.c_str());
}
else
{
printf("can not get mac address\n");
}
}
}
int main(int argc, char * argv[])
{
test_1();
test_2();
return(0);
}
四、uuid
int fd = open("/sys/class/dmi/id/product_uuid",O_RDONLY);
if(fd < 0)
{
perror("open error");
return 0;
}
char str[1024];
int count;
while((count = read(fd,str,1024)) > 0)
{
printf("uuid:%s\n",str);
}
close(fd);
int fd = open("/sys/class/dmi/id/product_serial",O_RDONLY);
if(fd < 0)
{
perror("open error board_serial");
return 0;
}
char str[1024];
int count;
while((count = read(fd,str,1024)) > 0)
{
printf("board_serial:%s\n",str);
}
close(fd);
五、磁盘信息
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <fcntl.h>
#include <unistd.h>
#include <scsi/sg.h>
#include <sys/ioctl.h>
#include <linux/hdreg.h>
#include <string>
#include <fstream>
static bool get_disk_name(std::string & disk_name)
{
disk_name.c_str();
std::ifstream ifs("/etc/mtab", std::ios::binary);
if (!ifs.is_open())
{
return(false);
}
char line[4096] = { 0 };
while (!ifs.eof())
{
ifs.getline(line, sizeof(line));
if (!ifs.good())
{
break;
}
const char * disk = line;
while (isspace(disk[0]))
{
++disk;
}
const char * space = strchr(disk, ' ');
if (NULL == space)
{
continue;
}
const char * mount = space + 1;
while (isspace(mount[0]))
{
++mount;
}
if ('/' != mount[0] || ' ' != mount[1])
{
continue;
}
while (space > disk && isdigit(space[-1]))
{
--space;
}
if (space > disk)
{
std::string(disk, space).swap(disk_name);
break;
}
}
ifs.close();
return(!disk_name.empty());
}
static void trim_serial(const void * serial, size_t serial_len, std::string & serial_no)
{
const char * serial_s = static_cast<const char *>(serial);
const char * serial_e = serial_s + serial_len;
while (serial_s < serial_e)
{
if (isspace(serial_s[0]))
{
++serial_s;
}
else if ('\0' == serial_e[-1] || isspace(serial_e[-1]))
{
--serial_e;
}
else
{
break;
}
}
if (serial_s < serial_e)
{
std::string(serial_s, serial_e).swap(serial_no);
}
}
static bool get_disk_serial_by_way_1(const std::string & disk_name, std::string & serial_no)
{
serial_no.clear();
int fd = open(disk_name.c_str(), O_RDONLY);
if (-1 == fd)
{
return(false);
}
struct hd_driveid drive = { 0 };
if (0 == ioctl(fd, HDIO_GET_IDENTITY, &drive))
{
trim_serial(drive.serial_no, sizeof(drive.serial_no), serial_no);
}
close(fd);
return(!serial_no.empty());
}
static bool scsi_io(
int fd, unsigned char * cdb,
unsigned char cdb_size, int xfer_dir,
unsigned char * data, unsigned int data_size,
unsigned char * sense, unsigned int sense_len
)
{
sg_io_hdr_t io_hdr = { 0 };
io_hdr.interface_id = 'S';
io_hdr.cmdp = cdb;
io_hdr.cmd_len = cdb_size;
io_hdr.sbp = sense;
io_hdr.mx_sb_len = sense_len;
io_hdr.dxfer_direction = xfer_dir;
io_hdr.dxferp = data;
io_hdr.dxfer_len = data_size;
io_hdr.timeout = 5000;
if (ioctl(fd, SG_IO, &io_hdr) < 0)
{
return(false);
}
if (SG_INFO_OK != (io_hdr.info & SG_INFO_OK_MASK) && io_hdr.sb_len_wr > 0)
{
return(false);
}
if (io_hdr.masked_status || io_hdr.host_status || io_hdr.driver_status)
{
return(false);
}
return(true);
}
static bool get_disk_serial_by_way_2(const std::string & disk_name, std::string & serial_no)
{
serial_no.clear();
int fd = open(disk_name.c_str(), O_RDONLY);
if (-1 == fd)
{
return(false);
}
int version = 0;
if (ioctl(fd, SG_GET_VERSION_NUM, &version) < 0 || version < 30000)
{
close(fd);
return(false);
}
const unsigned int data_size = 0x00ff;
unsigned char data[data_size] = { 0 };
const unsigned int sense_len = 32;
unsigned char sense[sense_len] = { 0 };
unsigned char cdb[] = { 0x12, 0x01, 0x80, 0x00, 0x00, 0x00 };
cdb[3] = (data_size >> 8) & 0xff;
cdb[4] = (data_size & 0xff);
if (scsi_io(fd, cdb, sizeof(cdb), SG_DXFER_FROM_DEV, data, data_size, sense, sense_len))
{
int page_len = data[3];
trim_serial(data + 4, page_len, serial_no);
}
close(fd);
return(!serial_no.empty());
}
static bool parse_serial(const char * line, int line_size, const char * match_words, std::string & serial_no)
{
const char * serial_s = strstr(line, match_words);
if (NULL == serial_s)
{
return(false);
}
serial_s += strlen(match_words);
while (isspace(serial_s[0]))
{
++serial_s;
}
const char * serial_e = line + line_size;
const char * comma = strchr(serial_s, ',');
if (NULL != comma)
{
serial_e = comma;
}
while (serial_e > serial_s && isspace(serial_e[-1]))
{
--serial_e;
}
if (serial_e <= serial_s)
{
return(false);
}
std::string(serial_s, serial_e).swap(serial_no);
return(true);
}
static void get_serial(const char * file_name, const char * match_words, std::string & serial_no)
{
serial_no.c_str();
std::ifstream ifs(file_name, std::ios::binary);
if (!ifs.is_open())
{
return;
}
char line[4096] = { 0 };
while (!ifs.eof())
{
ifs.getline(line, sizeof(line));
if (!ifs.good())
{
break;
}
if (0 == ifs.gcount())
{
continue;
}
if (parse_serial(line, ifs.gcount() - 1, match_words, serial_no))
{
break;
}
}
ifs.close();
}
static bool get_disk_serial_by_way_3(const std::string & disk_name, std::string & serial_no)
{
serial_no.c_str();
const char * hdparm_result = ".hdparm_result.txt";
char command[512] = { 0 };
snprintf(command, sizeof(command), "hdparm -i %s | grep SerialNo > %s", disk_name.c_str(), hdparm_result);
if (0 == system(command))
{
get_serial(hdparm_result, "SerialNo=", serial_no);
}
unlink(hdparm_result);
return(!serial_no.empty());
}
static bool get_disk_serial_by_way_4(std::string & serial_no)
{
serial_no.c_str();
const char * lshw_result = ".lshw_result.txt";
char command[512] = { 0 };
snprintf(command, sizeof(command), "lshw -class disk | grep serial > %s", lshw_result);
if (0 == system(command))
{
get_serial(lshw_result, "serial:", serial_no);
}
unlink(lshw_result);
return(!serial_no.empty());
}
static bool get_disk_serial_number(std::string & serial_no)
{
if (0 != getuid())
{
return(false);
}
std::string disk_name;
if (get_disk_name(disk_name))
{
if (get_disk_serial_by_way_1(disk_name, serial_no))
{
return(true);
}
if (get_disk_serial_by_way_2(disk_name, serial_no))
{
return(true);
}
if (get_disk_serial_by_way_3(disk_name, serial_no))
{
return(true);
}
}
if (get_disk_serial_by_way_4(serial_no))
{
return(true);
}
return(false);
}
static void test_1()
{
std::string serial_no;
if (get_disk_serial_number(serial_no))
{
printf("serial_number: [%s]\n", serial_no.c_str());
}
else
{
printf("get serial number failed\n");
}
}
static void test_2()
{
std::string disk_name;
if (get_disk_name(disk_name))
{
printf("disk_name:[%s]\n", disk_name.c_str());
{
std::string serial_no;
get_disk_serial_by_way_1(disk_name, serial_no);
printf("get_serial_by_way_1:[%s]\n", serial_no.c_str());
}
{
std::string serial_no;
get_disk_serial_by_way_2(disk_name, serial_no);
printf("get_serial_by_way_2:[%s]\n", serial_no.c_str());
}
{
std::string serial_no;
get_disk_serial_by_way_3(disk_name, serial_no);
printf("get_serial_by_way_3:[%s]\n", serial_no.c_str());
}
}
{
std::string serial_no;
get_disk_serial_by_way_4(serial_no);
printf("get_serial_by_way_4:[%s]\n", serial_no.c_str());
}
}
int main(int argc, char * argv[])
{
printf("---------------\n");
test_1();
printf("---------------\n");
test_2();
printf("---------------\n");
return(0);
}
以上信息可以参考;
Windows:
CPUID
wmic cpu get processorid
DISK SN
wmic diskdrive get serialnumber
smBIOS UUID
wmic csproduct get UUID
ID(机器码,只有品牌机才有)
wmic csproduct get IdentifyingNumber
Windows 平台:
UserInfo.h
#pragma once
#include "stdafx.h"
#define _WIN32_DCOM
#include <comdef.h>
#include <Wbemidl.h>
# pragma comment(lib, "wbemuuid.lib")
using namespace std;
typedef struct UserInfo_t
{
char CpuID[20]; //CPU序列号
char BaseBoardID[256]; //主板ID
char SystemDiskID[256]; //系统所在硬盘的序列号
char BIOSID[20]; //BIOS序列号
char MacAddress[20]; //MAC地址
}UserInfo;
int GetUserInfo(UserInfo &info);
GetUerInfo.cpp
#include "UserInfo.h"
#include <windows.h>
#include <string>
#include <strsafe.h>
#define PROPERTY_MAX_LEN 128 // 属性字段最大长度
typedef struct _T_DEVICE_PROPERTY
{
TCHAR szProperty[PROPERTY_MAX_LEN];
} T_DEVICE_PROPERTY;
#define WMI_QUERY_TYPENUM 7 // WMI查询支持的类型数
#pragma comment (lib, "comsuppw.lib")
#pragma comment (lib, "wbemuuid.lib")
static const int BUFFER_SIZE = 256;
typedef struct _T_WQL_QUERY
{
CHAR* szSelect; // SELECT语句
WCHAR* szProperty; // 属性字段
} T_WQL_QUERY;
// WQL查询语句
const T_WQL_QUERY szWQLQuery[] = {
// 处理器ID
"SELECT * FROM Win32_Processor WHERE (ProcessorId IS NOT NULL)",
L"ProcessorId",
// 主板序列号
"SELECT * FROM Win32_BaseBoard WHERE (SerialNumber IS NOT NULL)",
L"SerialNumber",
// BIOS序列号 不能查到
//"SELECT * FROM Win32_BIOS WHERE (SerialNumber IS NOT NULL)",
//L"SerialNumber",
// 网卡当前MAC地址
"SELECT * FROM Win32_NetworkAdapter WHERE (MACAddress IS NOT NULL) AND (NOT (PNPDeviceID LIKE 'ROOT%'))",
L"MACAddress",
//硬盘序列号//SELECT * FROM Win32_DiskPartition WHERE Bootable = TRUE
"SELECT * FROM Win32_DiskPartition WHERE Bootable = TRUE ",
L"DiskIndex",
//通过 index
"SELECT * FROM Win32_DiskDrive WHERE Index = ",
L"SerialNumber",
};
using namespace std;
void Trims(char* data) //去掉字符串中的空格
{
int i = -1, j = 0;
int ch = ' ';
while (data[++i] != '\0')
{
if (data[i] != ch)
{
data[j++] = data[i];
}
}
data[j] = '\0';
}
_Ret_z_ inline LPTSTR W2T(_In_z_ LPWSTR lp)
{
return LPTSTR(lp);
}
int GetUserInfo(UserInfo &info)
{
HRESULT hres;
memset(&info, 0x00, sizeof(UserInfo));
CoUninitialize();
hres = CoInitializeEx(0, COINIT_MULTITHREADED); //第二个参数设置当前线程的并发模式为多线程
//hres = CoInitializeEx(0,COINIT_APARTMENTTHREADED); //并发模式为单线程(即只能在单线程函数中调用GetUserInfo())
if (FAILED(hres))
{
return -1;
}
hres = CoInitializeSecurity(
NULL,
-1,
NULL,
NULL,
RPC_C_AUTHN_LEVEL_DEFAULT,
RPC_C_IMP_LEVEL_IMPERSONATE,
NULL,
EOAC_NONE,
NULL
);
if (FAILED(hres))
{
CoUninitialize();
return -2;
}
IWbemLocator *pLoc = NULL;
hres = CoCreateInstance(
CLSID_WbemLocator,
0,
CLSCTX_INPROC_SERVER,
IID_IWbemLocator, (LPVOID *)&pLoc);
if (FAILED(hres))
{
CoUninitialize();
return -3;
}
IWbemServices *pSvc = NULL;
hres = pLoc->ConnectServer(
_bstr_t(L"ROOT\\CIMV2"),
NULL,
NULL,
0,
NULL,
0,
0,
&pSvc
);
if (FAILED(hres))
{
pLoc->Release();
CoUninitialize();
return -4;
}
hres = CoSetProxyBlanket(
pSvc,
RPC_C_AUTHN_WINNT,
RPC_C_AUTHZ_NONE,
NULL,
RPC_C_AUTHN_LEVEL_CALL,
RPC_C_IMP_LEVEL_IMPERSONATE,
NULL,
EOAC_NONE
);
if (FAILED(hres))
{
pSvc->Release();
pLoc->Release();
CoUninitialize();
return -5;
}
char diskIndex[10] = { 0 };
for (int index = 0; index < 5; index++)
{
string sqlQuery = szWQLQuery[index].szSelect;
if (4 == index)
{
sqlQuery += diskIndex;
}
// 通过请求代理来向WMI发送请求
IEnumWbemClassObject *pEnumerator = NULL;
hres = pSvc->ExecQuery(
bstr_t("WQL"),
bstr_t(sqlQuery.c_str()),
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
NULL,
&pEnumerator
);
if (FAILED(hres))
{
pSvc->Release();
pLoc->Release();
CoUninitialize();
return -3;
}
// 循环枚举所有的结果对象
char tmp[256] = { 0 };
while (pEnumerator)
{
IWbemClassObject *pclsObj = NULL;
ULONG uReturn = 0;
pEnumerator->Next(
WBEM_INFINITE,
1,
&pclsObj,
&uReturn
);
if (uReturn == 0)
{
break;
}
// 获取属性值
VARIANT vtProperty;
VariantInit(&vtProperty);
pclsObj->Get(szWQLQuery[index].szProperty, 0, &vtProperty, NULL, NULL);
if (3 == index)
{
//根据系统所在硬盘的ID查询序列号
_itoa(vtProperty.intVal, diskIndex, 10);
}
else
{
wcstombs(tmp, vtProperty.bstrVal, sizeof(tmp) - 1);
Trims(tmp);
}
VariantClear(&vtProperty);
pclsObj->Release();
} // End While
switch (index)
{
case 0:
{
strcpy_s(info.CpuID, sizeof(info.CpuID) - 1, tmp);
}
break;
case 1:
{
strcpy_s(info.BaseBoardID, sizeof(info.BaseBoardID) - 1, tmp);
}
break;
case 2:
{
strcpy_s(info.MacAddress, sizeof(info.MacAddress) - 1, tmp);
}
break;
case 3:
{
strcpy_s(info.SystemDiskID, sizeof(info.SystemDiskID) - 1, tmp);
}
break;
case 4:
{
strcpy_s(info.SystemDiskID, sizeof(info.SystemDiskID) - 1, tmp);
}
break;
}
pEnumerator->Release();
}
pSvc->Release();
pLoc->Release();
CoUninitialize();
return 0;
}