问:vb有没有办法通过API得到硬盘的物理序列号(不是逻辑序列号)?
物理序列号指硬盘被格式化后序列号不会改变.

解决方案 »

  1.   

    你可以使用WMIhttp://vbnet.mvps.org/code/wmi/index.html
      

  2.   

    '转贴
    ’------------------------源代码开始--------------------------------
    Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, lpSecurityAttributes As Long, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) 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, lpOverlapped As Long) As Long
    Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)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 TypeType InParams
          cBufferSize  As Long
          irDriveRegs  As IDERegs
          bDriveNumber As Byte
          bReserved(0 To 19)   As Byte
    End Type
    Dim inbuff As InParams
    Dim outbuff(0 To 528) As ByteDim SerialNumber As String
    Function ChangeByteOrder(s As Variant, nLen As Long)
    Dim i As Long
    Dim pi As Long
    pi = 0
    For i = 0 To nLen / 2 - 1
        c = s(pi)
        s(pi) = s(pi + 1)
        s(pi + 1) = c
        pi = pi + 2
    NextEnd FunctionDim SerialNumber As StringSub Main()
    Dim nBytes As Long
    Dim nRet As Long
    Dim hVxD As LongDim BSerialNumber(0 To 19) As Byteinbuff.cBufferSize = 512
    inbuff.bDriveNumber = 0
    inbuff.irDriveRegs.bSectorCountReg = 1
    inbuff.irDriveRegs.bSectorNumberReg = 1
    inbuff.irDriveRegs.bCylHighReg = 0
    inbuff.irDriveRegs.bCylLowReg = 0
    inbuff.irDriveRegs.bDriveHeadReg = &HA0
    inbuff.irDriveRegs.bCommandReg = &HEC
    hVxD = CreateFile("\\.\smartvsd", 0, 0, 0, 1, 0, 0)
    nRet = DeviceIoControl(hVxD, &H7C088, inbuff, Len(inbuff) - 1, outbuff(0), 528, nBytes, 0)
    If nRet > 0 Then
        CopyMemory BSerialNumber(0), outbuff(36), 20
        SerialNumber = StrConv(BSerialNumber, vbUnicode)
        SerialNumber = Trim(SerialNumber)
    End If
    Call CloseHandle(hVxD)MsgBox SerialNumber
    End Sub'----------代码结束---------------------------------
      

  3.   

    小新:
        显示的是空的,代码如下:
    _____________________
    Option ExplicitPrivate Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, lpSecurityAttributes As Long, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) 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, lpOverlapped As Long) As Long
    Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)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 TypePrivate Type InParams
          cBufferSize  As Long
          irDriveRegs  As IDERegs
          bDriveNumber As Byte
          bReserved(0 To 19)   As Byte
    End TypeDim inbuff As InParams
    Dim outbuff(0 To 528) As Byte
    Dim SerialNumber As StringPrivate Function ChangeByteOrder(s As Variant, nLen As Long)
        Dim i As Long
        Dim pi As Long
        pi = 0
        For i = 0 To nLen / 2 - 1
            c = s(pi)
            s(pi) = s(pi + 1)
            s(pi + 1) = c
            pi = pi + 2
        Next
    End FunctionPrivate Sub Main()
        Dim nBytes As Long
        Dim nRet As Long
        Dim hVxD As Long
        
        Dim BSerialNumber(0 To 19) As Byte
        
        inbuff.cBufferSize = 512
        inbuff.bDriveNumber = 0
        inbuff.irDriveRegs.bSectorCountReg = 1
        inbuff.irDriveRegs.bSectorNumberReg = 1
        inbuff.irDriveRegs.bCylHighReg = 0
        inbuff.irDriveRegs.bCylLowReg = 0
        inbuff.irDriveRegs.bDriveHeadReg = &HA0
        inbuff.irDriveRegs.bCommandReg = &HEC
        hVxD = CreateFile("\\.\smartvsd", 0, 0, 0, 1, 0, 0)
        nRet = DeviceIoControl(hVxD, &H7C088, inbuff, Len(inbuff) - 1, outbuff(0), 528, nBytes, 0)
        If nRet > 0 Then
            CopyMemory BSerialNumber(0), outbuff(36), 20
            SerialNumber = StrConv(BSerialNumber, vbUnicode)
            SerialNumber = Trim(SerialNumber)
        End If
        Call CloseHandle(hVxD)
        
        Debug.Print SerialNumber
    End SubPrivate Sub Command1_Click()
        Call Main
    End Sub