How can I get hard disk serial number?

Dear friends,

i'm doing a project in which i need to find the serial number of the hard disk using C,C++ or Java using Windows platform. pls help us

with regards

Comments

  • Im using this in Visual C++ 6.0 (in a MFC program)
    I did something like this when i read Disk serial number, filesystem type and disk label from my drives.
    This code ONLY works for win 9x family, NT uses DeviceIoControl() functions in a different way.
    I did some Cutn Paste from my code so there may be code that is missing and/or some errors.

    #include
    #define VWIN32_DIOC_DOS_IOCTL 1 // INT 21H Function 730X

    // The CPU 32 BIT registers used
    typedef struct _DIOC_REGISTERS
    {
    DWORD reg_EBX;
    DWORD reg_EDX;
    DWORD reg_ECX;
    DWORD reg_EAX;
    DWORD reg_EDI;
    DWORD reg_ESI;
    DWORD reg_Flags;
    } DIOC_REGISTERS, *PDIOC_REGISTERS;

    #pragma pack(1) // Pack data structures to 1-byte boundary (All MS-DOS must)

    // Disk info structure
    typedef struct _MID {
    WORD midInfoLevel; //
    DWORD midSerialNum; // Disk serial number
    BYTE midVolLabel[11]; // Disk label
    BYTE midFileSysType[8]; // Filesystem FAT12, 16, 32
    } MID, *PMID;
    #pragma pack()

    /****************************************************************
    GetDiskInfo: Gets information about the disk media.

    Arguments:
    hDev: Handle to VWIN32.vxd.
    drive: Disk to get information about (A:=1, B:=2, C:=3, ...)
    mid: MID structure, to fill with disk information.

    Return value: return 0 if OK.

    ****************************************************************/
    int GetDiskInfo(HANDLE hDev, int drive, MID &mid)
    {
    DIOC_REGISTERS r_reg;
    BOOL fResult;
    DWORD cb;

    r_reg.reg_EAX = 0x440D; // IOCTL for block devices
    r_reg.reg_EBX = drive; // zero-based drive identifier
    r_reg.reg_ECX = 0x4866; // Get Media ID command
    r_reg.reg_EDX = (DWORD) ∣ // receives media identifier //information
    r_reg.reg_Flags = 0x0001; // assume error (carry flag is // set)

    fResult = DeviceIoControl(hDev, VWIN32_DIOC_DOS_IOCTL,
    &r_reg, sizeof(reg),
    &r_reg, sizeof(reg),&cb,
    0);

    if (!fResult || (r_reg.reg_Flags & 0x0001))
    return r_reg.reg_Flags;

    return 0;
    }


    //Get Handle To Device ONLY win 9x family

    HANDLE hDev = CreateFile("\\.\vwin32",
    GENERIC_READ|GENERIC_WRITE,
    FILE_SHARE_READ |FILE_SHARE_WRITE , NULL, OPEN_EXISTING,
    FILE_FLAG_DELETE_ON_CLOSE|FILE_ATTRIBUTE_ NORMAL, NULL);


    (use: CloseHandle(hDev); when finished with the handdle)


  • hi kurant,
    thank you very much for your effort to answer my question. i'm really very thankful to you. But i'm very unfaimiliar to VC++ and i dint understand the handles you have used in this program. Can u please send me the full code of the program(if possible .exe file) so that i can use without any overhead. my email id is harry_sai@reduffmail.com.

    Thanks in advance,

    with best regards

    harry_sai
  • hi kurant,
    thank you very much for your effort to answer my question. i'm really very thankful to you. But i'm very unfaimiliar to VC++ and i dint understand the handles you have used in this program. Can u please send me the full code of the program(if possible .exe file) so that i can use without any overhead. my email id is harry_sai@rediffmail.com.

    Thanks in advance,

    with best regards

    harry_sai
  • : Im using this in Visual C++ 6.0 (in a MFC program)
    : I did something like this when i read Disk serial number, filesystem type and disk label from my drives.
    : This code ONLY works for win 9x family, NT uses DeviceIoControl() functions in a different way.
    : I did some Cutn Paste from my code so there may be code that is missing and/or some errors.
    :
    : #include
    : #define VWIN32_DIOC_DOS_IOCTL 1 // INT 21H Function 730X
    :
    : // The CPU 32 BIT registers used
    : typedef struct _DIOC_REGISTERS
    : {
    : DWORD reg_EBX;
    : DWORD reg_EDX;
    : DWORD reg_ECX;
    : DWORD reg_EAX;
    : DWORD reg_EDI;
    : DWORD reg_ESI;
    : DWORD reg_Flags;
    : } DIOC_REGISTERS, *PDIOC_REGISTERS;
    :
    : #pragma pack(1) // Pack data structures to 1-byte boundary (All MS-DOS must)
    :
    : // Disk info structure
    : typedef struct _MID {
    : WORD midInfoLevel; //
    : DWORD midSerialNum; // Disk serial number
    : BYTE midVolLabel[11]; // Disk label
    : BYTE midFileSysType[8]; // Filesystem FAT12, 16, 32
    : } MID, *PMID;
    : #pragma pack()
    :
    : /****************************************************************
    : GetDiskInfo: Gets information about the disk media.
    :
    : Arguments:
    : hDev: Handle to VWIN32.vxd.
    : drive: Disk to get information about (A:=1, B:=2, C:=3, ...)
    : mid: MID structure, to fill with disk information.
    :
    : Return value: return 0 if OK.
    :
    : ****************************************************************/
    : int GetDiskInfo(HANDLE hDev, int drive, MID &mid)
    : {
    : DIOC_REGISTERS r_reg;
    : BOOL fResult;
    : DWORD cb;
    :
    : r_reg.reg_EAX = 0x440D; // IOCTL for block devices
    : r_reg.reg_EBX = drive; // zero-based drive identifier
    : r_reg.reg_ECX = 0x4866; // Get Media ID command
    : r_reg.reg_EDX = (DWORD) ∣ // receives media identifier //information
    : r_reg.reg_Flags = 0x0001; // assume error (carry flag is // set)
    :
    : fResult = DeviceIoControl(hDev, VWIN32_DIOC_DOS_IOCTL,
    : &r_reg, sizeof(reg),
    : &r_reg, sizeof(reg),&cb,
    : 0);
    :
    : if (!fResult || (r_reg.reg_Flags & 0x0001))
    : return r_reg.reg_Flags;
    :
    : return 0;
    : }
    :
    :
    : //Get Handle To Device ONLY win 9x family
    :
    : HANDLE hDev = CreateFile("\\.\vwin32",
    : GENERIC_READ|GENERIC_WRITE,
    : FILE_SHARE_READ |FILE_SHARE_WRITE , NULL, OPEN_EXISTING,
    : FILE_FLAG_DELETE_ON_CLOSE|FILE_ATTRIBUTE_ NORMAL, NULL);
    :
    :
    : (use: CloseHandle(hDev); when finished with the handdle)
    :
    :
    :

    i'm still new to c++, but i've done many c programs so i don't know much about MFC myself, but if you are using win9x/ms dos/console, you can try:

    system("vol c: > vol.txt"); /* where you can change c: with any drive you want to use */

    then you can have file i/o manipualation to get the volume label and serial number of the drive.


    weapon__x



  • : : Im using this in Visual C++ 6.0 (in a MFC program)
    : : I did something like this when i read Disk serial number, filesystem type and disk label from my drives.
    : : This code ONLY works for win 9x family, NT uses DeviceIoControl() functions in a different way.
    : : I did some Cutn Paste from my code so there may be code that is missing and/or some errors.
    : :
    : : #include
    : : #define VWIN32_DIOC_DOS_IOCTL 1 // INT 21H Function 730X
    : :
    : : // The CPU 32 BIT registers used
    : : typedef struct _DIOC_REGISTERS
    : : {
    : : DWORD reg_EBX;
    : : DWORD reg_EDX;
    : : DWORD reg_ECX;
    : : DWORD reg_EAX;
    : : DWORD reg_EDI;
    : : DWORD reg_ESI;
    : : DWORD reg_Flags;
    : : } DIOC_REGISTERS, *PDIOC_REGISTERS;
    : :
    : : #pragma pack(1) // Pack data structures to 1-byte boundary (All MS-DOS must)
    : :
    : : // Disk info structure
    : : typedef struct _MID {
    : : WORD midInfoLevel; //
    : : DWORD midSerialNum; // Disk serial number
    : : BYTE midVolLabel[11]; // Disk label
    : : BYTE midFileSysType[8]; // Filesystem FAT12, 16, 32
    : : } MID, *PMID;
    : : #pragma pack()
    : :
    : : /****************************************************************
    : : GetDiskInfo: Gets information about the disk media.
    : :
    : : Arguments:
    : : hDev: Handle to VWIN32.vxd.
    : : drive: Disk to get information about (A:=1, B:=2, C:=3, ...)
    : : mid: MID structure, to fill with disk information.
    : :
    : : Return value: return 0 if OK.
    : :
    : : ****************************************************************/
    : : int GetDiskInfo(HANDLE hDev, int drive, MID &mid)
    : : {
    : : DIOC_REGISTERS r_reg;
    : : BOOL fResult;
    : : DWORD cb;
    : :
    : : r_reg.reg_EAX = 0x440D; // IOCTL for block devices
    : : r_reg.reg_EBX = drive; // zero-based drive identifier
    : : r_reg.reg_ECX = 0x4866; // Get Media ID command
    : : r_reg.reg_EDX = (DWORD) ∣ // receives media identifier //information
    : : r_reg.reg_Flags = 0x0001; // assume error (carry flag is // set)
    : :
    : : fResult = DeviceIoControl(hDev, VWIN32_DIOC_DOS_IOCTL,
    : : &r_reg, sizeof(reg),
    : : &r_reg, sizeof(reg),&cb,
    : : 0);
    : :
    : : if (!fResult || (r_reg.reg_Flags & 0x0001))
    : : return r_reg.reg_Flags;
    : :
    : : return 0;
    : : }
    : :
    : :
    : : //Get Handle To Device ONLY win 9x family
    : :
    : : HANDLE hDev = CreateFile("\\.\vwin32",
    : : GENERIC_READ|GENERIC_WRITE,
    : : FILE_SHARE_READ |FILE_SHARE_WRITE , NULL, OPEN_EXISTING,
    : : FILE_FLAG_DELETE_ON_CLOSE|FILE_ATTRIBUTE_ NORMAL, NULL);
    : :
    : :
    : : (use: CloseHandle(hDev); when finished with the handdle)
    : :
    : :
    : :
    :
    : i'm still new to c++, but i've done many c programs so i don't know
    : much about MFC myself, but if you are using win9x/ms dos/console,
    : you can try:
    :
    : system("vol c: > vol.txt"); /* where you can change c: with any
    : drive you want to use */
    :
    : then you can have file i/o manipualation to get the volume label and
    : serial number of the drive.
    :
    :
    : weapon__x
    :
    :
    : Hi;
    how did you know how to write this code?
    I'm very intrested of able to write like you!
    Please reply
    :

This discussion has been closed.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories