最近给朋友帮忙,遇到一个问题:如何在Linux取到硬盘的序列号?目前网上说的方法大都是Windows 下用Delphi、C#等工具开发的。如何用ANSI C来实现呢?其实C在做这种底层事情方面比Delphi和C#都要容易的。下面这个函数就是取得硬盘序列号的完整代码,稍加改造后,取得硬盘型号等数据也是很容易的事:
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <sys/ioctl.h> #include <linux/hdreg.h> #include <fcntl.h> #include <errno.h> // 获取硬盘序列号,成功返回0, 失败返回-1 // szDevName为取得的序列号,空间由主程序预先分配好,并将空间长度用nLimit参数传递给get_hd_sn()函数 int get_hd_sn(const char* szDevName, char* szSN, size_t nLimit) { struct hd_driveid id; int nRtn = -1; int fd = open(szDevName, O_RDONLY|O_NONBLOCK); while(1) { if (fd < 0) { perror(szDevName); break; } //这句话是关键 if(!ioctl(fd, HDIO_GET_IDENTITY, &id)) { strncpy(szSN, id.serial_no, nLimit); //printf("Model Number=%s\n",id.model); nRtn = 0; } break; } return nRtn; }
|