Code Snippets

  

Visual Basic Source Code


Welcome to Dream.In.Code
Getting VB Help is Easy!

Join 105,765 VB Programmers for FREE! Ask your question and get quick answers from experts. There are 1,589 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!




HD serial Number (Hardcoded One)

Getting the unchangeable Hard Disk Serial Number.

Submitted By: Nitish4uall
Actions:
Rating:
Views: 8,446

Language: Visual Basic

Last Modified: May 26, 2006
Instructions: 'Add a listbox - name=list1
'Add a commanbutton - name=command1

Snippet


  1. 'Add a listbox  - name=list1
  2. 'Add a commanbutton - name=command1
  3.  
  4. Option Explicit
  5.  
  6. Private Const GENERIC_READ = &H80000000
  7. Private Const GENERIC_WRITE = &H40000000
  8. Private Const FILE_SHARE_READ = &H1
  9. Private Const FILE_SHARE_WRITE = &H2
  10. Private Const OPEN_EXISTING = 3
  11. Private Const CREATE_NEW = 1
  12. Private Const INVALID_HANDLE_VALUE = -1
  13. Private Const VER_PLATFORM_WIN32_NT = 2
  14. Private Const IDENTIFY_BUFFER_SIZE = 512
  15. Private Const OUTPUT_DATA_SIZE = IDENTIFY_BUFFER_SIZE + 16
  16.  
  17. 'GETVERSIONOUTPARAMS contains the data returned
  18. 'from the Get Driver Version function
  19. Private Type GETVERSIONOUTPARAMS
  20.    bVersion       As Byte 'Binary driver version.
  21.    bRevision      As Byte 'Binary driver revision
  22.    bReserved      As Byte 'Not used
  23.    bIDEDeviceMap  As Byte 'Bit map of IDE devices
  24.    fCapabilities  As Long 'Bit mask of driver capabilities
  25.    dwReserved(3)  As Long 'For future use
  26. End Type
  27.  
  28. 'IDE registers
  29. Private Type IDEREGS
  30.    bFeaturesReg     As Byte 'Used for specifying SMART "commands"
  31.    bSectorCountReg  As Byte 'IDE sector count register
  32.    bSectorNumberReg As Byte 'IDE sector number register
  33.    bCylLowReg       As Byte 'IDE low order cylinder value
  34.    bCylHighReg      As Byte 'IDE high order cylinder value
  35.    bDriveHeadReg    As Byte 'IDE drive/head register
  36.    bCommandReg      As Byte 'Actual IDE command
  37.    bReserved        As Byte 'reserved for future use - must be zero
  38. End Type
  39.  
  40. 'SENDCMDINPARAMS contains the input parameters for the
  41. 'Send Command to Drive function
  42. Private Type SENDCMDINPARAMS
  43.    cBufferSize     As Long     'Buffer size in bytes
  44.    irDriveRegs     As IDEREGS  'Structure with drive register values.
  45.    bDriveNumber    As Byte     'Physical drive number to send command to (0,1,2,3).
  46.    bReserved(2)    As Byte     'Bytes reserved
  47.    dwReserved(3)   As Long     'DWORDS reserved
  48.    bBuffer()      As Byte      'Input buffer.
  49. End Type
  50.  
  51. 'Valid values for the bCommandReg member of IDEREGS.
  52. Private Const IDE_ID_FUNCTION = &HEC            'Returns ID sector for ATA.
  53. Private Const IDE_EXECUTE_SMART_FUNCTION = &HB0 'Performs SMART cmd.
  54.                                                 'Requires valid bFeaturesReg,
  55.                                                 'bCylLowReg, and bCylHighReg
  56.  
  57. 'Cylinder register values required when issuing SMART command
  58. Private Const SMART_CYL_LOW = &H4F
  59. Private Const SMART_CYL_HI = &HC2
  60.  
  61. 'Status returned from driver
  62. Private Type DRIVERSTATUS
  63.    bDriverError  As Byte          'Error code from driver, or 0 if no error
  64.    bIDEStatus    As Byte          'Contents of IDE Error register
  65.                                   'Only valid when bDriverError is SMART_IDE_ERROR
  66.    bReserved(1)  As Byte
  67.    dwReserved(1) As Long
  68. End Type
  69.  
  70. Private Type IDSECTOR
  71.    wGenConfig                 As Integer
  72.    wNumCyls                   As Integer
  73.    wReserved                  As Integer
  74.    wNumHeads                  As Integer
  75.    wBytesPerTrack             As Integer
  76.    wBytesPerSector            As Integer
  77.    wSectorsPerTrack           As Integer
  78.    wVendorUnique(2)           As Integer
  79.    sSerialNumber(19)          As Byte
  80.    wBufferType                As Integer
  81.    wBufferSize                As Integer
  82.    wECCSize                   As Integer
  83.    sFirmwareRev(7)            As Byte
  84.    sModelNumber(39)           As Byte
  85.    wMoreVendorUnique          As Integer
  86.    wDoubleWordIO              As Integer
  87.    wCapabilities              As Integer
  88.    wReserved1                 As Integer
  89.    wPIOTiming                 As Integer
  90.    wDMATiming                 As Integer
  91.    wBS                        As Integer
  92.    wNumCurrentCyls            As Integer
  93.    wNumCurrentHeads           As Integer
  94.    wNumCurrentSectorsPerTrack As Integer
  95.    ulCurrentSectorCapacity    As Long
  96.    wMultSectorStuff           As Integer
  97.    ulTotalAddressableSectors  As Long
  98.    wSingleWordDMA             As Integer
  99.    wMultiWordDMA              As Integer
  100.    bReserved(127)             As Byte
  101. End Type
  102.  
  103. 'Structure returned by SMART IOCTL commands
  104. Private Type SENDCMDOUTPARAMS
  105.   cBufferSize   As Long         'Size of Buffer in bytes
  106.   DRIVERSTATUS  As DRIVERSTATUS 'Driver status structure
  107.   bBuffer()    As Byte          'Buffer of arbitrary length for data read from drive
  108. End Type
  109.  
  110. 'Vendor specific feature register defines
  111. 'for SMART "sub commands"
  112. Private Const SMART_ENABLE_SMART_OPERATIONS = &HD8
  113.  
  114. 'Status Flags Values
  115. Public Enum STATUS_FLAGS
  116.    PRE_FAILURE_WARRANTY = &H1
  117.    ON_LINE_COLLECTION = &H2
  118.    PERFORMANCE_ATTRIBUTE = &H4
  119.    ERROR_RATE_ATTRIBUTE = &H8
  120.    EVENT_COUNT_ATTRIBUTE = &H10
  121.    SELF_PRESERVING_ATTRIBUTE = &H20
  122. End Enum
  123.  
  124. 'IOCTL commands
  125. Private Const DFP_GET_VERSION = &H74080
  126. Private Const DFP_SEND_DRIVE_COMMAND = &H7C084
  127. Private Const DFP_RECEIVE_DRIVE_DATA = &H7C088
  128.  
  129. Private Type ATTR_DATA
  130.    AttrID As Byte
  131.    AttrName As String
  132.    AttrValue As Byte
  133.    ThresholdValue As Byte
  134.    WorstValue As Byte
  135.    StatusFlags As STATUS_FLAGS
  136. End Type
  137.  
  138. Private Type DRIVE_INFO
  139.    bDriveType As Byte
  140.    SerialNumber As String
  141.    Model As String
  142.    FirmWare As String
  143.    Cilinders As Long
  144.    Heads As Long
  145.    SecPerTrack As Long
  146.    BytesPerSector As Long
  147.    BytesperTrack As Long
  148.    NumAttributes As Byte
  149.    Attributes() As ATTR_DATA
  150. End Type
  151.  
  152. Private Enum IDE_DRIVE_NUMBER
  153.    PRIMARY_MASTER
  154.    PRIMARY_SLAVE
  155.    SECONDARY_MASTER
  156.    SECONDARY_SLAVE
  157.    TERTIARY_MASTER
  158.    TERTIARY_SLAVE
  159.    QUARTIARY_MASTER
  160.    QUARTIARY_SLAVE
  161. End Enum
  162.  
  163. Private Declare Function CreateFile Lib "kernel32" _
  164.    Alias "CreateFileA" _
  165.   (ByVal lpFileName As String, _
  166.    ByVal dwDesiredAccess As Long, _
  167.    ByVal dwShareMode As Long, _
  168.    lpSecurityAttributes As Any, _
  169.    ByVal dwCreationDisposition As Long, _
  170.    ByVal dwFlagsAndAttributes As Long, _
  171.    ByVal hTemplateFile As Long) As Long
  172.  
  173. Private Declare Function CloseHandle Lib "kernel32" _
  174.   (ByVal hObject As Long) As Long
  175.  
  176. Private Declare Function DeviceIoControl Lib "kernel32" _
  177.   (ByVal hDevice As Long, _
  178.    ByVal dwIoControlCode As Long, _
  179.    lpInBuffer As Any, _
  180.    ByVal nInBufferSize As Long, _
  181.    lpOutBuffer As Any, _
  182.    ByVal nOutBufferSize As Long, _
  183.    lpBytesReturned As Long, _
  184.    lpOverlapped As Any) As Long
  185.  
  186. Private Declare Sub CopyMemory Lib "kernel32" _
  187.    Alias "RtlMoveMemory" _
  188.   (hpvDest As Any, _
  189.    hpvSource As Any, _
  190.    ByVal cbCopy As Long)
  191.  
  192. Private Type OSVERSIONINFO
  193.    OSVSize As Long
  194.    dwVerMajor As Long
  195.    dwVerMinor As Long
  196.    dwBuildNumber As Long
  197.    PlatformID As Long
  198.    szCSDVersion As String * 128
  199. End Type
  200.  
  201. Private Declare Function GetVersionEx Lib "kernel32" _
  202.    Alias "GetVersionExA" _
  203.   (LpVersionInformation As OSVERSIONINFO) As Long
  204.  
  205.  
  206.  
  207. Private Sub Form_Load()
  208.  
  209.    Command1.Caption = "Get Drive Info"
  210.       Dim di As DRIVE_INFO
  211. di = GetDriveInfo(0)
  212. Me.Caption = Trim$(di.Model) & vbCrLf
  213. Me.Caption = Me.Caption & " - " & Trim$(di.SerialNumber)
  214.  
  215. End Sub
  216.  
  217.  
  218. Private Sub Command1_Click()
  219.  
  220.    Dim di As DRIVE_INFO
  221.    Dim drvNumber As Long
  222.    
  223.    For drvNumber = PRIMARY_MASTER To QUARTIARY_SLAVE
  224.    
  225.       di = GetDriveInfo(drvNumber)
  226.      
  227.       List1.AddItem "Drive " & drvNumber
  228.      
  229.       With di
  230.      
  231.          Select Case .bDriveType
  232.             Case 0
  233.                List1.AddItem vbTab & "[Not present]"
  234.             Case 1
  235.                List1.AddItem vbTab & "Model:" & vbTab & Trim$(.Model)
  236.                List1.AddItem vbTab & "Serial No:" & vbTab & Trim$(.SerialNumber)
  237.             Case 2
  238.                List1.AddItem vbTab & "[ATAPI drive - info not available]"
  239.             Case Else
  240.                List1.AddItem vbTab & "[drive type not known]"
  241.          End Select
  242.          
  243.       End With
  244.      
  245.    Next
  246.    
  247. End Sub
  248.  
  249.  
  250. Private Function GetDriveInfo(drvNumber As IDE_DRIVE_NUMBER) As DRIVE_INFO
  251.    
  252.    Dim hDrive As Long
  253.    Dim di As DRIVE_INFO
  254.    
  255.    hDrive = SmartOpen(drvNumber)
  256.    
  257.    If hDrive <> INVALID_HANDLE_VALUE Then
  258.    
  259.       If SmartGetVersion(hDrive) = True Then
  260.      
  261.          With di
  262.             .bDriveType = 0
  263.             .NumAttributes = 0
  264.             ReDim .Attributes(0)
  265.             .bDriveType = 1
  266.          End With
  267.          
  268.          If SmartCheckEnabled(hDrive, drvNumber) Then
  269.            
  270.             If IdentifyDrive(hDrive, IDE_ID_FUNCTION, drvNumber, di) = True Then
  271.          
  272.                GetDriveInfo = di
  273.                
  274.             End If   'IdentifyDrive
  275.          End If   'SmartCheckEnabled
  276.       End If   'SmartGetVersion
  277.    End If   'hDrive <> INVALID_HANDLE_VALUE
  278.    
  279.    CloseHandle hDrive
  280.    
  281. End Function
  282.  
  283.  
  284. Private Function IdentifyDrive(ByVal hDrive As Long, _
  285.                                ByVal IDCmd As Byte, _
  286.                                ByVal drvNumber As IDE_DRIVE_NUMBER, _
  287.                                di As DRIVE_INFO) As Boolean
  288.    
  289.   'Function: Send an IDENTIFY command to the drive
  290.   'drvNumber = 0-3
  291.   'IDCmd = IDE_ID_FUNCTION or IDE_ATAPI_ID
  292.    Dim SCIP As SENDCMDINPARAMS
  293.    Dim IDSEC As IDSECTOR
  294.    Dim bArrOut(OUTPUT_DATA_SIZE - 1) As Byte
  295.    Dim cbBytesReturned As Long
  296.    
  297.    With SCIP
  298.       .cBufferSize = IDENTIFY_BUFFER_SIZE
  299.       .bDriveNumber = CByte(drvNumber)
  300.        
  301.       With .irDriveRegs
  302.          .bFeaturesReg = 0
  303.          .bSectorCountReg = 1
  304.          .bSectorNumberReg = 1
  305.          .bCylLowReg = 0
  306.          .bCylHighReg = 0
  307.          .bDriveHeadReg = &HA0 'compute the drive number
  308.          If Not IsWinNT4Plus Then
  309.             .bDriveHeadReg = .bDriveHeadReg Or ((drvNumber And 1) * 16)
  310.          End If
  311.          'the command can either be IDE
  312.          'identify or ATAPI identify.
  313.          .bCommandReg = CByte(IDCmd)
  314.       End With
  315.    End With
  316.    
  317.    If DeviceIoControl(hDrive, _
  318.                       DFP_RECEIVE_DRIVE_DATA, _
  319.                       SCIP, _
  320.                       Len(SCIP) - 4, _
  321.                       bArrOut(0), _
  322.                       OUTPUT_DATA_SIZE, _
  323.                       cbBytesReturned, _
  324.                       ByVal 0&) Then
  325.                      
  326.       CopyMemory IDSEC, bArrOut(16), Len(IDSEC)
  327.  
  328.       di.Model = StrConv(SwapBytes(IDSEC.sModelNumber), vbUnicode)
  329.       di.SerialNumber = StrConv(SwapBytes(IDSEC.sSerialNumber), vbUnicode)
  330.      
  331.       IdentifyDrive = True
  332.      
  333.     End If
  334.    
  335. End Function
  336.  
  337.  
  338. Private Function IsWinNT4Plus() As Boolean
  339.  
  340.   'returns True if running Windows NT4 or later
  341.    Dim osv As OSVERSIONINFO
  342.  
  343.    osv.OSVSize = Len(osv)
  344.  
  345.    If GetVersionEx(osv) = 1 Then
  346.    
  347.       IsWinNT4Plus = (osv.PlatformID = VER_PLATFORM_WIN32_NT) And _
  348.                      (osv.dwVerMajor >= 4)
  349.  
  350.    End If
  351.  
  352. End Function
  353.  
  354.  
  355. Private Function SmartCheckEnabled(ByVal hDrive As Long, _
  356.                                    drvNumber As IDE_DRIVE_NUMBER) As Boolean
  357.    
  358.   'SmartCheckEnabled - Check if SMART enable
  359.   'FUNCTION: Send a SMART_ENABLE_SMART_OPERATIONS command to the drive
  360.   'bDriveNum = 0-3
  361.    Dim SCIP As SENDCMDINPARAMS
  362.    Dim SCOP As SENDCMDOUTPARAMS
  363.    Dim cbBytesReturned As Long
  364.    
  365.    With SCIP
  366.    
  367.       .cBufferSize = 0
  368.      
  369.       With .irDriveRegs
  370.            .bFeaturesReg = SMART_ENABLE_SMART_OPERATIONS
  371.            .bSectorCountReg = 1
  372.            .bSectorNumberReg = 1
  373.            .bCylLowReg = SMART_CYL_LOW
  374.            .bCylHighReg = SMART_CYL_HI
  375.  
  376.            .bDriveHeadReg = &HA0
  377.             If Not IsWinNT4Plus Then
  378.                .bDriveHeadReg = .bDriveHeadReg Or ((drvNumber And 1) * 16)
  379.             End If
  380.            .bCommandReg = IDE_EXECUTE_SMART_FUNCTION
  381.            
  382.        End With
  383.        
  384.        .bDriveNumber = drvNumber
  385.        
  386.    End With
  387.    
  388.    SmartCheckEnabled = DeviceIoControl(hDrive, _
  389.                                       DFP_SEND_DRIVE_COMMAND, _
  390.                                       SCIP, _
  391.                                       Len(SCIP) - 4, _
  392.                                       SCOP, _
  393.                                       Len(SCOP) - 4, _
  394.                                       cbBytesReturned, _
  395.                                       ByVal 0&)
  396. End Function
  397.  
  398.  
  399. Private Function SmartGetVersion(ByVal hDrive As Long) As Boolean
  400.    
  401.    Dim cbBytesReturned As Long
  402.    Dim GVOP As GETVERSIONOUTPARAMS
  403.    
  404.    SmartGetVersion = DeviceIoControl(hDrive, _
  405.                                      DFP_GET_VERSION, _
  406.                                      ByVal 0&, 0, _
  407.                                      GVOP, _
  408.                                      Len(GVOP), _
  409.                                      cbBytesReturned, _
  410.                                      ByVal 0&)
  411.    
  412. End Function
  413.  
  414.  
  415. Private Function SmartOpen(drvNumber As IDE_DRIVE_NUMBER) As Long
  416.  
  417.   'Open SMART to allow DeviceIoControl
  418.   'communications and return SMART handle
  419.  
  420.    If IsWinNT4Plus() Then
  421.      
  422.       SmartOpen = CreateFile("\\.\PhysicalDrive" & CStr(drvNumber), _
  423.                              GENERIC_READ Or GENERIC_WRITE, _
  424.                              FILE_SHARE_READ Or FILE_SHARE_WRITE, _
  425.                              ByVal 0&, _
  426.                              OPEN_EXISTING, _
  427.                              0&, _
  428.                              0&)
  429.  
  430.    Else
  431.      
  432.       SmartOpen = CreateFile("\\.\SMARTVSD", _
  433.                               0&, 0&, _
  434.                               ByVal 0&, _
  435.                               CREATE_NEW, _
  436.                               0&, _
  437.                               0&)
  438.    End If
  439.    
  440. End Function
  441.  
  442.  
  443. Private Function SwapBytes(b() As Byte) As Byte()
  444.    
  445.    
  446.    Dim bTemp As Byte
  447.    Dim cnt As Long
  448.  
  449.    For cnt = LBound(b) To UBound(b) Step 2
  450.       bTemp = b(cnt)
  451.       b(cnt) = b(cnt + 1)
  452.       b(cnt + 1) = bTemp
  453.    Next cnt
  454.      
  455.    SwapBytes = b()
  456.      
  457. End Function

Copy & Paste


Comments


rupesh_11 2008-07-31 05:38:19

Hmmm.. Great... Thanks, rupesh11@gmail.com


Add comment


You must be registered and logged on to </dream.in.code> to leave comments.