How to capture hardware id

Asked

Viewed 835 times

2

I’m trying to find a way to capture the serial of the hard drive, CPU and motherboard, the CPU I found a mode that is using cpuid. h, now the motherboard and the hard drive I found no way to capture, anyone has any idea ?

1 answer

1

There are a few ways to do this. You can use the command system in c to directly call a terminal command.

For Linux:

system("hdparm -i /dev/hda | grep -i serial");

System-free:

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);
}

For Windows:

system("wmic path win32_physicalmedia get SerialNumber");

No use 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);

Solution taken from Soen.

  • I liked the answer, but it is not complete - the questioner also wants to know about the motherboard serial. You only write "ways to do 'that'" without making it explicit (in English :-) ) that 'that' is getting the serial from HD.

  • @jsbueno I was waiting for a comment from the author of the question to know if this is enough. Suddenly she’s working on Macosx and none of the information I gave her would be enough ;-)

  • Thanks for the reply Gabrieloshiro, so what I intend to do is to be able to work on all platforms. I have a program already ready, what I want to do is to save the serial id of the hard drive, cpu and motherboard in the database, of each one connected to my program, because the locks will be made through such information

  • @But when vc compiles a code in C vc knows which platform you are compiling for. Even if you want to make a unique code that runs on multiple platforms you will have to compile your program for each platform, correct? If that is the case, you have the system information at build time. And you have to use system to call console commands that give you this information. Somewhere in your program you have to have a if that selects which platform you are on.

  • @Gabrieloshiro I wanted a way that doesn’t need to call command on the terminal, because the goal is to capture the serial id as soon as they connect to my server and save it in mysql.

  • @But the code in c will be running on the client, correct?

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.