VB取硬盘的物理信息(序列号,容量,转速,型号)

来源:互联网 发布:安卓看片软件哪个好 编辑:程序博客网 时间:2024/06/10 08:49
 
  1. '
  2. '取硬盘的物理信息(序列号,容量,转速,型号)(smHDinfo)
  3. '
  4. '/网站:东方热讯:http://www.easthot.net
  5. '/邮件:sales@easthot.net
  6. '/2003.01.23
  7. '*************************************************************************
  8. Option Explicit
  9. '/以下这一行是必须的,困为要做结构复制。而结构中有数组。所以,没有它则会错位
  10. Option Base 0
  11. Private Const DFP_GET_VERSION = 
  12. Private Const DFP_SEND_DRIVE_COMMAND = 
  13. Private Const DFP_RECEIVE_DRIVE_DATA = 
  14. '/#pragma pack(1)
  15. Private Type TGETVERSIONOUTPARAMS   '{
  16.     bVersion As Byte  '/Binary driver version.
  17.     bRevision As Byte '/Binary driver revision.
  18.     bReserved As Byte  '/Not used.
  19.     bIDEDeviceMap As Byte '/Bit map of IDE devices.
  20.     fCapabilities As Long '/Bit mask of driver capabilities.
  21.     dwReserved(4) As Long '/For future use.
  22. End Type
  23. Private Type TIDEREGS
  24.     bFeaturesReg As Byte   '/Used for specifying SMART "commands".
  25.     bSectorCountReg As Byte  '/IDE sector count register
  26.     bSectorNumberReg As Byte  '/IDE sector number register
  27.     bCylLowReg As Byte    '/IDE low order cylinder value
  28.     bCylHighReg As Byte   '/IDE high order cylinder value
  29.     bDriveHeadReg As Byte   '/IDE drive/head register
  30.     bCommandReg As Byte   '/Actual IDE command.
  31.     bReserved As Byte    '/reserved for future use.  Must be zero.
  32. End Type
  33. Private Type TSENDCMDINPARAMS
  34.     cBufferSize As Long   '/Buffer size in bytes
  35.     irDriveRegs As TIDEREGS   '/Structure with drive register values.
  36.     bDriveNumber As Byte   '/Physical drive number to send  '/command to (0,1,2,3).
  37.     bReserved(2) As Byte   '/Reserved for future expansion.
  38.     dwReserved(3) As Long   '/For future use.
  39.     '//BYTE  bBuffer(1)   '/Input buffer.
  40. End Type
  41. Private Type TDRIVERSTATUS
  42.     bDriverError As Byte  '/Error code from driver, '/or 0 if no error.
  43.     bIDEStatus  As Byte  '/Contents of IDE Error register.
  44.            '//Only valid when bDriverError '/is SMART_IDE_ERROR.
  45.     bReserved(1) As Byte   '/Reserved for future expansion.
  46.     dwReserved(1) As Long   '/Reserved for future expansion.
  47. End Type
  48. Private Type TSENDCMDOUTPARAMS
  49.     cBufferSize As Long      '/Size of bBuffer in bytes
  50.     DRIVERSTATUS As TDRIVERSTATUS   '/Driver status structure.
  51.     bBuffer(511) As Byte   '/Buffer of arbitrary length
  52.              '//in which to store the data read from the drive.
  53. End Type
  54. '/下面的结构是要从另一结构复制数据过来的,所以,必须是字节数与VC的完全一致
  55. '/而不能用兼容变量,但这里的我们还是用了兼容变量,Integer,因为此结构中这一
  56. '/类型的的变量程序中没有用到,如果要用到,建议改为Byte类型。因为VB没有USHORT
  57. Private Type TIDSECTOR
  58.     wGenConfig As Integer
  59.     wNumCyls As Integer
  60.     wReserved As Integer
  61.     wNumHeads As Integer
  62.     wBytesPerTrack As Integer
  63.     wBytesPerSector As Integer
  64.     wSectorsPerTrack As Integer
  65.     wVendorUnique(2) As Integer
  66.     sSerialNumber(19) As Byte
  67.     wBufferType As Integer
  68.     wBufferSize As Integer
  69.     wECCSize As Integer
  70.     sFirmwareRev(7) As Byte
  71.     sModelNumber(39) As Byte
  72.     wMoreVendorUnique As Integer
  73.     wDoubleWordIO As Integer
  74.     wCapabilities As Integer
  75.     wReserved1 As Integer
  76.     wPIOTiming As Integer
  77.     wDMATiming As Integer
  78.     wBS As Integer
  79.     wNumCurrentCyls As Integer
  80.     wNumCurrentHeads As Integer
  81.     wNumCurrentSectorsPerTrack As Integer
  82.     ulCurrentSectorCapacity(3) As Byte   '/这里只能用byte,因为VB没有无符号的LONG型变量
  83.     wMultSectorStuff As Integer
  84.     ulTotalAddressableSectors(3) As Byte '/这里只能用byte,因为VB没有无符号的LONG型变量
  85.     wSingleWordDMA As Integer
  86.     wMultiWordDMA As Integer
  87.     bReserved(127) As Byte
  88. End Type
  89. Private vers As TGETVERSIONOUTPARAMS
  90. Private in_data As TSENDCMDINPARAMS
  91. Private out_data As TSENDCMDOUTPARAMS
  92. Private h As Long
  93. Private I As Long
  94. Private J As Byte
  95. Private Type OSVERSIONINFO
  96.     dwOSVersionInfoSize As Long
  97.     dwMajorVersion As Long
  98.     dwMinorVersion As Long
  99.     dwBuildNumber As Long
  100.     dwPlatformId As Long
  101.     szCSDVersion As String * 128
  102. End Type
  103. Private Declare Function GetVersionEx Lib "kernel32" Alias "GetVersionExA" _
  104.           (LpVersionInformation As OSVERSIONINFO) As Long
  105. Private Const VER_PLATFORM_WIN32S = 0
  106. Private Const VER_PLATFORM_WIN32_WINDOWS = 1
  107. Private Const VER_PLATFORM_WIN32_NT = 2
  108. Private Declare Function CreateFile Lib "kernel32" _
  109.     Alias "CreateFileA" (ByVal lpFileName As String, _
  110.     ByVal dwDesiredAccess As LongByVal dwShareMode As Long, _
  111.     ByVal lpSecurityAttributes As Long, _
  112.     ByVal dwCreationDisposition As Long, _
  113.     ByVal dwFlagsAndAttributes As LongByVal hTemplateFile As Long) _
  114.     As Long
  115. Private Const CREATE_NEW = 1
  116. Private Const GENERIC_READ = 
  117. Private Const GENERIC_WRITE = 
  118. Private Const OPEN_EXISTING = 3
  119. Private Const FILE_SHARE_READ = 
  120. Private Const FILE_SHARE_WRITE = 
  121. Private Type OVERLAPPED
  122.     Internal As Long
  123.     InternalHigh As Long
  124.     offset As Long
  125.     OffsetHigh As Long
  126.     hEvent As Long
  127. End Type
  128. Private Declare Function DeviceIoControl Lib "kernel32" _
  129.     (ByVal hDevice As LongByVal dwIoControlCode As Long, _
  130.     lpInBuffer As Any, ByVal nInBufferSize As Long, _
  131.     lpOutBuffer As Any, ByVal nOutBufferSize As Long, _
  132.     lpBytesReturned As Long, lpOverlapped As OVERLAPPED) As Long
  133. Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As LongAs Long
  134. Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _
  135.          hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)
  136. Private Sub ChangeByteOrder(szString() As Byte, uscStrSize As Long)
  137.     Dim I As Long
  138.     Dim temp As String
  139.      For I = 0 To uscStrSize - 1 Step 2
  140.         temp = szString(I)
  141.         szString(I) = szString(I + 1)
  142.         szString(I + 1) = temp
  143.      Next I
  144. End Sub
  145. Private Function hdid9x() As smHdInfoType()
  146.     '/We start in 95/98/Me
  147.     Dim RevInfo() As smHdInfoType
  148.     Dim RevID As Long
  149.     Dim olp As OVERLAPPED
  150.     Dim lRet As Long
  151.     
  152.     h = CreateFile("//./Smartvsd", 0, 0, 0, CREATE_NEW, 0, 0)
  153.     If h = 0 Then
  154.         RevID = RevID + 1
  155.         ReDim Preserve RevInfo(RevID - 1)
  156.         RevInfo(RevID - 1).InfoFlag = False
  157.         GoTo EndFun
  158.     End If
  159.     
  160.     lRet = DeviceIoControl(h, DFP_GET_VERSION, ByVal 0&, 0, vers, Len(vers), ByVal I, olp)
  161.     If lRet = 0 Then
  162.         RevID = RevID + 1
  163.         ReDim Preserve RevInfo(RevID - 1)
  164.         RevInfo(RevID - 1).InfoFlag = False
  165.         CloseHandle (h)
  166.         GoTo EndFun
  167.     End If
  168.     
  169.     '/If IDE identify command not supported, fails
  170.     If (vers.fCapabilities And 1) <> 1 Then
  171.         '/hdid9x = "Error: IDE identify command not supported."
  172.         RevID = RevID + 1
  173.         ReDim Preserve RevInfo(RevID - 1)
  174.         RevInfo(RevID - 1).InfoFlag = False
  175.         CloseHandle (h)
  176.         GoTo EndFun
  177.     End If
  178.     
  179.     '/Display IDE drive number detected
  180.     Dim sPreOutStr As String
  181.     sPreOutStr = DetectIDE(vers.bIDEDeviceMap)
  182.     '/hdid9x = sPreOutStr
  183.     
  184.     '/Identify the IDE drives
  185.     For J = 0 To 3
  186.         Dim phdinfo As TIDSECTOR
  187.         Dim s(40) As Byte
  188.         
  189.         RevID = RevID + 1
  190.         ReDim Preserve RevInfo(RevID - 1)
  191.         RevInfo(RevID - 1).Hdid = J
  192.         If (J And 1) = 1 Then
  193.             in_data.irDriveRegs.bDriveHeadReg = 
  194.         Else
  195.             in_data.irDriveRegs.bDriveHeadReg = 
  196.         End If
  197.         If (vers.fCapabilities And (16 / (2 ^ J))) = (16 / (2 ^ J)) Then
  198.             RevInfo(RevID - 1).InfoFlag = False
  199.         Else
  200.               in_data.irDriveRegs.bCommandReg = 
  201.               in_data.bDriveNumber = J
  202.               in_data.irDriveRegs.bSectorCountReg = 1
  203.               in_data.irDriveRegs.bSectorNumberReg = 1
  204.               in_data.cBufferSize = 512
  205.               RevInfo(RevID - 1).InfoFlag = True
  206.               lRet = DeviceIoControl(h, DFP_RECEIVE_DRIVE_DATA, in_data, Len(in_data), out_data, Len(out_data), ByVal I, olp)
  207.               
  208.               If lRet = 0 Then
  209.                   RevInfo(RevID - 1).InfoFlag = False
  210.                   CloseHandle (h)
  211.                   GoTo EndFun
  212.               End If
  213.               Dim StrOut As String
  214.               CopyMemory phdinfo, out_data.bBuffer(0), Len(phdinfo)
  215.               
  216.               CopyMemory s(0), phdinfo.sModelNumber(0), 40
  217.               s(40) = 0
  218.               ChangeByteOrder s, 40
  219.               
  220.               StrOut = ByteArrToString(s, 40)
  221.               RevInfo(RevID - 1).ModuleNumber = Trim$(StrOut)
  222.               CopyMemory s(0), phdinfo.sFirmwareRev(0), 8
  223.               s(8) = 0
  224.               ChangeByteOrder s, 8
  225.               
  226.               StrOut = ByteArrToString(s, 8)
  227.               RevInfo(RevID - 1).FirmwareRev = Trim$(StrOut)
  228.               CopyMemory s(0), phdinfo.sSerialNumber(0), 20
  229.               s(20) = 0
  230.               ChangeByteOrder s, 20
  231.               
  232.               StrOut = ByteArrToString(s, 20)
  233.               '/hdid9x = hdid9x & vbCrLf & "Serial Number:" & StrOut
  234.               RevInfo(RevID - 1).SerialNumber = Trim$(StrOut)
  235.               CopyMemory s(0), phdinfo.ulTotalAddressableSectors(0), 4
  236.               s(5) = 0
  237.               Dim dblStrOut As Double
  238.               dblStrOut = ByteArrToLong(s)
  239.               RevInfo(RevID - 1).Capcity = CLng(dblStrOut / 2 / 1024) & "M"
  240.           End If
  241.     Next J
  242.     '/Close handle before quit
  243.     CloseHandle (h)
  244. EndFun:
  245.     hdid9x = RevInfo
  246. End Function
  247. Private Function hdidnt() As smHdInfoType()
  248.     Dim hd As String * 80
  249.     Dim phdinfo As TIDSECTOR
  250.     Dim s(40) As Byte
  251.     Dim StrOut As String
  252.     
  253.     Dim RevInfo() As smHdInfoType
  254.     Dim RevID As Long
  255.     
  256.     '/hdidnt = ""
  257.     '/We start in NT/Win2000
  258.     
  259.     For J = 0 To 3  '/这里取四个硬盘的信息,因为正常PC不超过四个硬盘
  260.          RevID = RevID + 1
  261.          ReDim Preserve RevInfo(RevID - 1)
  262.          RevInfo(RevID - 1).Hdid = CStr(J)
  263.          
  264.          hd = "//./PhysicalDrive" & CStr(J)
  265.          '/hdidnt = hdidnt & vbCrLf & hd
  266.          h = CreateFile(hd, GENERIC_READ Or GENERIC_WRITE, _
  267.              FILE_SHARE_READ Or FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0)
  268.          
  269.          Dim olpv As OVERLAPPED
  270.          Dim lRet As Long
  271.          
  272.          lRet = DeviceIoControl(h, DFP_GET_VERSION, ByVal 0&, 0, vers, Len(vers), ByVal I, olpv)
  273.          
  274.          If lRet = 0 Then
  275.             RevInfo(RevID - 1).InfoFlag = False
  276.             CloseHandle (h)
  277.          Else
  278.                 '/If IDE identify command not supported, fails
  279.                 If (vers.fCapabilities And 1) <> 1 Then
  280.                       '/hdidnt = "Error: IDE identify command not supported."
  281.                       RevInfo(RevID - 1).InfoFlag = False
  282.                       CloseHandle (h)
  283.                       GoTo EndFun
  284.                       '/Exit Function
  285.                 End If
  286.                 '/Identify the IDE drives
  287.                 If (J And 1) = 1 Then
  288.                     in_data.irDriveRegs.bDriveHeadReg = 
  289.                 Else
  290.                     in_data.irDriveRegs.bDriveHeadReg = 
  291.                 End If
  292.                 If (vers.fCapabilities And (16 / (2 ^ J))) <> 0 Then
  293.                     '/We don't detect a ATAPI device.
  294.                     RevInfo(RevID - 1).InfoFlag = False
  295.                     
  296.                     '/hdidnt = hdidnt & vbCrLf & "Drive " & CStr(j + 1) & " is a ATAPI device, we don't detect it"
  297.                 Else
  298.                       
  299.                       in_data.irDriveRegs.bCommandReg = 
  300.                       in_data.bDriveNumber = J
  301.                       in_data.irDriveRegs.bSectorCountReg = 1
  302.                       in_data.irDriveRegs.bSectorNumberReg = 1
  303.                       in_data.cBufferSize = 512
  304.                       
  305.                       Dim olpr As OVERLAPPED
  306.                       
  307.                       lRet = DeviceIoControl(h, DFP_RECEIVE_DRIVE_DATA, in_data, Len(in_data), out_data, Len(out_data), ByVal I, olpr)
  308.                       If lRet <= 0 Then
  309.                            '/hdidnt = hdidnt & vbCrLf & "DeviceIoControl failed:DFP_RECEIVE_DRIVE_DATA"
  310.                            RevInfo(RevID - 1).InfoFlag = False
  311.                            CloseHandle (h)
  312.                            
  313.                       Else
  314.                          RevInfo(RevID - 1).InfoFlag = True
  315.                          CopyMemory phdinfo, out_data.bBuffer(0), Len(phdinfo)
  316.                          
  317.                          CopyMemory s(0), phdinfo.sModelNumber(0), 40
  318.                          s(40) = 0
  319.                          ChangeByteOrder s, 40
  320.                          
  321.                          StrOut = ByteArrToString(s, 40)
  322.                          RevInfo(RevID - 1).ModuleNumber = Trim$(StrOut)
  323.                          '/hdidnt = hdidnt & vbCrLf & "Module Number:" & StrOut
  324.                          CopyMemory s(0), phdinfo.sFirmwareRev(0), 8
  325.                          s(8) = 0
  326.                          ChangeByteOrder s, 8
  327.                          
  328.                          StrOut = ByteArrToString(s, 8)
  329.                          RevInfo(RevID - 1).FirmwareRev = Trim$(StrOut)
  330.                          '/hdidnt = hdidnt & vbCrLf & "Firmware rev:" & StrOut
  331.                          CopyMemory s(0), phdinfo.sSerialNumber(0), 20
  332.                          s(20) = 0
  333.                          ChangeByteOrder s, 20
  334.                          
  335.                          StrOut = ByteArrToString(s, 20)
  336.                          
  337.                          '/hdidnt = hdidnt & vbCrLf & "Serial Number:" & StrOut
  338.                          RevInfo(RevID - 1).SerialNumber = Trim$(StrOut)
  339.                          CopyMemory s(0), phdinfo.ulTotalAddressableSectors(0), 4
  340.                          s(5) = 0
  341.                          Dim dblStrOut As Double
  342.                          dblStrOut = ByteArrToLong(s)
  343.                          RevInfo(RevID - 1).Capcity = CLng(dblStrOut / 2 / 1024) & "M"
  344.                          '/hdidnt = hdidnt & vbCrLf & "Capacity:" & dblStrOut / 2 / 1024 & "M"
  345.                          CloseHandle (h)
  346.                       End If
  347.                 End If
  348.            End If
  349.     Next
  350. EndFun:
  351.     hdidnt = RevInfo
  352. End Function
  353. Private Function DetectIDE(bIDEDeviceMap As ByteAs String
  354.     If (bIDEDeviceMap And 1) Then
  355.         If (bIDEDeviceMap And 16) Then
  356.              DetectIDE = DetectIDE & "ATAPI device is attached to primary controller, drive 0."
  357.         Else
  358.              DetectIDE = DetectIDE & "IDE device is attached to primary controller, drive 0."
  359.         End If
  360.     End If
  361.     If (bIDEDeviceMap And 2) Then
  362.         If (bIDEDeviceMap And 32) Then
  363.              DetectIDE = DetectIDE & "ATAPI device is attached to primary controller, drive 1."
  364.         Else
  365.              DetectIDE = DetectIDE & "IDE device is attached to primary controller, drive 1."
  366.         End If
  367.     End If
  368.     If (bIDEDeviceMap And 4) Then
  369.         If (bIDEDeviceMap And 64) Then
  370.              DetectIDE = DetectIDE & "ATAPI device is attached to secondary controller, drive 0."
  371.         Else
  372.              DetectIDE = DetectIDE & "IDE device is attached to secondary controller, drive 0."
  373.         End If
  374.     End If
  375.     If (bIDEDeviceMap And 8) Then
  376.         If (bIDEDeviceMap And 128) Then
  377.              DetectIDE = DetectIDE & "ATAPI device is attached to secondary controller, drive 1."
  378.         Else
  379.              DetectIDE = DetectIDE & "IDE device is attached to secondary controller, drive 1."
  380.         End If
  381.     End If
  382. End Function
  383. Private Function ByteArrToString(inByte() As ByteByVal strlen As IntegerAs String
  384.     Dim I As Integer
  385.     For I = 0 To strlen
  386.         If inByte(I) = 0 Then
  387.            Exit For
  388.         End If
  389.         ByteArrToString = ByteArrToString & Chr(inByte(I))
  390.     Next I
  391. End Function
  392. Private Function ByteArrToLong(inByte() As ByteAs Double
  393.     Dim I As Integer
  394.     For I = 0 To 3
  395.         ByteArrToLong = ByteArrToLong + CDbl(inByte(I)) * (256 ^ I)
  396.     Next I
  397. End Function
  398. Public Function GetHdInfo() As smHdInfoType()
  399.     Dim RevInfo() As smHdInfoType
  400.     Dim verinfo As OSVERSIONINFO
  401.     Dim Ret As Long
  402.     
  403.     verinfo.dwOSVersionInfoSize = Len(verinfo)
  404.     Ret = GetVersionEx(verinfo)
  405.     
  406.     Select Case verinfo.dwPlatformId
  407.     Case VER_PLATFORM_WIN32S
  408.          ReDim RevInfo(1)
  409.          RevInfo(0).InfoFlag = False
  410.     Case VER_PLATFORM_WIN32_WINDOWS
  411.         RevInfo = hdid9x()
  412.     Case VER_PLATFORM_WIN32_NT
  413.         RevInfo = hdidnt()
  414.     End Select
  415.     GetHdInfo = RevInfo
  416. End Function
原创粉丝点击