找回密码
 加入我们

QQ登录

只需一步,快速开始

搜索
查看: 6798|回复: 3

直接从RING3获取硬盘序列号

  [复制链接]

1214

主题

352

回帖

11

精华

管理员

菜鸟

积分
93755

贡献奖关注奖人气王精英奖乐于助人勋章

发表于 2010-3-9 22:33:27 | 显示全部楼层 |阅读模式

  1. '直接从RING3获取硬盘序列号
  2. Option Explicit
  3. '以下这一行是必须的,困为要做结构复制。而结构中有数组。所以,没有它则会错位
  4. Option Base 0
  5. Private Const DFP_GET_VERSION = &H74080
  6. Private Const DFP_SEND_DRIVE_COMMAND = &H7C084
  7. Private Const DFP_RECEIVE_DRIVE_DATA = &H7C088
  8. '#pragma   pack(1)
  9.   Private Type TGETVERSIONOUTPARAMS           '{
  10.     bVersion As Byte    'Binary   driver   version.
  11.     bRevision As Byte    'Binary   driver   revision.
  12.     bReserved As Byte    'Not   used.
  13.     bIDEDeviceMap As Byte    'Bit   map   of   IDE   devices.
  14.     fCapabilities As Long    'Bit   mask   of   driver   capabilities.
  15.     dwReserved(4) As Long    'For   future   use.
  16. End Type
  17.   Private Type TIDEREGS
  18.     bFeaturesReg As Byte    'Used   for   specifying   SMART   "commands".
  19.     bSectorCountReg As Byte    'IDE   sector   count   register
  20.     bSectorNumberReg As Byte    'IDE   sector   number   register
  21.     bCylLowReg As Byte    'IDE   low   order   cylinder   value
  22.     bCylHighReg As Byte    'IDE   high   order   cylinder   value
  23.     bDriveHeadReg As Byte    'IDE   drive/head   register
  24.     bCommandReg As Byte    'Actual   IDE   command.
  25.     bReserved As Byte    'reserved   for   future   use.     Must   be   zero.
  26. End Type
  27.   Private Type TSENDCMDINPARAMS
  28.     cBufferSize As Long    'Buffer   size   in   bytes
  29.     irDriveRegs As TIDEREGS    'Structure   with   drive   register   values.
  30.     bDriveNumber As Byte    'Physical   drive   number   to   send     'command   to   (0,1,2,3).
  31.     bReserved(2) As Byte    'Reserved   for   future   expansion.
  32.     dwReserved(3) As Long    'For   future   use.
  33.     ''BYTE     bBuffer(1)       'Input   buffer.
  34. End Type
  35.   Private Type TDRIVERSTATUS
  36.     bDriverError As Byte    'Error   code   from   driver,   'or   0   if   no   error.
  37.     bIDEStatus As Byte    'Contents   of   IDE   Error   register.
  38.     'Only   valid   when   bDriverError   'is   SMART_IDE_ERROR.
  39.     bReserved(1) As Byte    'Reserved   for   future   expansion.
  40.     dwReserved(1) As Long    'Reserved   for   future   expansion.
  41. End Type
  42.   Private Type TSENDCMDOUTPARAMS
  43.     cBufferSize As Long    'Size   of   bBuffer   in   bytes
  44.     DRIVERSTATUS As TDRIVERSTATUS    'Driver   status   structure.
  45.     bBuffer(511) As Byte    'Buffer   of   arbitrary   length
  46.     'in   which   to   store   the   data   read   from   the   drive.
  47. End Type
  48. '下面的结构是要从另一结构复制数据过来的,所以,必须是字节数与VC的完全一致
  49. '而不能用兼容变量,但这里的我们还是用了兼容变量,Integer,因为此结构中这一
  50. '类型的的变量程序中没有用到,如果要用到,建议改为Byte类型。因为VB没有USHORT
  51.   Private Type TIDSECTOR
  52.     wGenConfig As Integer
  53.     wNumCyls As Integer
  54.     wReserved As Integer
  55.     wNumHeads As Integer
  56.     wBytesPerTrack As Integer
  57.     wBytesPerSector As Integer
  58.     wSectorsPerTrack As Integer
  59.     wVendorUnique(2) As Integer
  60.     sSerialNumber(19) As Byte
  61.     wBufferType As Integer
  62.     wBufferSize As Integer
  63.     wECCSize As Integer
  64.     sFirmwareRev(7) As Byte
  65.     sModelNumber(39) As Byte
  66.     wMoreVendorUnique As Integer
  67.     wDoubleWordIO As Integer
  68.     wCapabilities As Integer
  69.     wReserved1 As Integer
  70.     wPIOTiming As Integer
  71.     wDMATiming As Integer
  72.     wBS As Integer
  73.     wNumCurrentCyls As Integer
  74.     wNumCurrentHeads As Integer
  75.     wNumCurrentSectorsPerTrack As Integer
  76.     ulCurrentSectorCapacity(3) As Byte    '这里只能用byte,因为VB没有无符号的LONG型变量
  77.     wMultSectorStuff As Integer
  78.     ulTotalAddressableSectors(3) As Byte    '这里只能用byte,因为VB没有无符号的LONG型变量
  79.     wSingleWordDMA As Integer
  80.     wMultiWordDMA As Integer
  81.     bReserved(127) As Byte
  82. End Type
  83. Private vers As TGETVERSIONOUTPARAMS
  84. Private in_data As TSENDCMDINPARAMS
  85. Private out_data As TSENDCMDOUTPARAMS
  86. Private h As Long
  87. Private i As Long
  88. Private j As Byte
  89. Private Type OSVERSIONINFO
  90.     dwOSVersionInfoSize As Long
  91.     dwMajorVersion As Long
  92.     dwMinorVersion As Long
  93.     dwBuildNumber As Long
  94.     dwPlatformId As Long
  95.     szCSDVersion As String * 128
  96. End Type
  97. Private Declare Function GetVersionEx Lib "kernel32" Alias "GetVersionExA" _
  98.         (LpVersionInformation As OSVERSIONINFO) As Long
  99. Private Const VER_PLATFORM_WIN32S = 0
  100. Private Const VER_PLATFORM_WIN32_WINDOWS = 1
  101. Private Const VER_PLATFORM_WIN32_NT = 2
  102. Private Declare Function CreateFile Lib "kernel32" _
  103.         Alias "CreateFileA" (ByVal lpFileName As String, _
  104.         ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, _
  105.         ByVal lpSecurityAttributes As Long, _
  106.         ByVal dwCreationDisposition As Long, _
  107.         ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) _
  108.         As Long
  109. Private Const CREATE_NEW = 1
  110. Private Const GENERIC_READ = &H80000000
  111. Private Const GENERIC_WRITE = &H40000000
  112. Private Const OPEN_EXISTING = 3
  113. Private Const FILE_SHARE_READ = &H1
  114. Private Const FILE_SHARE_WRITE = &H2
  115. Private Type OVERLAPPED
  116.     Internal As Long
  117.     InternalHigh As Long
  118.     offset As Long
  119.     OffsetHigh As Long
  120.     hEvent As Long
  121. End Type
  122. Private Declare Function DeviceIoControl Lib "kernel32" _
  123.         (ByVal hDevice As Long, ByVal dwIoControlCode As Long, _
  124.         lpInBuffer As Any, ByVal nInBufferSize As Long, _
  125.         lpOutBuffer As Any, ByVal nOutBufferSize As Long, _
  126.         lpBytesReturned As Long, lpOverlapped As OVERLAPPED) As Long
  127. Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
  128. Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" ( _
  129.         hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)

  130. Private Sub CopyRight()
  131.     'VC原版权代码(再发行时,请注意采用注解的方式,请不要删除的方式侵权,谢谢!)
  132.     '****************************************************************************
  133.     '   cerr<<endl<<"HDD   identifier   v1.0   for   WIN95/98/Me/NT/2000.   written   by   Lu   Lin"<<endl
  134.     '   cerr<<"For   more   information,   please   visit   Inside   Programming:   http:'lu0.126.com"<<endl
  135.     '   cerr<<"2000.11.3"<<endl<<endl
  136.     '****************************************************************************
  137. Dim StrMsg As String
  138.     StrMsg = StrMsg & "直接从RING3调用API   DeviceIoControl()来获取硬盘信息的VB程序   "
  139.     StrMsg = StrMsg & vbCrLf & "VC源作板权信息如下:"
  140.     StrMsg = StrMsg & vbCrLf & "***********************************************************"
  141.     StrMsg = StrMsg & vbCrLf & "HDD   identifier   v1.0   for   WIN95/98/Me/NT/2000.   written   by   Lu   Lin"
  142.     StrMsg = StrMsg & vbCrLf & "For   more   information,   please   visit   Inside   Programming:   http://lu0.126.com"
  143.     StrMsg = StrMsg & vbCrLf & "2000.11.3"
  144.     StrMsg = StrMsg & vbCrLf & "***********************************************************"
  145.     StrMsg = StrMsg & vbCrLf & "VB程序编制:BARDO"
  146.     StrMsg = StrMsg & vbCrLf & "网站:东方热讯:http://www.easthot.net"
  147.     StrMsg = StrMsg & vbCrLf & "邮件:sales@easthot.net"
  148.     StrMsg = StrMsg & vbCrLf & "2003.01.23"
  149.     MsgBox StrMsg
  150. End Sub
  151. Sub ChangeByteOrder(szString() As Byte, uscStrSize As Long)
  152. Dim i As Long
  153. Dim temp As String
  154.     For i = 0 To uscStrSize - 1 Step 2
  155.         temp = szString(i)
  156.         szString(i) = szString(i + 1)
  157.         szString(i + 1) = temp
  158.     Next i
  159. End Sub
  160. Private Function hdid9x() As String
  161.     'We   start   in   95/98/Me
  162.     h = CreateFile("\\.\Smartvsd", 0, 0, 0, CREATE_NEW, 0, 0)
  163.     If h = 0 Then
  164.         hdid9x = "open   smartvsd.vxd   failed"
  165.         Exit Function
  166.     End If
  167.     Dim olp As OVERLAPPED
  168.     Dim lRet As Long
  169.     lRet = DeviceIoControl(h, DFP_GET_VERSION, ByVal 0&, 0, vers, Len(vers), ByVal i, olp)
  170.     If lRet = 0 Then
  171.         hdid9x = "DeviceIoControl   failed:DFP_GET_VERSION"
  172.         CloseHandle (h)
  173.         Exit Function
  174.     End If
  175.     'If   IDE   identify   command   not   supported,   fails
  176.     If (vers.fCapabilities And 1) <> 1 Then
  177.         hdid9x = "Error:   IDE   identify   command   not   supported."
  178.         CloseHandle (h)
  179.         Exit Function
  180.     End If
  181.     'Display   IDE   drive   number   detected
  182.     Dim sPreOutStr As String
  183.     sPreOutStr = DetectIDE(vers.bIDEDeviceMap)
  184.     hdid9x = sPreOutStr
  185.     'Identify   the   IDE   drives
  186.     For j = 0 To 3
  187.         Dim phdinfo As TIDSECTOR
  188.         Dim s(40) As Byte
  189.         If (j And 1) = 1 Then
  190.             in_data.irDriveRegs.bDriveHeadReg = &HB0
  191.         Else
  192.             in_data.irDriveRegs.bDriveHeadReg = &HA0
  193.         End If
  194.         If (vers.fCapabilities And (16 \ (2 ^ j))) = (16 \ (2 ^ j)) Then
  195.             'We   don't   detect   a   ATAPI   device.
  196.             hdid9x = "Drive   " & CStr(j + 1) & "   is   a   ATAPI   device,   we   don't   detect   it"
  197.         Else
  198.             in_data.irDriveRegs.bCommandReg = &HEC
  199.             in_data.bDriveNumber = j
  200.             in_data.irDriveRegs.bSectorCountReg = 1
  201.             in_data.irDriveRegs.bSectorNumberReg = 1
  202.             in_data.cBufferSize = 512
  203.             lRet = DeviceIoControl(h, DFP_RECEIVE_DRIVE_DATA, in_data, Len(in_data), out_data, Len(out_data), ByVal i, olp)
  204.             If lRet = 0 Then
  205.                 hdid9x = "DeviceIoControl   failed:DFP_RECEIVE_DRIVE_DATA"
  206.                 CloseHandle (h)
  207.                 Exit Function
  208.             End If
  209.             Dim StrOut As String
  210.             CopyMemory phdinfo, out_data.bBuffer(0), Len(phdinfo)
  211.             CopyMemory s(0), phdinfo.sModelNumber(0), 40
  212.             s(40) = 0
  213.             ChangeByteOrder s, 40
  214.             StrOut = ByteArrToString(s, 40)
  215.             hdid9x = hdid9x & vbCrLf & "Module   Number:" & StrOut
  216.             CopyMemory s(0), phdinfo.sFirmwareRev(0), 8
  217.             s(8) = 0
  218.             ChangeByteOrder s, 8
  219.             StrOut = ByteArrToString(s, 8)
  220.             hdid9x = hdid9x & vbCrLf & "Firmware   rev:" & StrOut
  221.             CopyMemory s(0), phdinfo.sSerialNumber(0), 20
  222.             s(20) = 0
  223.             ChangeByteOrder s, 20
  224.             StrOut = ByteArrToString(s, 20)
  225.             hdid9x = hdid9x & vbCrLf & "Serial   Number:" & StrOut
  226.             CopyMemory s(0), phdinfo.ulTotalAddressableSectors(0), 4
  227.             s(5) = 0
  228.             Dim dblStrOut As Double
  229.             dblStrOut = ByteArrToLong(s)
  230.             hdid9x = hdid9x & vbCrLf & "Capacity:" & dblStrOut / 2 / 1024 & "M"
  231.         End If
  232.     Next j
  233.     'Close   handle   before   quit
  234.     CloseHandle (h)
  235.     CopyRight
  236. End Function
  237. Private Function hdidnt() As String
  238. Dim hd As String * 80
  239. Dim phdinfo As TIDSECTOR
  240. Dim s(40) As Byte
  241. Dim StrOut As String
  242.     hdidnt = ""
  243.     'We   start   in   NT/Win2000
  244.     For j = 0 To 3    '这里取四个硬盘的信息,因为正常PC不超过四个硬盘
  245.         hd = "\\.\PhysicalDrive" & CStr(j)
  246.         hdidnt = hdidnt & vbCrLf & hd
  247.         h = CreateFile(hd, GENERIC_READ Or GENERIC_WRITE, _
  248.                 FILE_SHARE_READ Or FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0)
  249.         Dim olpv As OVERLAPPED
  250.         Dim lRet As Long
  251.         lRet = DeviceIoControl(h, DFP_GET_VERSION, ByVal 0&, 0, vers, Len(vers), ByVal i, olpv)
  252.         If lRet = 0 Then
  253.             CloseHandle (h)
  254.         Else
  255.             'If   IDE   identify   command   not   supported,   fails
  256.             If (vers.fCapabilities And 1) <> 1 Then
  257.                 hdidnt = "Error:   IDE   identify   command   not   supported."
  258.                 CloseHandle (h)
  259.                 Exit Function
  260.             End If
  261.             'Identify   the   IDE   drives
  262.             If (j And 1) = 1 Then
  263.                 in_data.irDriveRegs.bDriveHeadReg = &HB0
  264.             Else
  265.                 in_data.irDriveRegs.bDriveHeadReg = &HA0
  266.             End If
  267.             If (vers.fCapabilities And (16 \ (2 ^ j))) <> 0 Then
  268.                 'We   don't   detect   a   ATAPI   device.
  269.                 hdidnt = hdidnt & vbCrLf & "Drive   " & CStr(j + 1) & "   is   a   ATAPI   device,   we   don't   detect   it"
  270.             Else
  271.                 in_data.irDriveRegs.bCommandReg = &HEC
  272.                 in_data.bDriveNumber = j
  273.                 in_data.irDriveRegs.bSectorCountReg = 1
  274.                 in_data.irDriveRegs.bSectorNumberReg = 1
  275.                 in_data.cBufferSize = 512
  276.                 Dim olpr As OVERLAPPED
  277.                 lRet = DeviceIoControl(h, DFP_RECEIVE_DRIVE_DATA, in_data, Len(in_data), out_data, Len(out_data), ByVal i, olpr)
  278.                 If lRet <= 0 Then
  279.                     hdidnt = hdidnt & vbCrLf & "DeviceIoControl   failed:DFP_RECEIVE_DRIVE_DATA"
  280.                     CloseHandle (h)
  281.                 Else
  282.                     CopyMemory phdinfo, out_data.bBuffer(0), Len(phdinfo)
  283.                     CopyMemory s(0), phdinfo.sModelNumber(0), 40
  284.                     s(40) = 0
  285.                     ChangeByteOrder s, 40
  286.                     StrOut = ByteArrToString(s, 40)
  287.                     hdidnt = hdidnt & vbCrLf & "Module   Number:" & StrOut
  288.                     CopyMemory s(0), phdinfo.sFirmwareRev(0), 8
  289.                     s(8) = 0
  290.                     ChangeByteOrder s, 8
  291.                     StrOut = ByteArrToString(s, 8)
  292.                     hdidnt = hdidnt & vbCrLf & "Firmware   rev:" & StrOut
  293.                     CopyMemory s(0), phdinfo.sSerialNumber(0), 20
  294.                     s(20) = 0
  295.                     ChangeByteOrder s, 20
  296.                     StrOut = ByteArrToString(s, 20)
  297.                     hdidnt = hdidnt & vbCrLf & "Serial   Number:" & StrOut
  298.                     CopyMemory s(0), phdinfo.ulTotalAddressableSectors(0), 4
  299.                     s(5) = 0
  300.                     Dim dblStrOut As Double
  301.                     dblStrOut = ByteArrToLong(s)
  302.                     hdidnt = hdidnt & vbCrLf & "Capacity:" & dblStrOut / 2 / 1024 & "M"
  303.                     CloseHandle (h)
  304.                 End If
  305.             End If
  306.         End If
  307.     Next j
  308.     CopyRight
  309. End Function
  310. Sub Main()
  311. Dim verinfo As OSVERSIONINFO
  312. Dim Ret As Long
  313.     verinfo.dwOSVersionInfoSize = Len(verinfo)
  314.     Ret = GetVersionEx(verinfo)
  315.     Dim OutStr As String
  316.     Select Case verinfo.dwPlatformId
  317.     Case VER_PLATFORM_WIN32S
  318.         MsgBox "Win32s   is   not   supported   by   this   programm."
  319.         End
  320.     Case VER_PLATFORM_WIN32_WINDOWS
  321.         OutStr = hdid9x
  322.         MsgBox OutStr
  323.         End
  324.     Case VER_PLATFORM_WIN32_NT
  325.         OutStr = hdidnt
  326.         MsgBox OutStr
  327.         End
  328.     End Select
  329. End Sub

  330. Private Function DetectIDE(bIDEDeviceMap As Byte) As String
  331.     If (bIDEDeviceMap And 1) Then
  332.         If (bIDEDeviceMap And 16) Then
  333.             DetectIDE = DetectIDE & "ATAPI   device   is   attached   to   primary   controller,   drive   0."
  334.         Else
  335.             DetectIDE = DetectIDE & "IDE   device   is   attached   to   primary   controller,   drive   0."
  336.         End If
  337.     End If
  338.     If (bIDEDeviceMap And 2) Then
  339.         If (bIDEDeviceMap And 32) Then
  340.             DetectIDE = DetectIDE & "ATAPI   device   is   attached   to   primary   controller,   drive   1."
  341.         Else
  342.             DetectIDE = DetectIDE & "IDE   device   is   attached   to   primary   controller,   drive   1."
  343.         End If
  344.     End If
  345.     If (bIDEDeviceMap And 4) Then
  346.         If (bIDEDeviceMap And 64) Then
  347.             DetectIDE = DetectIDE & "ATAPI   device   is   attached   to   secondary   controller,   drive   0."
  348.         Else
  349.             DetectIDE = DetectIDE & "IDE   device   is   attached   to   secondary   controller,   drive   0."
  350.         End If
  351.     End If
  352.     If (bIDEDeviceMap And 8) Then
  353.         If (bIDEDeviceMap And 128) Then
  354.             DetectIDE = DetectIDE & "ATAPI   device   is   attached   to   secondary   controller,   drive   1."
  355.         Else
  356.             DetectIDE = DetectIDE & "IDE   device   is   attached   to   secondary   controller,   drive   1."
  357.         End If
  358.     End If
  359. End Function

  360. Private Function ByteArrToString(inByte() As Byte, ByVal strlen As Integer) As String
  361. Dim i As Integer
  362.     For i = 0 To strlen
  363.         If inByte(i) = 0 Then
  364.             Exit For
  365.         End If
  366.         ByteArrToString = ByteArrToString & Chr(inByte(i))
  367.     Next i
  368. End Function
  369. Private Function ByteArrToLong(inByte() As Byte) As Double
  370. Dim i As Integer
  371.     For i = 0 To 3
  372.         ByteArrToLong = ByteArrToLong + CDbl(inByte(i)) * (256 ^ i)
  373.     Next i
  374. End Function


复制代码

评分

参与人数 1水晶币 +40 +40 收起 理由
HoviDelphic + 40 + 40 just for test

查看全部评分

【VB】QQ群:1422505加的请打上VB好友
【易语言】QQ群:9531809  或 177048
【FOXPRO】QQ群:6580324  或 33659603
【C/C++/VC】QQ群:3777552
【NiceBasic】QQ群:3703755

15

主题

73

回帖

0

精华

金牌会员

菜鸟No.1

积分
765
发表于 2010-3-28 19:36:15 | 显示全部楼层
这么好的东西怎能不顶。。。。
好好学习,天天想上!

2

主题

9

回帖

0

精华

初来乍到

积分
21
发表于 2010-4-21 10:28:46 | 显示全部楼层
顶!顶!顶!

280

主题

203

回帖

0

精华

版主

积分
1808
发表于 2010-4-23 11:16:55 | 显示全部楼层
好东西!!!
您需要登录后才可以回帖 登录 | 加入我们

本版积分规则

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