How can get Hard Disk Serial Number or Processor ID

Hello.
How can get Hard Disk Serial Number or Processor ID with c, c++ or wxWidgets?
I am looking for code and i dont find any.
Any idea?

Thanks
Jim
closed account (48bpfSEw)
look here: http://stackoverflow.com/questions/4084402/get-hard-disk-serial-number
Thanks for reply.
This code is for windows and Visual Studio.
But i am in Mint and use CodeBlocks and C++ 11.


1
2
3
4
5
6
7
8
9
10
11
    ManagementObjectSearcher searcher = new
    ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");

   foreach(ManagementObject wmi_HD in searcher.Get())
   {
    HardDrive hd = new HardDrive();
    hd.Model = wmi_HD["Model"].ToString();
    hd.Type  = wmi_HD["InterfaceType"].ToString(); 
    hdCollection.Add(hd);
   }


Get the Serial Number

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");

   int i = 0;
   foreach(ManagementObject wmi_HD in searcher.Get())
   {
    // get the hard drive from collection
    // using index
    HardDrive hd = (HardDrive)hdCollection[i];

    // get the hardware serial no.
    if (wmi_HD["SerialNumber"] == null)
     hd.SerialNo = "None";
    else
     hd.SerialNo = wmi_HD["SerialNumber"].ToString();

    ++i;
   }
Last edited on
Thanks for reply.

http://www.cyberciti.biz/faq/find-hard-disk-hardware-specs-on-linux/ ==>>This is for terminal.

http://forums.codeguru.com/showthread.php?388416-Reading-hard-disk-cpu-serial-number
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/*
* didkinfo.c
* It gets the hard disk information in this case serial no.
* It uses ioctl() system call
*/

#include <iostream>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/types.h>
#include <linux/hdreg.h>
#include <linux/fcntl.h>
#include <unistd.h>

int main()
{
int fd,err,i;

/* structure to get disk information and
* returned by HDIO_GET_IDENTITY, as per ANSI ATA2 rev.2f spec
*/
struct hd_driveid hd;

/* open the device */
if( (fd=open("/dev/hda", O_RDONLY ) ) < 0 ) // ==>  ‘open’ was not declared in this scope
perror("Device Open Error");

/* get required info */

if( (err = ioctl(fd,HDIO_GET_IDENTITY,&hd) ) < 0)
perror("IOCTL err");
else
printf("Serial No = %s\n",hd.serial_no);

return (0);
}


Is good, but gives error. ‘open’ was not declared in this scope|


Third gives Must be root to use, and if i am not root?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/hdreg.h>
#include <unistd.h>


int main(int argc,
     char *argv[])
{
    static struct hd_driveid hd;
    int fd;

    if (geteuid() >  0) {
        printf("ERROR: Must be root to use\n"); //===>> Must be root to use
        exit(1);
    }

    if ((fd = open(argv[1], O_RDONLY|O_NONBLOCK)) < 0) {
        printf("ERROR: Cannot open device %s\n", argv[1]);
        exit(1);
    }

    if (!ioctl(fd, HDIO_GET_IDENTITY, &hd)) {
        printf("Hard Disk Model: %.40s\n", hd.model);
        printf("  Serial Number: %.20s\n", hd.serial_no);
    } else if (errno == -ENOMSG) {
        printf("No hard disk identification information available\n");
    } else {
        perror("ERROR: HDIO_GET_IDENTITY");
        exit(1);
    }

    exit(0);
}


And if disable getuid bransh gives ERROR: Cannot open device Must be root to open.
But in a program how is that posible?

Last edited on
Try using the terminal version and use pipes to get the output. See this page on how to use pipes:

http://www.sw-at.com/blog/2011/03/23/popen-execute-shell-command-from-cc/

Edit: Here's another page that shows creating a function to encapsulate getting the output from an arbitrary command. Other than the horrendous complete lack of indentation, I like it. And it's reusable.

https://www.jeremymorgan.com/tutorials/c-programming/how-to-capture-the-output-of-a-linux-command-in-c/
Last edited on
Topic archived. No new replies allowed.