2008-11-07 VB 如何取得硬碟序號 12842 0 VB / VBA 摘要:VB 如何取得硬碟序號 VB 如何取得硬碟序號'自訂型態 Private Type IDEREGS bFeaturesReg As Byte bSectorCountReg As Byte bSectorNumberReg As Byte bCylLowReg As Byte bCylHighReg As Byte bDriveHeadReg As Byte bCommandReg As Byte bReserved As Byte End Type Private Type DRIVERSTATUS bDriveError As Byte bIDEStatus As Byte bReserved(1 To 2) As Byte dwReserved(1 To 2) As Long End Type Private Type SENDCMDOUTPARAMS cBufferSize As Long DStatus As DRIVERSTATUS bBuffer(1 To 512) As Byte End Type Private Type SENDCMDINPARAMS cBufferSize As Long irDriveRegs As IDEREGS bDriveNumber As Byte bReserved(1 To 3) As Byte dwReserved(1 To 4) As Long End Type 'API宣告 Private Declare Function CreateFileA Lib "Kernel32" _ (ByVal lpFileName As String, _ ByVal dwDesiredAccess As Long, _ ByVal dwShareMode As Long, _ ByVal lpSecurityAttributes As Long, _ ByVal dwCreationDisposition As Long, _ ByVal dwFlagsAndAttributes As Long, _ ByVal hTemplateFile As Long) As Long Private Declare Sub RtlZeroMemory Lib "Kernel32" _ (dest As Any, ByVal numBytes As Long) Private Declare Function DeviceIoControl Lib "Kernel32" _ (ByVal hDevice As Long, _ ByVal dwIoControlCode As Long, _ lpInBuffer As Any, _ ByVal nInBufferSize As Long, _ lpOutBuffer As Any, _ ByVal nOutBufferSize As Long, _ lpBytesReturned As Long, _ ByVal lpOverlapped As Long) As Long Private Declare Function CloseHandle Lib "Kernel32" _ (ByVal hObject As Long) As Long Private Declare Sub RtlMoveMemory Lib "Kernel32" _ (Destination As Any, Source As Any, ByVal Length As Long) Private Sub Command1_Click() MsgBox "硬碟序號 : " & Get_HD_SNo(0) ' 第一顆硬碟 MsgBox "硬碟序號 : " & Get_HD_SNo(1) ' 第二顆硬碟 MsgBox "硬碟序號 : " & Get_HD_SNo(2) ' 第三顆硬碟 .. End Sub ' 取得硬碟序號 Private Function Get_HD_SNo(DrvIdx As Byte) As String Dim ParaIn As SENDCMDINPARAMS Dim ParaOut As SENDCMDOUTPARAMS Dim Sno As String Dim h As Long Dim intLp As Integer If Len(Environ("OS")) > 0 Then h = CreateFileA("\\.\PhysicalDrive" & DrvIdx, -1073741824, 3, 0, 3, 0, 0) Else h = CreateFileA("\\.\Smartvsd", 0, 0, 0, 1, 0, 0) End If If h = 0 Then Exit Function RtlZeroMemory ParaIn, Len(ParaIn) RtlZeroMemory ParaOut, Len(ParaOut) With ParaIn .bDriveNumber = DrvIdx .cBufferSize = 512 With .irDriveRegs .bDriveHeadReg = IIf(DrvIdx And 1, 176, 160) .bCommandReg = 236 .bSectorCountReg = 1 .bSectorNumberReg = 1 End With End With DeviceIoControl h, 508040, ParaIn, Len(ParaIn), ParaOut, Len(ParaOut), 0, 0 For intLp = 21 To 40 Step 2 If ParaOut.bBuffer(intLp + 1) = 0 Then Exit For Sno = Sno & Chr(ParaOut.bBuffer(intLp + 1)) If ParaOut.bBuffer(intLp) > 0 Then Sno = Sno & Chr(ParaOut.bBuffer(intLp)) Next CloseHandle h Get_HD_SNo = Trim(Sno) End Function 以上為 VB6 語法 , VB.Net 則需改寫 其他相關參考 :如何取得 硬碟 及 主機板 序號C# 取得 硬碟機 序號 ( 物理 / 邏輯 磁碟) 回首頁