如何获取C / C ++中的磁盘驱动器序列号 [英] How do I get the disk drive serial number in C/C++
本文介绍了如何获取C / C ++中的磁盘驱动器序列号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这已经回答,但它是一个C#解决方案。如何在C或C ++中执行此操作?
This has been already answered but it's a C# solution. How do I do this in C or C++?
推荐答案
有几种方法可以做到这一点。您可以使用系统进行呼叫以获取信息。
There are a few ways to do this. You could make calls using system to get the information.
system("hdparm -i /dev/hda | grep -i serial");
不使用系统:
Without using system:
static struct hd_driveid hd;
int fd;
if ((fd = open("/dev/hda", O_RDONLY | O_NONBLOCK)) < 0) {
printf("ERROR opening /dev/hda\n");
exit(1);
}
if (!ioctl(fd, HDIO_GET_IDENTITY, &hd)) {
printf("%.20s\n", hd.serial_no);
} else if (errno == -ENOMSG) {
printf("No serial number available\n");
} else {
perror("ERROR: HDIO_GET_IDENTITY");
exit(1);
}
Windows:
For Windows:
system("wmic path win32_physicalmedia get SerialNumber");
不使用系统(基于获取WMI数据):
Without using system (Based on Getting WMI Data ):
hres = pSvc->ExecQuery(
bstr_t("WQL"),
bstr_t("SELECT SerialNumber FROM Win32_PhysicalMedia"),
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
NULL,
&pEnumerator);
hr = pclsObj->Get(L"SerialNumber", 0, &vtProp, 0, 0);
这篇关于如何获取C / C ++中的磁盘驱动器序列号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文