高手们可不可以给我一段读取硬盘序列号的示例代码?要求在XP系统和9X系列下都有效。最好还能读取硬盘型号,谢谢。

解决方案 »

  1.   

    Option Explicit
        '取得硬盘的序列号
    Private Declare Function GetVolumeInformation Lib "kernel32" Alias "GetVolumeInformationA" (ByVal lpRootPathName As String, ByVal lpVolumeNameBuffer As String, ByVal nVolumeNameSize As Long, lpVolumeSerialNumber As Long, lpMaximumComponentLength As Long, lpFileSystemFlags As Long, ByVal lpFileSystemNameBuffer As String, ByVal nFileSystemNameSize As Long) As LongPrivate Sub Form_Load()
        Me.AutoRedraw = True
        Me.Print GetHardDiskSrNo
    End Sub
    '得到硬盘序列号,加密用。
    Public Function GetHardDiskSrNo() As Long
        Dim lngSerial As Long, strVName As String, strFSName As String
        'Create buffers
        strVName = String$(255, Chr$(0))
        strFSName = String$(255, Chr$(0))
        'Get the volume information
        GetVolumeInformation "C:\", strVName, 255, lngSerial, 0, 0, strFSName, 255
        GetHardDiskSrNo = lngSerial
    End Function
      

  2.   

    fishzone(阿愚)的方法是获取C盘的分区序列号,而且在format以C盘,或fdisk后,这个序列号会改变。这种方法不适合做加密。用VB获取硬盘序列号:难。  :(
      

  3.   

    Option Explicit
    Public Type SECURITY_ATTRIBUTES
            nLength As Long
            lpSecurityDescriptor As Long
            bInheritHandle As Long
    End TypePublic Type OVERLAPPED
            Internal As Long
            InternalHigh As Long
            offset As Long
            OffsetHigh As Long
            hEvent As Long
    End TypePublic Declare Function CreateFile Lib "KERNEL32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, lpSecurityAttributes As SECURITY_ATTRIBUTES, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
    Public Declare Function CloseHandle Lib "KERNEL32" (ByVal hObject As Long) As Long
    Public 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 OVERLAPPED) As LongPublic Const CREATE_NEW = 1
    Public Const CREATE_ALWAYS = 2
    Public Const OPEN_EXISTING = 3
    Public Const OPEN_ALWAYS = 4
    Public Const TRUNCATE_EXISTING = 5Public Const DFP_GET_VERSION = &H74080
    Public Const DFP_SEND_DRIVE_COMMAND = &H7C084
    Public Const DFP_RECEIVE_DRIVE_DATA = &H7C088Type TypeGETVERSIONOUTPARAMS
      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 TypeType TypeIDEREGS
      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 TypeType TypeSENDCMDINPARAMS
      cBufferSize As Long  '  // Buffer size in bytes
      irDriveRegs As TypeIDEREGS  '  // Structure with drive register values.
      bDriveNumber As Byte  '  // Physical drive number to send
            'command to (0,1,2,3).
      bReserved(2) As Byte  '  // Reserved for future expansion.
      dwReserved(3) As Long  '  // For future use.
     '//BYTE  bBuffer[1];   // Input buffer.
    End Type
    Type TypeDRIVERSTATUS
      bDriverError As Byte  '  // Error code from driver,
      bIDEStatus As Byte  '   // Contents of IDE Error register.
      bReserved(1) As Byte  '  // Reserved for future expansion.
      dwReserved(1) As Long  '  // Reserved for future expansion.
    End TypeType TypeSENDCMDOUTPARAMS
      cBufferSize As Long  '  // Size of bBuffer in bytes
      DriverStatus As TypeDRIVERSTATUS  '  // Driver status structure.
      bBuffer(511) As Byte  '   // Buffer of arbitrary length
    End TypeType TypeIDSECTOR
      wGenConfig As Integer
      wNumCyls As Integer
      wReserved As Integer
      wNumHeads As Integer
      wBytesPerTrack As Integer
      wBytesPerSector As Integer
      wSectorsPerTrack As Integer
      wVendorUnique(2) As Integer
      sSerialNumber As String * 20
      wBufferType As Integer
      wBufferSize As Integer
      wECCSize As Integer
      sFirmwareRev As String * 8
      sModelNumber As String * 40
      wMoreVendorUnique As Integer
      wDoubleWordIO As Integer
      wCapabilities As Integer
      wReserved1 As Integer
      wPIOTiming As Integer
      wDMATiming As Integer
      wBS As Integer
      wNumCurrentCyls As Integer
      wNumCurrentHeads As Integer
      wNumCurrentSectorsPerTrack As Integer
      ulCurrentSectorCapacity As Long
      wMultSectorStuff As Integer
      ulTotalAddressableSectors As Long
      wSingleWordDMA As Integer
      wMultiWordDMA As Integer
      bReserved(127) As Byte
    End TypePrivate Function BytesToStr(Bytes() As Byte, ByteCount As Integer, Optional From As Integer = 0) As String
      Dim s As String
      Dim i As Integer
      s = ""
      For i = 1 To ByteCount Step 2
        If i = ByteCount Then
          s = s + Chr(Bytes(i + From))
        Else
          s = s + Chr(Bytes(i + 1 + From))
          s = s + Chr(Bytes(i + From))
        End If
      Next i
      BytesToStr = s
    End Function'¶ÁÈ¡Ó²ÅÌÓ²¼þ²ÎÊý
    Public Function GetHDINFO(Optional DriverNum As Integer = 0) As TypeIDSECTOR
      Dim SecAtt As SECURITY_ATTRIBUTES
      Dim Ovlp As OVERLAPPED
      Dim vers As TypeGETVERSIONOUTPARAMS
      Dim cIn As TypeSENDCMDINPARAMS
      Dim cOut As TypeSENDCMDOUTPARAMS
      Dim phdinfo As TypeIDSECTOR
      Dim h As Long
      Dim i As Long
      
      h = CreateFile("\\.\Smartvsd", 0, 0, SecAtt, CREATE_NEW, 0, 0)
      If h = 0 Then
        GetHDINFO = phdinfo
        Exit Function
      End If
      If DeviceIoControl(h, DFP_GET_VERSION, 0, 0, vers, Len(vers), i, Ovlp) = 0 Then
        GoTo Exit_Sub
      End If
      If (vers.fCapabilities And 1) = 0 Then
        GoTo Exit_Sub
      End If
      If (DriverNum And 1) <> 0 Then
        cIn.irDriveRegs.bDriveHeadReg = &HB0
      Else
        cIn.irDriveRegs.bDriveHeadReg = &HA0
      End If
      If (vers.fCapabilities And (16 / (2 ^ DriverNum))) <> 0 Then
        GoTo Exit_Sub
      Else
        cIn.irDriveRegs.bCommandReg = &HEC
      End If
      cIn.bDriveNumber = DriverNum
      cIn.irDriveRegs.bSectorCountReg = 1
      cIn.irDriveRegs.bSectorNumberReg = 1
      cIn.cBufferSize = 512
      If DeviceIoControl(h, DFP_RECEIVE_DRIVE_DATA, cIn, Len(cIn), cOut, Len(cOut), i, Ovlp) = 0 Then
            GoTo Exit_Sub
      End If
      '&sup2;&Icirc;&Ecirc;&yacute;&acute;&laquo;&micro;&Yacute;
      '×&cent;:&Ouml;&raquo;&acute;&laquo;&micro;&Yacute;&Aacute;&Euml; &sup2;ú&AElig;·&Atilde;&ucirc;&sup3;&AElig;,&ETH;&Iacute;&ordm;&Aring;±à&Acirc;&euml;,&ETH;ò&Aacute;&ETH;&ordm;&Aring; &Egrave;&yacute;&cedil;&ouml;&sup2;&Icirc;&Ecirc;&yacute;
      phdinfo.sModelNumber = BytesToStr(cOut.bBuffer, 40, 53)
      phdinfo.sFirmwareRev = BytesToStr(cOut.bBuffer, 8, 45)
      phdinfo.sSerialNumber = BytesToStr(cOut.bBuffer, 20, 19)
    Exit_Sub:
      CloseHandle h
      GetHDINFO = phdinfo
    End Function
      

  4.   

    Private Sub Command1_Click()
      Dim phdinfo As TypeIDSECTOR
      phdinfo = GetHDINFO()
      Text1 = Trim(phdinfo.sModelNumber)
      Text2 = Trim(phdinfo.sFirmwareRev)
      Text3 = Trim(phdinfo.sSerialNumber)
    End Sub
      

  5.   

    text1:产品名称
    text2:型号编码
    text3:序列号
      

  6.   

    转摘
    《提取使用者机器的硬盘序列号》  新建一模块文件,并将如下声明的语句和常量添加到module1.bas模块中:  declare function getvolumeinformation lib "kernel32" alias "getvolumeinformationa"   (byval lprootpathname as string, byval lpvolumenamebuffer as string, byval   nvolumenamesize as long, lpvolumeserialnumber as long, lpmaximumcomponentlength as   long, lpfilesystemflags as long, byval lpfilesystemnamebuffer as string, byval   nfilesystemnamesize as long) as long  global getval as long  编程时需注意的是要将声明语句写在同一行中。
      窗体设置
      在form1上添加2个文本框,name属性分别设置为text1、text2;再添加1个按钮,name属性设置为command1。
      添加代码
      将如下程序代码添加到form1的form1_load事件中:  private sub form_load()  dim tempstr1 as string * 256  dim tempstr2 as string * 256  dim templon1 as long  dim templon2 as long  ………  ‘读取是否注册的信息,如何控制这里不再说明  ………  call getvolumeinformation("c:\", tempstr1, 256, getval, templon1, templon2,   tempstr2, 256)  text1.text = getval ‘提取本机c盘的序列号至文本框一  end sub  将如下程序代码添加到command1的command1_click事件中:  private sub command1_click()  if text2 〈〉 cstr(getval) then   msgbox "注册码不正确,请认真检查输入是否正确。"  else  msgbox "你已经成功注册,请重新启动本软件。"  ………  (将正确注册的信息写入,使软件功能以后不受限制。具体方法依个人爱好进行设置。)  ………  end if  end sub
      

  7.   

    http://www.dapha.net/down/list.asp?id=1886
    源码
      

  8.   

    hi128(斯文)的方法我在XP下试了,点击COMMAND1后无反应……
      

  9.   

    在vb不可能做到这些的。
    除非你用汇编或者c++的dll调用底层的东西。
      

  10.   

    to hi128(斯文):
    那个"\\.\Smartvsd"是什么东西啊,我的CreateFile 返回是-1,然后DeviceIoControl(DFP_GET_VERSION, ...) 的返回值就是0我按照Platform SDK中的描述把它改成了"\\.\PHYSICALDRIVE0",CreateFile 的返回值看上去是可以的,但DeviceIoControl(DFP_GET_VERSION, ...) 的返回值还是0,为什么呀
      

  11.   

    我查了NT DDK你的
    Public Const DFP_GET_VERSION = &H74080
    Public Const DFP_SEND_DRIVE_COMMAND = &H7C084
    Public Const DFP_RECEIVE_DRIVE_DATA = &H7C088是对应于MSDN 中的
    SMART_GET_VERSION
    SMART_SEND_DRIVE_COMMAND
    SMART_RCV_DRIVE_DATA我想问一下:
    什么是SMART(Self-Monitoring Analysis and Reporting Technology) 设备呢
    所有的硬盘一定是SMART 设备么
      

  12.   

    BTW
    there is somewhat difference between the two TypeSENDCMDINPARAMS types MSDN's SENDCMDINPARAMS and yoursyou can find the SENDCMDINPARAMS in MSDN at :
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/buses/hh/buses/ide_struct_11gy.asp