Page 1 of 2

How can I get the serial number of harddisk ?

Posted: Thu Dec 31, 2009 9:44 am
by pamdx
I want to get the serial number of harddisk or CPU , so that my program can run only in the computer I want to.But I don't know how to get the serial number of harddisk by purebasic .Do somebody tell me how? Thanks very much.

Re: How can I get the harddisk serial ?

Posted: Thu Dec 31, 2009 9:51 am
by eesau
Here is some code that should help get you started.

Re: How can I get the serial number of harddisk ?

Posted: Thu Dec 31, 2009 10:05 am
by pamdx
Thank you very much for your reply ,it's very helpful for me .

Re: How can I get the serial number of harddisk ?

Posted: Thu Dec 31, 2009 12:18 pm
by ts-soft

Code: Select all

Procedure.s GetDriveSerial(Drive.s)
  Protected lpVolumeNameBuffer.s, lpVolumeSerialNumber.l
  If Len(Drive) = 1 : Drive + ":\" : EndIf
  If Right(Drive, 1) <> "\" : Drive + "\" : EndIf
  lpVolumeNameBuffer.s = Space(#MAX_PATH +1)
  GetVolumeInformation_(@Drive, @lpVolumeNameBuffer, #MAX_PATH +1, @lpVolumeSerialNumber, 0,0,0,0)
  ProcedureReturn Hex(PeekW(@lpVolumeSerialNumber + 2) & $FFFF) + "-" + Hex(PeekW(@lpVolumeSerialNumber) & $FFFF)
EndProcedure
Debug GetDriveSerial("C:")

Re: How can I get the serial number of harddisk ?

Posted: Thu Dec 31, 2009 1:36 pm
by UserOfPure
@ts-soft: Your code returns 0000-0000 on my dad's PC. Any ideas?

Re: How can I get the serial number of harddisk ?

Posted: Thu Dec 31, 2009 1:58 pm
by ts-soft
MSDN wrote:To programmatically obtain the hard disk's serial number that the manufacturer assigns
The manager assings 0 :mrgreen:

serial of harddisc, mainboard or cpu is never a way to protect anything.

Re: How can I get the serial number of harddisk ?

Posted: Thu Dec 31, 2009 7:05 pm
by cas
This code returns partition serial number which changes everytime you format that partition.
HDD serial number never changes but it is harder to retrieve, especially if you have disks in RAID...

Re: How can I get the serial number of harddisk ?

Posted: Thu Dec 31, 2009 11:27 pm
by Rook Zimbabwe
AND sometimes it does not work at all!

There have been many other ways discussed in the attempt to restrict runtime or runevents in a program. The consensus is so far the best way involves the registry and a display of the username and email of the person that registered the software.

A remote web hiht server has also been theorized aka microsofts "prrotection" updates...

Long story short... no matter what you do... unless yu have some sort of uncrackable CD Key that you are using EVEN IF you usse something like WIBU you can be hacked/cracked/ or CDSmacked! :mrgreen:

Re: How can I get the serial number of harddisk ?

Posted: Fri Jan 01, 2010 12:53 am
by SFSxOI
Are you looking for the hard drive manufacturer assigned serial number or the volume serial number?

Re: How can I get the serial number of harddisk ?

Posted: Fri Jan 01, 2010 1:02 am
by UserOfPure
cas wrote:This code returns partition serial number which changes everytime you format that partition.
But it also doesn't work reliably, as I mentioned above, returning 0000-0000 instead of the serial number.

Re: How can I get the serial number of harddisk ?

Posted: Fri Jan 01, 2010 5:19 am
by cas
UserOfPure wrote:
cas wrote:This code returns partition serial number which changes everytime you format that partition.
But it also doesn't work reliably, as I mentioned above, returning 0000-0000 instead of the serial number.
How do you know that it is not reliable?
My guess is that you used some custom drive formatting application which instead of generating random serial number and writing it to drive after format just skipped the 'random' part and written only zeros. So this is then valid serial number. 8) Just a guess...

You can open cmd.exe and type "dir c:", on top it will say 'Volume Serial Number is xxxx-xxxx'

Re: How can I get the serial number of harddisk ?

Posted: Fri Jan 01, 2010 7:30 am
by SFSxOI
Concerning RAID drives and others:

WMI offers a few different classes for getting the serial number of drives. There are a few different WMI classes that offer up the drive serial number, Win32_DiskDrive and Win32_PhysicalMedia for example.

The SerialNumber property for the Win32_DiskDrive class description says this:

"SerialNumber
Data type: string
Access type: Read-only

Number allocated by the manufacturer to identify the physical media.

Example: WD-WM3493798728

Windows Server 2003, Windows XP, Windows 2000, and Windows NT 4.0: This property is not available. "

The SerialNumber property for the Win32_PhysicalMedia class description says this:

"SerialNumber
Data type: string
Access type: Read-only

Manufacturer-allocated number used to identify the physical media.

Example: WD-WM3493798728"

So those both are the same in this respect except that one of them can be used in Windows XP and the other can't. Both work in Windows 7, and they worked in Windows Vista also. Heres a quick simple thing using srods COMate for getting the hardware serial numbers of drives using both classes (there are other classes that do this also):

Code: Select all

XIncludeFile "COMatePLUS.pbi"

Procedure DD_Info1()

Define.COMateObject objWMIService, DDInfo
colDDInfo.COMateEnumObject
strComputer.s = "." 

objWMIService = COMate_GetObject("winmgmts:\\" + strComputer + "\root\cimv2", "") 
If objWMIService 
  colDDInfo = objWMIService\CreateEnumeration("ExecQuery('Select * from Win32_PhysicalMedia')")

  If colDDInfo
    DDInfo = colDDInfo\GetNextObject()
    While DDInfo
        
      Debug DDInfo\GetStringProperty("SerialNumber")

      DDInfo\Release()
      DDInfo = colDDInfo\GetNextObject()
    Wend
    colDDInfo\Release()
  EndIf
  objWMIService\Release()
  Else
      MessageRequester("Error", "DDInfo")  
EndIf

EndProcedure


Procedure DD_Info2()

Define.COMateObject objWMIService, DDInfo
colDDInfo.COMateEnumObject
strComputer.s = "." 

objWMIService = COMate_GetObject("winmgmts:\\" + strComputer + "\root\cimv2", "") 
If objWMIService 
  colDDInfo = objWMIService\CreateEnumeration("ExecQuery('Select * from Win32_DiskDrive')")

  If colDDInfo
    DDInfo = colDDInfo\GetNextObject()
    While DDInfo
        
      Debug DDInfo\GetStringProperty("SerialNumber")

      DDInfo\Release()
      DDInfo = colDDInfo\GetNextObject()
    Wend
    colDDInfo\Release()
  EndIf
  objWMIService\Release()
  Else
      MessageRequester("Error", "DDInfo")  
EndIf

EndProcedure

DD_Info1()
DD_Info2()
Both classes produce the same serial number. I didn't provide any identification for which drive is which so thats up to you. If its a raid drive its serial number is given as RAID in some cases, it does for mine.

Notice they say "Manufacturer serial number" and not "Volume Serial Number" like for for each parition. So these are manufacturer assigned, not user assigned by formatting or partitioning. These should not change. I don't think the manufacturer would go to the trouble of assigning a unique serial number to identify something only for the user to have the ability to change it by something so common as a format or even an operating system install (clean installs of Windows basically performs a format of the drive).

Both descriptions for both classes say "Manufacturer allocated number used to identify the physical media." . As for using this a a protection method, hmmmm...well, lets look at that. If I look up my RAID drive in the windows registry I find this (below is exported from the registry):

Code: Select all

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\Scsi\Scsi Port 6\Scsi Bus 0\Target Id 0\Logical Unit Id 0]
"Identifier"="RAID0"
"Type"="DiskPeripheral"
"InquiryData"=hex:00,00,04,02,1f,00,00,00,52,41,49,44,30,00,00,00,00,00,00,00,\
  00,00,00,00,00,00,00,00,00,00,00,00,30,30,30,30
"SerialNumber"="RAID                "
"DeviceIdentifierPage"=hex:00,83,00,34,02,01,00,10,31,39,37,42,32,33,36,33,31,\
  39,37,42,32,33,36,33,01,02,00,10,31,39,37,42,32,33,36,33,31,39,37,42,32,33,\
  36,33,01,03,00,08,20,39,37,42,32,33,36,33
see where it says "SerialNumber"="RAID " ...so personally I would not want to base a software protection that could be overcome by simply getting the serial number from the registry. Some people will say that the hard drive serial numbers that WMI gives are simply from the registry, the only thing that comes close to being in the registry for me for hard drive serial numbers is the RAID drive and it appeared as simply "RAID" for its serial number and this kinda makes sense because I already know that RAID0 striped arrays drives don't have serial numbers if they are RAID0 and my RAID is RAID0, only when the drives in the RAID0 array are no longer in the array can you read a serial number for them. None of the other hard drive (non-raid drives) serial numbers appear in the registry for the rest of the drives on the machine. I'm on Windows 7 Ultimate so there may be something with previous opertaing systems in the registry for serial numbers, I don't remember if there was or not.

But what about non-RAID SATA drives? Yep the above WMI code using COMate gets them. But since i'm focusing manily on RAID here, what about non RAID0 Raid arrays like RAID 5? Take another look at that registry export, the key path - "Scsi\Scsi Port 6\Scsi Bus 0" > ahhhhhh HA! A clue for reading hardware serial numbers for RAID (not RAID0). Everything I can find and read leads back to having to query the Scsi bus and the devices on it to get the actual manufacturer hardware serial numbers of RAID drives. So, can you tell what i'm thinking ? :)

Re: How can I get the serial number of harddisk ?

Posted: Fri Jan 01, 2010 12:37 pm
by UserOfPure
cas wrote:How do you know that it is not reliable?
Because it's been tested.
cas wrote:My guess is that you used some custom drive formatting application
:roll: It's just NTFS from a clean Windows install that I performed myself.
cas wrote:You can open cmd.exe and type "dir c:", on top it will say 'Volume Serial Number is xxxx-xxxx'
As I said, it's been tested, and the cmd.exe shows the serial number, and the code above shows 0000-0000 instead. The code doesn't work with all hard drives, pure and simple.

Re: How can I get the serial number of harddisk ?

Posted: Fri Jan 01, 2010 4:55 pm
by cas
@UserOfPure:
Have you tried investigating where code from ts-soft fails?
You can use GetLastError_() if GetVolumeInformation_() returns 0 and then look on MSDN what that error means.

I can't believe that so simple command from MSDN fails in PB and i bet that 'dir' uses this same api to output drive serial number to cmd.exe. :?

Re: How can I get the serial number of harddisk ?

Posted: Fri Jan 01, 2010 5:34 pm
by SFSxOI
@UserOfPure

ts-soft's code works here fine in Windows 7 Ultimate for getting the drive volume information. i'm formatted NTFS and its gets the info fine. Are you using this with administrator privilages? Are you using this on USB drives (sometimes a USB drive will not report the serial). It does return 0-0 on CD or DVD drives but thats normal.