找回密码
 加入我们

QQ登录

只需一步,快速开始

搜索
查看: 4785|回复: 0

[磁盘文件] 马大哈系列功能模块-----利用SMART接口获取硬盘序列号(VB6.0)

[复制链接]

275

主题

3017

回帖

1

精华

管理员

嗷嗷叫的老马

积分
17064

论坛牛人贡献奖关注奖最佳版主进步奖人气王疯狂作品奖精英奖赞助论坛勋章乐于助人勋章

QQ
发表于 2013-3-21 17:08:59 | 显示全部楼层 |阅读模式
很久很久以前网上摘的,好用.

调用方式我做了一个小封装:
  1. msgbox GetHDDID(1)         '取第一个硬盘的序列号
复制代码
ModGetHID.bas (17.6 KB, 下载次数: 3186)
以下是代码:
  1. '****************************************************************
  2. '原作: Bardo
  3. '出处: 《东方热讯》网站
  4. '网址: www.easthot.net
  5. '****************************************************************
  6. '(如需转载,请不在删除以上信息,否则视为侵权!)
  7. '****************************************************************
  8. '要这个有什么用?可以生成与硬件相关的注册码。控制软件不重复使用!那么,很多多人认为VB实现不了。自然没有找到方法,一定是实现不了。然而,感谢WWW,我们能在上面找到VC的源码,DELPHI的源码。但是VB的就是见不到。为此,我决定将VC的源码改成VB的,以下即是:

  9. 'VC原作说明部分(再发行时,请注意采用注解的方式,请不要删除的方式侵权,谢谢!)
  10. '*************************************************************************
  11. '通常情况下,我们通过=&HEC命令对IDE端口进行监测.获取硬盘信息.
  12. '一般情况下,我们就写个VXD或者DRIVER来完成.但是现在,通过MS的S.M.A.R.T.接口,
  13. '我们可以直接从RING3调用API DeviceIoControl()来获取硬盘信息.下面乃是我的例程:
  14. '另外,也有编译好的版本供大家平时使用.欢迎下载.
  15. '/*+++
  16. 'HDID.CPP
  17. 'Written by Lu Lin
  18. 'http://lu0.126.com
  19. '2000.11.3
  20. '---*/
  21. '*************************************************************************
  22. 'VB程序编制: BARDO
  23. '本来我想写一个只取盘动物理序列号的。但是考虑到大家学习的方便。还是将原来的代码
  24. '全部翻译了出来。如果你需要单一的只查一个主硬盘的序列号的程序,欢迎到本站下载。
  25. '
  26. '网站:东方热讯:http://www.easthot.net
  27. '邮件:sales@easthot.net
  28. '2003.01.23
  29. '*************************************************************************
  30. Option Explicit
  31. '以下这一行是必须的,困为要做结构复制。而结构中有数组。所以,没有它则会错位
  32. Option Base 0

  33. Private Const DFP_GET_VERSION As Long = &H74080
  34. Private Const DFP_SEND_DRIVE_COMMAND  As Long = &H7C084
  35. Private Const DFP_RECEIVE_DRIVE_DATA  As Long = &H7C088

  36. '#pragma pack(1)
  37. Private Type TGETVERSIONOUTPARAMS   '{
  38.     bVersion As Byte  'Binary driver version.
  39.     bRevision As Byte 'Binary driver revision.
  40.     bReserved As Byte  'Not used.
  41.     bIDEDeviceMap As Byte 'Bit map of IDE devices.
  42.     fCapabilities As Long 'Bit mask of driver capabilities.
  43.     dwReserved(4) As Long 'For future use.
  44. End Type

  45. Private Type TIDEREGS
  46.     bFeaturesReg As Byte   'Used for specifying SMART "commands".
  47.     bSectorCountReg As Byte  'IDE sector count register
  48.     bSectorNumberReg As Byte  'IDE sector number register
  49.     bCylLowReg As Byte    'IDE low order cylinder value
  50.     bCylHighReg As Byte   'IDE high order cylinder value
  51.     bDriveHeadReg As Byte   'IDE drive/head register
  52.     bCommandReg As Byte   'Actual IDE command.
  53.     bReserved As Byte    'reserved for future use.  Must be zero.
  54. End Type

  55. Private Type TSENDCMDINPARAMS
  56.     cBufferSize As Long   'Buffer size in bytes
  57.     irDriveRegs As TIDEREGS   'Structure with drive register values.
  58.     bDriveNumber As Byte   'Physical drive number to send  'command to (0,1,2,3).
  59.     bReserved(2) As Byte   'Reserved for future expansion.
  60.     dwReserved(3) As Long   'For future use.
  61.     ''BYTE  bBuffer(1)   'Input buffer.
  62. End Type

  63. Private Type TDRIVERSTATUS
  64.     bDriverError As Byte  'Error code from driver, 'or 0 if no error.
  65.     bIDEStatus  As Byte  'Contents of IDE Error register.
  66.            'Only valid when bDriverError 'is SMART_IDE_ERROR.
  67.     bReserved(1) As Byte   'Reserved for future expansion.
  68.     dwReserved(1) As Long   'Reserved for future expansion.
  69. End Type

  70. Private Type TSENDCMDOUTPARAMS
  71.     cBufferSize As Long      'Size of bBuffer in bytes
  72.     DRIVERSTATUS As TDRIVERSTATUS   'Driver status structure.
  73.     bBuffer(511) As Byte   'Buffer of arbitrary length
  74.              'in which to store the data read from the drive.
  75. End Type

  76. '下面的结构是要从另一结构复制数据过来的,所以,必须是字节数与VC的完全一致
  77. '而不能用兼容变量,但这里的我们还是用了兼容变量,Integer,因为此结构中这一
  78. '类型的的变量程序中没有用到,如果要用到,建议改为Byte类型。因为VB没有USHORT

  79. Private Type TIDSECTOR
  80.     wGenConfig As Integer
  81.     wNumCyls As Integer
  82.     wReserved As Integer
  83.     wNumHeads As Integer
  84.     wBytesPerTrack As Integer
  85.     wBytesPerSector As Integer
  86.     wSectorsPerTrack As Integer
  87.     wVendorUnique(2) As Integer
  88.     sSerialNumber(19) As Byte
  89.     wBufferType As Integer
  90.     wBufferSize As Integer
  91.     wECCSize As Integer
  92.     sFirmwareRev(7) As Byte
  93.     sModelNumber(39) As Byte
  94.     wMoreVendorUnique As Integer
  95.     wDoubleWordIO As Integer
  96.     wCapabilities As Integer
  97.     wReserved1 As Integer
  98.     wPIOTiming As Integer
  99.     wDMATiming As Integer
  100.     wBS As Integer
  101.     wNumCurrentCyls As Integer
  102.     wNumCurrentHeads As Integer
  103.     wNumCurrentSectorsPerTrack As Integer
  104.     ulCurrentSectorCapacity(3) As Byte   '这里只能用byte,因为VB没有无符号的LONG型变量
  105.     wMultSectorStuff As Integer
  106.     ulTotalAddressableSectors(3) As Byte '这里只能用byte,因为VB没有无符号的LONG型变量
  107.     wSingleWordDMA As Integer
  108.     wMultiWordDMA As Integer
  109.     bReserved(127) As Byte
  110. End Type

  111. '/*+++
  112. 'Global vars
  113. '---*/
  114. Private vers As TGETVERSIONOUTPARAMS
  115. Private in_data As TSENDCMDINPARAMS
  116. Private out_data As TSENDCMDOUTPARAMS
  117. Private h As Long
  118. Private I As Long
  119. Private J As Byte

  120. Private Type OSVERSIONINFO
  121.     dwOSVersionInfoSize As Long
  122.     dwMajorVersion As Long
  123.     dwMinorVersion As Long
  124.     dwBuildNumber As Long
  125.     dwPlatformId As Long
  126.     szCSDVersion As String * 128
  127. End Type

  128. Private Declare Function GetVersionEx Lib "kernel32" Alias "GetVersionExA" _
  129.           (LpVersionInformation As OSVERSIONINFO) As Long

  130. Private Const VER_PLATFORM_WIN32S = 0
  131. Private Const VER_PLATFORM_WIN32_WINDOWS = 1
  132. Private Const VER_PLATFORM_WIN32_NT = 2

  133. Private Declare Function CreateFile Lib "kernel32" _
  134.     Alias "CreateFileA" (ByVal lpFileName As String, _
  135.     ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, _
  136.     ByVal lpSecurityAttributes As Long, _
  137.     ByVal dwCreationDisposition As Long, _
  138.     ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) _
  139.     As Long

  140. Private Const CREATE_NEW = 1
  141. Private Const GENERIC_READ = &H80000000
  142. Private Const GENERIC_WRITE = &H40000000
  143. Private Const OPEN_EXISTING = 3
  144. Private Const FILE_SHARE_READ = &H1
  145. Private Const FILE_SHARE_WRITE = &H2

  146. Private Type OVERLAPPED
  147.     Internal As Long
  148.     InternalHigh As Long
  149.     offset As Long
  150.     OffsetHigh As Long
  151.     hEvent As Long
  152. End Type

  153. Private Declare Function DeviceIoControl Lib "kernel32" _
  154.     (ByVal hDevice As Long, ByVal dwIoControlCode As Long, _
  155.     lpInBuffer As Any, ByVal nInBufferSize As Long, _
  156.     lpOutBuffer As Any, ByVal nOutBufferSize As Long, _
  157.     lpBytesReturned As Long, lpOverlapped As OVERLAPPED) As Long

  158. Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

  159. Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _
  160.          hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)


  161. Private Sub CopyRight()
  162.     'VC原版权代码(再发行时,请注意采用注解的方式,请不要删除的方式侵权,谢谢!)
  163.     '****************************************************************************
  164.     ' cerr<<endl<<"HDD identifier v1.0 for WIN95/98/Me/NT/2000. written by Lu Lin"<<endl
  165.     ' cerr<<"For more information, please visit Inside Programming: http:'lu0.126.com"<<endl
  166.     ' cerr<<"2000.11.3"<<endl<<endl
  167.     '****************************************************************************
  168.     Dim StrMsg As String
  169.     StrMsg = StrMsg & "直接从RING3调用API DeviceIoControl()来获取硬盘信息的VB程序 "
  170.     StrMsg = StrMsg & vbCrLf & "VC源作板权信息如下:"
  171.     StrMsg = StrMsg & vbCrLf & "***********************************************************"
  172.     StrMsg = StrMsg & vbCrLf & "HDD identifier v1.0 for WIN95/98/Me/NT/2000. written by Lu Lin"
  173.     StrMsg = StrMsg & vbCrLf & "For more information, please visit Inside Programming: http://lu0.126.com"
  174.     StrMsg = StrMsg & vbCrLf & "2000.11.3"
  175.     StrMsg = StrMsg & vbCrLf & "***********************************************************"
  176.     StrMsg = StrMsg & vbCrLf & "VB程序编制:BARDO"
  177.     StrMsg = StrMsg & vbCrLf & "网站:东方热讯:http://www.easthot.net"
  178.     StrMsg = StrMsg & vbCrLf & "邮件:sales@easthot.net"
  179.     StrMsg = StrMsg & vbCrLf & "2003.01.23"
  180.     MsgBox StrMsg
  181. End Sub

  182. Sub ChangeByteOrder(szString() As Byte, uscStrSize As Long)
  183.     Dim I As Long
  184.     Dim Temp As String
  185.      For I = 0 To uscStrSize - 1 Step 2
  186.         Temp = szString(I)
  187.         szString(I) = szString(I + 1)
  188.         szString(I + 1) = Temp
  189.      Next I
  190. End Sub

  191. Private Function hdid9x() As String

  192. 'We start in 95/98/Me
  193. h = CreateFile("\\.\Smartvsd", 0, 0, 0, CREATE_NEW, 0, 0)
  194. If h = 0 Then
  195.     hdid9x = "open smartvsd.vxd failed"
  196.     Exit Function
  197. End If

  198. Dim olp As OVERLAPPED
  199. Dim lRet As Long
  200. lRet = DeviceIoControl(h, DFP_GET_VERSION, ByVal 0&, 0, vers, Len(vers), ByVal I, olp)
  201. If lRet = 0 Then
  202.         hdid9x = "DeviceIoControl failed:DFP_GET_VERSION"
  203.         CloseHandle (h)
  204.         Exit Function
  205. End If

  206. 'If IDE identify command not supported, fails
  207. If (vers.fCapabilities And 1) <> 1 Then
  208.     hdid9x = "Error: IDE identify command not supported."
  209.     CloseHandle (h)
  210.     Exit Function
  211. End If

  212. 'Display IDE drive number detected
  213. Dim sPreOutStr As String
  214. sPreOutStr = DetectIDE(vers.bIDEDeviceMap)
  215. hdid9x = sPreOutStr

  216. 'Identify the IDE drives
  217. For J = 0 To 3
  218.     Dim phdinfo As TIDSECTOR
  219.     Dim S(40) As Byte
  220.    
  221.     If (J And 1) = 1 Then
  222.         in_data.irDriveRegs.bDriveHeadReg = &HB0
  223.     Else
  224.         in_data.irDriveRegs.bDriveHeadReg = &HA0
  225.     End If
  226.     If (vers.fCapabilities And (16 \ (2 ^ J))) = (16 \ (2 ^ J)) Then
  227.         'We don't detect a ATAPI device.
  228.         hdid9x = "Drive " & CStr(J + 1) & " is a ATAPI device, we don't detect it"
  229.     Else
  230.           in_data.irDriveRegs.bCommandReg = &HEC
  231.           in_data.bDriveNumber = J
  232.           in_data.irDriveRegs.bSectorCountReg = 1
  233.           in_data.irDriveRegs.bSectorNumberReg = 1
  234.           in_data.cBufferSize = 512
  235.          
  236.           lRet = DeviceIoControl(h, DFP_RECEIVE_DRIVE_DATA, in_data, Len(in_data), out_data, Len(out_data), ByVal I, olp)
  237.          
  238.           If lRet = 0 Then
  239.               hdid9x = "DeviceIoControl failed:DFP_RECEIVE_DRIVE_DATA"
  240.               CloseHandle (h)
  241.               Exit Function
  242.           End If
  243.          
  244.           Dim StrOut As String
  245.          
  246.           CopyMemory phdinfo, out_data.bBuffer(0), Len(phdinfo)
  247.          
  248.           CopyMemory S(0), phdinfo.sModelNumber(0), 40
  249.           S(40) = 0
  250.           ChangeByteOrder S, 40
  251.          
  252.           StrOut = ByteArrToString(S, 40)
  253.          
  254.           hdid9x = hdid9x & vbCrLf & "Module Number:" & StrOut
  255.           CopyMemory S(0), phdinfo.sFirmwareRev(0), 8
  256.           S(8) = 0
  257.           ChangeByteOrder S, 8
  258.          
  259.           StrOut = ByteArrToString(S, 8)
  260.          
  261.           hdid9x = hdid9x & vbCrLf & "Firmware rev:" & StrOut
  262.           CopyMemory S(0), phdinfo.sSerialNumber(0), 20
  263.           S(20) = 0
  264.           ChangeByteOrder S, 20
  265.          
  266.           StrOut = ByteArrToString(S, 20)
  267.          
  268.           hdid9x = hdid9x & vbCrLf & "Serial Number:" & StrOut
  269.          
  270.           CopyMemory S(0), phdinfo.ulTotalAddressableSectors(0), 4
  271.          
  272.           S(5) = 0
  273.           Dim dblStrOut As Double
  274.           dblStrOut = ByteArrToLong(S)
  275.           hdid9x = hdid9x & vbCrLf & "Capacity:" & dblStrOut / 2 / 1024 & "M"
  276.       End If
  277. Next J

  278. 'Close handle before quit
  279. CloseHandle (h)
  280. CopyRight

  281. End Function

  282. Private Function hdidnt(ByRef outHDDID() As String) As String
  283. Dim hd As String * 80
  284. Dim phdinfo As TIDSECTOR
  285. Dim S(40) As Byte
  286. Dim StrOut As String

  287. ReDim outHDDID(3)

  288. hdidnt = ""
  289. 'We start in NT/Win2000

  290. For J = 0 To 3  '这里取四个硬盘的信息,因为正常PC不超过四个硬盘
  291.      hd = "\\.\PhysicalDrive" & CStr(J)
  292.      hdidnt = hdidnt & vbCrLf & hd
  293.      h = CreateFile(hd, GENERIC_READ Or GENERIC_WRITE, _
  294.           FILE_SHARE_READ Or FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0)
  295.      
  296.      Dim olpv As OVERLAPPED
  297.      
  298.      Dim lRet As Long
  299.      lRet = DeviceIoControl(h, DFP_GET_VERSION, ByVal 0&, 0, vers, Len(vers), ByVal I, olpv)
  300.      
  301.      If lRet = 0 Then
  302.          CloseHandle (h)
  303.      Else
  304.             'If IDE identify command not supported, fails
  305.             If (vers.fCapabilities And 1) <> 1 Then
  306.                   hdidnt = "Error: IDE identify command not supported."
  307.                   CloseHandle (h)
  308.                   Exit Function
  309.             End If
  310.             'Identify the IDE drives
  311.             If (J And 1) = 1 Then
  312.                 in_data.irDriveRegs.bDriveHeadReg = &HB0
  313.             Else
  314.                 in_data.irDriveRegs.bDriveHeadReg = &HA0
  315.             End If
  316.             If (vers.fCapabilities And (16 \ (2 ^ J))) <> 0 Then
  317.                 'We don't detect a ATAPI device.
  318.                 hdidnt = hdidnt & vbCrLf & "Drive " & CStr(J + 1) & " is a ATAPI device, we don't detect it"
  319.             Else
  320.                   
  321.                   in_data.irDriveRegs.bCommandReg = &HEC
  322.                   in_data.bDriveNumber = J
  323.                   in_data.irDriveRegs.bSectorCountReg = 1
  324.                   in_data.irDriveRegs.bSectorNumberReg = 1
  325.                   in_data.cBufferSize = 512
  326.                   
  327.                   Dim olpr As OVERLAPPED
  328.                   
  329.                   lRet = DeviceIoControl(h, DFP_RECEIVE_DRIVE_DATA, in_data, Len(in_data), out_data, Len(out_data), ByVal I, olpr)
  330.                   If lRet <= 0 Then
  331.                        hdidnt = hdidnt & vbCrLf & "DeviceIoControl failed:DFP_RECEIVE_DRIVE_DATA"
  332.                        CloseHandle (h)
  333.                        
  334.                   Else
  335.                
  336.                      CopyMemory phdinfo, out_data.bBuffer(0), Len(phdinfo)
  337.                      
  338.                      CopyMemory S(0), phdinfo.sModelNumber(0), 40
  339.                      S(40) = 0
  340.                      ChangeByteOrder S, 40
  341.                      
  342.                      StrOut = ByteArrToString(S, 40)
  343.                      
  344.                      hdidnt = hdidnt & vbCrLf & "Module Number:" & StrOut
  345.                      CopyMemory S(0), phdinfo.sFirmwareRev(0), 8
  346.                      S(8) = 0
  347.                      ChangeByteOrder S, 8
  348.                      
  349.                      StrOut = ByteArrToString(S, 8)
  350.                      
  351.                      hdidnt = hdidnt & vbCrLf & "Firmware rev:" & StrOut
  352.                      CopyMemory S(0), phdinfo.sSerialNumber(0), 20
  353.                      S(20) = 0
  354.                      ChangeByteOrder S, 20
  355.                      
  356.                      StrOut = ByteArrToString(S, 20)
  357.                      
  358.                      hdidnt = hdidnt & vbCrLf & "Serial Number:" & StrOut
  359.                      outHDDID(J + 1) = Trim(StrOut)
  360.                      
  361.                      CopyMemory S(0), phdinfo.ulTotalAddressableSectors(0), 4
  362.                      S(5) = 0
  363.                      Dim dblStrOut As Double
  364.                      dblStrOut = ByteArrToLong(S)
  365.                      
  366.                      hdidnt = hdidnt & vbCrLf & "Capacity:" & dblStrOut / 2 / 1024 & "M"
  367.                      CloseHandle (h)
  368.                   End If
  369.             End If
  370.        End If
  371. Next J
  372. 'CopyRight

  373. End Function

  374. Private Function DetectIDE(bIDEDeviceMap As Byte) As String
  375.     If (bIDEDeviceMap And 1) Then
  376.         If (bIDEDeviceMap And 16) Then
  377.              DetectIDE = DetectIDE & "ATAPI device is attached to primary controller, drive 0."
  378.         Else
  379.              DetectIDE = DetectIDE & "IDE device is attached to primary controller, drive 0."
  380.         End If
  381.     End If
  382.     If (bIDEDeviceMap And 2) Then
  383.         If (bIDEDeviceMap And 32) Then
  384.              DetectIDE = DetectIDE & "ATAPI device is attached to primary controller, drive 1."
  385.         Else
  386.              DetectIDE = DetectIDE & "IDE device is attached to primary controller, drive 1."
  387.         End If
  388.     End If
  389.     If (bIDEDeviceMap And 4) Then
  390.         If (bIDEDeviceMap And 64) Then
  391.              DetectIDE = DetectIDE & "ATAPI device is attached to secondary controller, drive 0."
  392.         Else
  393.              DetectIDE = DetectIDE & "IDE device is attached to secondary controller, drive 0."
  394.         End If
  395.     End If
  396.     If (bIDEDeviceMap And 8) Then
  397.         If (bIDEDeviceMap And 128) Then
  398.              DetectIDE = DetectIDE & "ATAPI device is attached to secondary controller, drive 1."
  399.         Else
  400.              DetectIDE = DetectIDE & "IDE device is attached to secondary controller, drive 1."
  401.         End If
  402.     End If
  403. End Function

  404. Private Function ByteArrToString(inByte() As Byte, ByVal strlen As Integer) As String
  405.     Dim I As Integer
  406.     For I = 0 To strlen
  407.         If inByte(I) = 0 Then
  408.            Exit For
  409.         End If
  410.         ByteArrToString = ByteArrToString & Chr(inByte(I))
  411.     Next I
  412. End Function

  413. Private Function ByteArrToLong(inByte() As Byte) As Double
  414.     Dim I As Integer
  415.     For I = 0 To 3
  416.         ByteArrToLong = ByteArrToLong + CDbl(inByte(I)) * (256 ^ I)
  417.     Next I
  418. End Function

  419. Public Function GetHDDID(ByVal HDDIndex As Long, Optional ByRef outFullInfo As String = "-1") As String
  420.     '取硬盘ID
  421.     'HDDIndex - 硬盘号(1 - 4)
  422.     'outFullInfo - 可选,输出;用于输出完整的硬盘信息
  423.     '返回值:
  424.     '       指定硬盘的ID
  425.     '嗷嗷叫的老马 添加
  426.     '
  427.     Dim VerInfo As OSVERSIONINFO
  428.     Dim Ret As Long, I As Long, J As Long, K As Long
  429.     Dim strInfo As String, strInfoArr() As String
  430.    
  431.     VerInfo.dwOSVersionInfoSize = Len(VerInfo)
  432.     Ret = GetVersionEx(VerInfo)
  433.    
  434.     Select Case VerInfo.dwPlatformId
  435.     Case VER_PLATFORM_WIN32S
  436.         strInfo = "Win32s is not supported by this programm."
  437.     Case VER_PLATFORM_WIN32_WINDOWS
  438.         strInfo = hdid9x
  439.     Case VER_PLATFORM_WIN32_NT
  440.         strInfo = hdidnt(strInfoArr)
  441.     End Select
  442.     If outFullInfo <> "-1" Then outFullInfo = strInfo
  443.     GetHDDID = strInfoArr(HDDIndex)
  444. End Function
复制代码
我就是嗷嗷叫的老马了......

您需要登录后才可以回帖 登录 | 加入我们

本版积分规则

快速回复 返回顶部 返回列表