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;
}
/*
* 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?
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.