🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #8472

Re: get disk serial number

#8472
From: zhan wei ming
Date: Wed, 27 Oct 2004 13:59
100 lines
4357 bytes
Andrue,
thanks for your quick response.

what I want to do is to get unique id for a specific computer, to protect my
application.

actually I have downloaded some source code to get the disk serial number
but it seems not working.
the code uses two main APIs: CreateFile and DeviceIoControl, and the second
one always return 0 which means error.

the lines below are extract from the codes.

#define IOCTL_STORAGE_QUERY_PROPERTY   CTL_CODE(IOCTL_STORAGE_BASE, 0x0500,
METHOD_BUFFERED, FILE_ANY_ACCESS)

unsigned long GetDiskSerialNumber()
{
      HANDLE hPhysicalDriveIOCTL = 0;

         //  Try to get a handle to PhysicalDrive IOCTL, report failure
         //  and exit if can't.
        char * driveName = "\\\\.\\PhysicalDrive0";

         //  Windows NT, Windows 2000, Windows XP - admin rights not
required
      hPhysicalDriveIOCTL = CreateFile (driveName, 0,
                               FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
                               OPEN_EXISTING, 0, NULL);

      if (hPhysicalDriveIOCTL != INVALID_HANDLE_VALUE)
      {
         STORAGE_PROPERTY_QUERY query;
         DWORD cbBytesReturned = 0;
         char buffer [10000];

         memset ((void *) & query, 0, sizeof (query));
         query.PropertyId = StorageDeviceProperty;
         query.QueryType = PropertyStandardQuery;

         memset (buffer, 0, sizeof (buffer));

         if ( DeviceIoControl (hPhysicalDriveIOCTL,
IOCTL_STORAGE_QUERY_PROPERTY,
                   & query,
                   sizeof (query),
                   & buffer,
                   sizeof (buffer),
                   & cbBytesReturned, NULL) )
         {
                 STORAGE_DEVICE_DESCRIPTOR * descrip =
(STORAGE_DEVICE_DESCRIPTOR *) & buffer;
                 char serialNumber [1000];

                 strcpy (serialNumber,
                      flipAndCodeBytes ( & buffer [descrip ->
SerialNumberOffset]));
                if (0 == HardDriveSerialNumber [0] &&
                  //  serial number must be alphanumeric
                   //  (but there can be leading spaces on IBM drives)
               (isalnum (serialNumber [0]) || isalnum (serialNumber [19])))
                strcpy (HardDriveSerialNumber, serialNumber);
         }
         else
          {
                DWORD err = GetLastError ();
          }

         memset (buffer, 0, sizeof (buffer));

         CloseHandle (hPhysicalDriveIOCTL);
      }

      ..........
}

any hints are appreciated.

zhan wei ming

"Andrue Cope [TeamB]" <no.spam@not.a.valid.address> wrote in message
news:417fa38f@newsgroups.borland.com...
> zhan wei ming wrote:
>
> > hi there,
> > any body knows how to get serial number (not volume serial number) of
> > a disk, say C drive?
>
> Yes but it's not simple. We issue a SCSI Identify command then
> interpret what comes back. Since Windows 'fakes' SCSI responses for IDE
> drives the resulting data block will often have been manufactured by
> the OS and it's contents aren't neccessarily useful.
>
> There may be a better method using the EnumDevices functions but this
> will only work under Windows 2000 or XP where SetupAPI.DLL is available.
>
> -- 
> Andrue Cope [TeamB]
> [Bicester, Uk]
> http://info.borland.com/newsgroups/guide.html
Technical Details
Message-ID: <417fa9df@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news.glorb.com!newshosting.com!nx02.iad01.newshosting.com!diablo.voicenet.com!38.119.100.83.MISMATCH!news-out1.nntp.be!propagator2-sterling!news-in-sterling.newsfeeds.com!in.nntp.be!caladan.arrakis.es!caladan.landsraad.net!newsgroups.borland.com!not-for-mail
References: <417fa0fd@newsgroups.borland.com> <417fa38f@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,bdae4482adfc73e4
X-Google-Attributes: gid10cc67,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "zhan wei ming" <zhanweiming@hotmail.com>
Newsgroups: borland.public.cppbuilder.nativeapi
References: <417fa0fd@newsgroups.borland.com> <417fa38f@newsgroups.borland.com>
Subject: Re: get disk serial number
Date: Wed, 27 Oct 2004 21:59:43 +0800
Lines: 102
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1409
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409
NNTP-Posting-Host: 211.136.158.34
Message-ID: <417fa9df@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1098885599 211.136.158.34 (27 Oct 2004 06:59:59 -0700)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news.glorb.com!newshosting.com!nx02.iad01.newshosting.com!diablo.voicenet.com!38.119.100.83.MISMATCH!news-out1.nntp.be!propagator2-sterling!news-in-sterling.newsfeeds.com!in.nntp.be!caladan.arrakis.es!caladan.landsraad.net!newsgroups.borland.com!not-for-mail