2024年4月11日发(作者:)

获取硬盘序列号的代码

网上有很多获取硬盘序列号的代码,但大部分都只能取得逻辑分

区的序列号,而不能取得硬盘的物理序列号。我们知道,逻辑分区的

序列号是FORMAT时产生的,重新FORMAT将会改变。这对于要求

唯一识别码的程式来说是致命的。这段代码利用API可以取得物理硬

盘的很多特性,包括硬盘出厂时的唯一编号。这段代码我是从CSDN

论坛获得,原作者不详。我将其稍做修改和增添,独立出两个常用的

函数:GetDiskVolume 、GetHardDiskInfo ......

'======================================

======

'模块功能: 取得硬盘的信息

'编 程:来自互联网,阿勇修改

'更新日期:2005/7/8

'调用方法:

' GetDiskVolume() 取得逻辑盘的序列号

' GetHardDiskInfo() 取得物理盘的型号或序列号

'======================================

======

Private Const MAX_IDE_DRIVES As Long = 4 ' Max number

of drives assuming primary/secondary, master/slave topology

Private Const READ_ATTRIBUTE_BUFFER_SIZE As Long = 512

Private Const IDENTIFY_BUFFER_SIZE As Long = 512

Private Const READ_THRESHOLD_BUFFER_SIZE As Long =

512

Private Const DFP_GET_VERSION As Long = &H74080

Private Const DFP_SEND_DRIVE_COMMAND As Long =

&H7C084

Private Const DFP_RECEIVE_DRIVE_DATA As Long =

&H7C088

Private Type GETVERSIONOUTPARAMS

bVersion As Byte ' Binary driver version.

bRevision As Byte ' Binary driver revision.

bReserved As Byte ' Not used.

bIDEDeviceMap As Byte ' Bit map of IDE devices.

fCapabilities As Long ' Bit mask of driver capabilities.

dwReserved(3) As Long ' For future use.

End Type

Private Const CAP_IDE_ID_FUNCTION As Long = 1 ' ATA ID

command supported

Private Const CAP_IDE_ATAPI_ID As Long = 2 ' ATAPI ID

command supported

Private Const CAP_IDE_EXECUTE_SMART_FUNCTION As

Long = 4 ' SMART commannds supported

Private Type IDEREGS

bFeaturesReg As Byte ' Used for specifying SMART

"commands".

bSectorCountReg As Byte ' IDE sector count register

bSectorNumberReg As Byte ' IDE sector number register

bCylLowReg As Byte ' IDE low order cylinder value

bCylHighReg As Byte ' IDE high order cylinder value

bDriveHeadReg As Byte ' IDE drive/head register

bCommandReg As Byte ' Actual IDE command.

bReserved As Byte ' reserved for future use. Must be zero.

End Type