Question : vb.net - get hard drive serial

hello there,
how can I get the SMART hard drive serial number and if SMART is not enabled then just get the normal serial?

Answer : vb.net - get hard drive serial

create a new item in your project as class
remove any autogenerated code on it
and copy all code below in it

then just use it like:
MessageBox.Show(serial.GetHDDSerialNo())

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
260:
261:
262:
263:
264:
265:
266:
267:
268:
269:
270:
271:
272:
273:
274:
275:
276:
277:
278:
279:
280:
281:
282:
283:
284:
285:
286:
287:
288:
289:
290:
291:
292:
293:
294:
295:
296:
297:
298:
299:
300:
301:
302:
303:
304:
305:
306:
307:
308:
309:
310:
311:
312:
313:
314:
Imports System.Runtime.InteropServices 
 
Public Class SerialNo 
    ' Get the Hard disk serial no to pass the HMSite for generating the Key2 
    Public Function GetHDDSerialNo() As String 
        Dim query As System.Management.ManagementObjectSearcher
        Dim queryCollection As System.Management.ManagementObjectCollection
        Dim oq As System.Management.ObjectQuery
        Dim DriveName As String = ""
        Dim DriveSerialNo As String = "" 
        oq = New System.Management.ObjectQuery("SELECT * FROM Win32_DiskDrive")
        query = New System.Management.ManagementObjectSearcher(oq)
        queryCollection = query.Get()
        For Each mo As System.Management.ManagementObject In queryCollection
            If (mo.GetPropertyValue("SerialNumber") Is Nothing) Then
                Continue For 
            Else
                DriveSerialNo = mo.GetPropertyValue("SerialNumber")
            End If
        Next
        Return DriveSerialNo
    End Function
End Class
Friend Class DriveInfo
#Region "Enum"
    Enum DriveTypes
        Fixed
        Removable
        Unknown
    End Enum 
    Public Enum IDE_DRIVE_NUMBER
        PRIMARY_MASTER
        PRIMARY_SLAVE
        SECONDARY_MASTER
        SECONDARY_SLAVE
        TERTIARY_MASTER
        TERTIARY_SLAVE
        QUARTIARY_MASTER
        QUARTIARY_SLAVE
    End Enum
#End Region
#Region "Private Variable"
    Private _serialNumber As String
    Private _model As String
    Private _firmware As String 
    Private _driveType As DriveTypes = DriveTypes.Unknown
    Private _numberCylinders As Integer
    Private _numberHeads As Integer
    Private _sectorsPerTrack As Integer
    Private _bufferSize As Integer
#End Region
#Region "Constructor"
    Public Sub New(ByVal driveNumber As Integer) 
        Dim result As Boolean
        Dim handle As Integer
        Dim returnSize As Integer 
        Dim sci As New SENDCMDINPARAMS
        Dim sco As New SENDCMDOUTPARAMS 
        If Environment.OSVersion.Platform = PlatformID.Win32NT Then
            handle = CreateFile("\\.\PhysicalDrive" & CStr(driveNumber), GENERIC_READ Or GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0)
        Else ' 9X
            handle = CreateFile("\\.\Smartvsd", 0, 0, 0, CREATE_NEW, 0, 0)
        End If 
        If handle <> INVALID_HANDLE_VALUE Then 
            sci.DriveNumber = CType(driveNumber, Byte)
            sci.BufferSize = Marshal.SizeOf(sco)
            'sci.DriveRegs.DriveHead = CType(&HA0 Or (driveNumber << 4), Byte)
            'sci.DriveRegs.Command = &HEC
            sci.DriveRegs.SectorCount = 1
            sci.DriveRegs.SectorNumber = 1 
            sci.DriveRegs.Features = &HD0 '&HDA
            sci.DriveRegs.CylinderLow = SMART_CYL_LOW
            sci.DriveRegs.CylinderHigh = SMART_CYL_HI
            sci.DriveRegs.DriveHead = DRIVE_HEAD_REG
            sci.DriveRegs.Command = SMART_CMD 
            If DeviceIoControl(handle, SMART_SEND_DRIVE_COMMAND, sci, Marshal.SizeOf(sci), sco, Marshal.SizeOf(sco), returnSize, 0) <> 0 Then
                _serialNumber = SwapChars(sco.IDS.SerialNumber)
                _model = SwapChars(sco.IDS.ModelNumber)
                _firmware = SwapChars(sco.IDS.FirmwareRevision)
                _numberCylinders = sco.IDS.NumberCylinders
                _numberHeads = sco.IDS.NumberHeads
                _sectorsPerTrack = sco.IDS.SectorsPerTrack
                _bufferSize = sco.IDS.BufferSize * 512
                If (sco.IDS.GenConfig And &H80) = &H80 Then
                    _driveType = DriveTypes.Removable
                ElseIf (sco.IDS.GenConfig And &H40) = &H40 Then
                    _driveType = DriveTypes.Fixed
                Else
                    _driveType = DriveTypes.Unknown
                End If
            End If 

            CloseHandle(handle) 
        End If
    End Sub 
#End Region 
#Region "Win32 Interop"
     _
  Private Class IDEREGS
        Public Features As Byte
        Public SectorCount As Byte
        Public SectorNumber As Byte
        Public CylinderLow As Byte
        Public CylinderHigh As Byte
        Public DriveHead As Byte
        Public Command As Byte
        Public Reserved As Byte
    End Class
     _
  Private Class SENDCMDINPARAMS
        Public BufferSize As Integer
        Public DriveRegs As IDEREGS
        Public DriveNumber As Byte
         _
          Public Reserved() As Byte
         _
          Public Reserved2() As Integer 
        Public Sub New()
            DriveRegs = New IDEREGS
            Reserved = New Byte(2) {}
            Reserved2 = New Integer(3) {}
        End Sub 
    End Class 
     _
  Private Class DRIVERSTATUS 
        Public DriveError As Byte
        Public IDEStatus As Byte
         _
          Public Reserved() As Byte
         _
          Public Reserved2() As Integer 
        Public Sub New()
            Reserved = New Byte(1) {}
            Reserved2 = New Integer(1) {}
        End Sub 
    End Class 
     _
  Private Class IDSECTOR 
        Public GenConfig As Short '0
        Public NumberCylinders As Short '1
        Public Reserved As Short '2
        Public NumberHeads As Short '3
        Public BytesPerTrack As Short '4
        Public BytesPerSector As Short '5
        Public SectorsPerTrack As Short '6
         _
          Public VendorUnique() As Short '7
         _
          Public SerialNumber() As Char '10
        Public BufferClass As Short '20
        Public BufferSize As Short '21
        Public ECCSize As Short '22
         _
          Public FirmwareRevision() As Char '23
         _
          Public ModelNumber() As Char '27
        Public MoreVendorUnique As Short '47
        Public DoubleWordIO As Short '48
        Public Capabilities As Short '49
        Public Reserved1 As Short '50
        Public PIOTiming As Short '51
        Public DMATiming As Short '52
        Public BS As Short '53
        Public NumberCurrentCyls As Short '54
        Public NumberCurrentHeads As Short '55
        Public NumberCurrentSectorsPerTrack As Short '56
        Public CurrentSectorCapacity As Integer '57
        Public MultipleSectorCapacity As Short '59
        Public MultipleSectorStuff As Short '60
        Public TotalAddressableSectors As Integer '61
        Public SingleWordDMA As Short '63
        Public MultiWordDMA As Short '64
         _
          Public Reserved2() As Byte '65 
        Public Sub New()
            VendorUnique = New Short(2) {}
            Reserved2 = New Byte(381) {}
            FirmwareRevision = New Char(7) {}
            SerialNumber = New Char(19) {}
            ModelNumber = New Char(39) {}
        End Sub 
    End Class
     _
  Private Class SENDCMDOUTPARAMS 
        Public BufferSize As Integer
        Public Status As DRIVERSTATUS
        Public IDS As IDSECTOR 
        Public Sub New()
            Status = New DRIVERSTATUS
            IDS = New IDSECTOR
        End Sub
    End Class 
    Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Integer) As Integer
    Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Integer, ByVal dwShareMode As Integer, ByVal lpSecurityAttributes As Integer, ByVal dwCreationDisposition As Integer, ByVal dwFlagsAndAttributes As Integer, ByVal hTemplateFile As Integer) As Integer 
    Private Declare Function DeviceIoControl Lib "kernel32" (ByVal hDevice As Integer, ByVal dwIoControlCode As Integer, <[In](), Out()> ByVal lpInBuffer As SENDCMDINPARAMS, ByVal nInBufferSize As Integer, <[In](), Out()> ByVal lpOutBuffer As SENDCMDOUTPARAMS, ByVal nOutBufferSize As Integer, ByRef lpBytesReturned As Integer, ByVal lpOverlapped As Integer) As Integer 
    Private Const CREATE_NEW As Integer = 1
    Private Const OPEN_EXISTING As Integer = 3
    Private Const GENERIC_READ As Integer = &H80000000
    Private Const GENERIC_WRITE As Integer = &H40000000 
    Private Const FILE_SHARE_READ As Integer = &H1
    Private Const FILE_SHARE_WRITE As Integer = &H2 
    Private Const VER_PLATFORM_WIN32_NT As Integer = 2
    Private Const DFP_RECEIVE_DRIVE_DATA As Integer = &H7C088
    Private Const INVALID_HANDLE_VALUE As Integer = -1
    Private Const SMART_CYL_LOW = &H4F
    Private Const SMART_CYL_HI = &HC2 
    ' Construction/Destruction
    Private Const DRIVE_HEAD_REG = &HA0 
    ' Valid values for the bCommandReg member of IDEREGS.
    Private Const ATAPI_ID_CMD = &HA1 ' Returns ID sector for ATAPI.
    Private Const ID_CMD = &HEC ' Returns ID sector for ATA.
    Private Const SMART_CMD = &HB0 ' Performs SMART cmd.
    ' Requires valid bFeaturesReg,
    ' bCylLowReg, and bCylHighReg 
    ' SMART Command constants
    Private Const SMART_GET_VERSION = &H74080
    Private Const SMART_SEND_DRIVE_COMMAND = &H7C084
    Private Const SMART_RCV_DRIVE_DATA = &H7C088
    Private Shared Function SwapChars(ByVal chars() As Char) As String
        For i As Integer = 0 To chars.Length - 2 Step 2
            chars.Reverse(chars, i, 2)
        Next
        Return New String(chars).Trim
    End Function
#End Region 
#Region "Property"
    ReadOnly Property SerialNumber() As String
        Get
            Return _serialNumber
        End Get
    End Property 
    ReadOnly Property Model() As String
        Get
            Return _model
        End Get
    End Property 
    ReadOnly Property Firmware() As String
        Get
            Return _firmware
        End Get
    End Property
    ReadOnly Property NumberCylinders() As Integer
        Get
            Return _numberCylinders
        End Get
    End Property 
    ReadOnly Property NumberHeads() As Integer
        Get
            Return _numberHeads
        End Get
    End Property
    ReadOnly Property SectorsPerTrack() As Integer
        Get
            Return _sectorsPerTrack
        End Get
    End Property 
    ReadOnly Property BufferSize() As Integer
        Get
            Return _bufferSize
        End Get
    End Property 
    ReadOnly Property DriveType() As DriveTypes
        Get
            Return _driveType
        End Get
    End Property
#End Region
End Class
Random Solutions  
 
programming4us programming4us