Solved

Accessing HDD serial #

Posted on 1997-04-14
5
269 Views
Last Modified: 2013-12-03
I need to be able to access the serial number of any given disk drive (ie. the one that comes up every time you do a dir from DOS) from within Windows.  The only functions I could find are in the File Manager extension library, which require the File Manager.  How do you get this info direct from Windows?
0
Comment
Question by:scce
  • 3
  • 2
5 Comments
 
LVL 23

Accepted Solution

by:
chensu earned 100 total points
Comment Utility
The Win32 function GetVolumeInformation() returns this info.

BOOL GetVolumeInformation(
    LPCTSTR lpRootPathName,      // address of root directory of the file system
    LPTSTR lpVolumeNameBuffer,      // address of name of the volume
    DWORD nVolumeNameSize,      // length of lpVolumeNameBuffer
    LPDWORD lpVolumeSerialNumber,      // address of volume serial number
    LPDWORD lpMaximumComponentLength,      // address of system's maximum filename length
    LPDWORD lpFileSystemFlags,      // address of file system flags
    LPTSTR lpFileSystemNameBuffer,      // address of name of file system
    DWORD nFileSystemNameSize       // length of lpFileSystemNameBuffer
   );
0
 
LVL 23

Expert Comment

by:chensu
Comment Utility
My answer is for 32-bit applications only. In case you are writing 16-bit applications, I copied an article from Visaul C++ KBase here.

Retrieving a Disk Volume Serial Number from C  

PSS ID Number: Q69223
Article last modified on 01-24-1995
 
5.10 6.00 6.00a 6.00ax 7.00 | 1.00 1.50
 
MS-DOS                      | WINDOWS
 

----------------------------------------------------------------------
The information in this article applies to:
 
 - The C Run-time (CRT) included with:
 
    - Microsoft C for MS-DOS, versions 5.1, 6.0, 6.0a, and 6.0ax
    - Microsoft C/C++ for MS-DOS, version 7.0
    - Microsoft Visual C++ for Windows, versions 1.0 and 1.5
----------------------------------------------------------------------
 
SUMMARY
=======
 
Beginning with MS-DOS version 4.0, a semi-random 32-bit binary
identification number (ID) is assigned to each disk that MS-DOS
formats. The volume serial number (or ID) is stored at offset 27H to
2AH in the boot sector of each disk.
 
NOTE: code compiled with Visual C++ version 1.5 may yield the following
message from Windows NT:
 
   An applicaion has attempted to directly access the hard disk, which
   cannot be supported. This may cause the application to function
   incorrectly.
 
It provides Terminate and Ignore buttons. If the user is logged on with
Administrative privileges, this will succeed for a FAT partition, else it
fails for a FAT partition. It always fails for an NTFS partition. After
clicking Terminate or Ignore the program returns with the error message
that has been coded (error on int 25).
 
MORE INFORMATION
================
 
The following program illustrates how to retrieve this information:
 
/***************************************************************/
/*                                                             */
/* This program reads the volume serial number (or ID) from    */
/* the boot sector of a specified disk. The DOS interrupt 25   */
/* Absolute Disk Read is used to read in the boot sector.      */
/*                                                             */
/* Note: The volume ID is only implemented from MS-DOS 4.0     */
/*       and later.                                            */
/*                                                             */
/* The output consists of the OEM name and version of the      */
/* disk-formatting program (stored at offset 03H to 0AH in the */
/* boot sector), the disk volume label, and the disk-volume    */
/* serial number.                                              */
/*                                                             */
/***************************************************************/
 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dos.h>
#include <conio.h>
 
char bootsector[1024];
char volume[12];
char ver[9];
char block[10];
 
void main(void)
{
   int ax, _far *p, drive;
   struct find_t fileinfo;
   char filename[13], _far *myvar, _far *q;
   union REGS inregs, outregs;
   struct SREGS segregs;
 
   printf("Enter drive number (0=A,1=B,2=C, ...): ");
   drive = getche() - '0';
 
   /**************************************/
   /* Parameter block for int 25H        */
   /* Bytes    Description               */
   /* -------  -----------               */
   /* 00H-03H  32-bit sector number      */
   /* 04H-05H  Number of sectors to read */
   /* 06H-07H  Offset of buffer          */
   /* 08H-09H  Segment of buffer         */
   /**************************************/
 
   block[0] = block[1] = block[2] = block[3] = 0;
   block[4] = 1;
   block[5] = 0;
 
   myvar = bootsector;
 
   p = (int *)&block[6];
   *p = FP_OFF(myvar);
 
   p = (int *)&block[8];
   *p = FP_SEG(myvar);
 
   q = block;
   inregs.h.al = (char)drive;
   inregs.x.cx = -1;
   inregs.x.bx = FP_OFF(q);
   segregs.ds = FP_SEG(q);
   ax = int86x(0x25, &inregs, &outregs, &segregs);
 
   /*** Error routine ***/
 
   if (outregs.x.cflag)
   {
      printf("\n\nerror on int 25\n");
      printf("this is AX:%04X", ax);
      exit(-1);
   }
 
   /*** Output ***/
 
   printf("\n\nDrive %c\n-------\n\n", drive +'A');
 
   strncpy(ver, &bootsector[3], 8);
   printf("OEM name and version: %s\n", ver);
 
   /* Use _dos_findfirst for the volume label */
 
   filename[0] = (char)(drive + 'A');
   filename[1] = '\0';
   strcat(filename, ":\\*.*");
   if(!_dos_findfirst(filename, _A_VOLID, &fileinfo))
   printf("Volume name         : %s\n", fileinfo.name);
 
   /* Before printing serial number, check if version >= 4.x */
 
   if ((ver[6] == '.') && (ver[5] >= '4') && (ver[5] <= '9'))
      printf("Serial number       : %02X%02X-%02X%02X\n\n",
                              (unsigned char) bootsector[0x2a],
                              (unsigned char) bootsector[0x29],
                              (unsigned char) bootsector[0x28],
                              (unsigned char) bootsector[0x27]);
}
 
Additional reference words: kbinf 5.10 6.00 6.00a 6.00ax 7.00 1.00 1.50
KBCategory: kbprg
KBSubcategory: CRTIss
=============================================================================
Copyright Microsoft Corporation 1995.


 

0
 

Author Comment

by:scce
Comment Utility
To chensu,

Thanks, that was exactly what I was after - just as well you added the comment, since it is in fact Win16 I'm targetting.  Since the solution for this platform involves the low-level DOS interrupt, will it work with Win NT/95? Do you know if it returns a sensible answer for net/removable drives?

You can contact me on scce@ozemail.com.au.

Thanks again,

Stephen
0
 
LVL 23

Expert Comment

by:chensu
Comment Utility
Yes, it will work with Win NT/95 since your program is 16-bit. Win32 does not support most of the DOS interrupt. And it will work with removeable drives such as floppy drives. But I am not sure if it will work with network drives (I think it is unlikely).

Good Luck!

0
 

Author Comment

by:scce
Comment Utility
Thank you!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Event ID 10010 3 36
Multi Table MS Access Query 5 52
Example of WebAPI 7 29
T-SQL Debugging - Temp Object Content 2 49
This tutorial is about how to put some of your C++ program's functionality into a standard DLL, and how to make working with the EXE and the DLL simple and seamless.   We'll be using Microsoft Visual Studio 2008 and we will cut out the noise; that i…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now