Posted By

devubha on 07/06/09


Tagged


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

shasha
D4RKCODE


HardDisk Serial No


 / Published in: VB.NET
 

You must be administrator to get Drive Serial No

  1. Imports System.Runtime.InteropServices
  2.  
  3. Public Class SerialNo
  4. ' Get the Hard disk serial no to pass the HMSite for generating the Key2
  5. Public Function GetHDDSerialNo() As String
  6. Dim query As System.Management.ManagementObjectSearcher
  7. Dim queryCollection As System.Management.ManagementObjectCollection
  8. Dim oq As System.Management.ObjectQuery
  9. Dim DriveName As String = ""
  10. Dim DriveSerialNo As String = ""
  11.  
  12. oq = New System.Management.ObjectQuery("SELECT * FROM Win32_DiskDrive")
  13. query = New System.Management.ManagementObjectSearcher(oq)
  14. queryCollection = query.Get()
  15. For Each mo As System.Management.ManagementObject In queryCollection
  16. DriveName = mo("Name").ToString
  17. ' Retrive the serial number
  18. If IsNumeric(DriveName.Substring(DriveName.Length - 1)) Then
  19. Dim info As New DriveInfo(DriveName.Substring(DriveName.Length - 1))
  20. If Not info.SerialNumber Is Nothing Then
  21. DriveSerialNo = info.SerialNumber.ToString
  22. ' Replace Some specila char in some time this will create problem
  23. DriveSerialNo = DriveSerialNo.Replace(Chr(34), " ")
  24. DriveSerialNo = DriveSerialNo.Replace("'", " ")
  25. DriveSerialNo = DriveSerialNo.Replace(Chr(13), " ")
  26. Exit For
  27. End If
  28. End If
  29. Next
  30. Return DriveSerialNo
  31. End Function
  32. End Class
  33.  
  34. Friend Class DriveInfo
  35.  
  36. #Region "Enum"
  37. Enum DriveTypes
  38. Fixed
  39. Removable
  40. Unknown
  41. End Enum
  42.  
  43. Public Enum IDE_DRIVE_NUMBER
  44. PRIMARY_MASTER
  45. PRIMARY_SLAVE
  46. SECONDARY_MASTER
  47. SECONDARY_SLAVE
  48. TERTIARY_MASTER
  49. TERTIARY_SLAVE
  50. QUARTIARY_MASTER
  51. QUARTIARY_SLAVE
  52. End Enum
  53. #End Region
  54.  
  55. #Region "Private Variable"
  56. Private _serialNumber As String
  57. Private _model As String
  58. Private _firmware As String
  59.  
  60. Private _driveType As DriveTypes = DriveTypes.Unknown
  61. Private _numberCylinders As Integer
  62. Private _numberHeads As Integer
  63. Private _sectorsPerTrack As Integer
  64. Private _bufferSize As Integer
  65. #End Region
  66.  
  67. #Region "Constructor"
  68.  
  69. Public Sub New(ByVal driveNumber As Integer)
  70.  
  71. Dim result As Boolean
  72. Dim handle As Integer
  73. Dim returnSize As Integer
  74.  
  75. Dim sci As New SENDCMDINPARAMS
  76. Dim sco As New SENDCMDOUTPARAMS
  77.  
  78. If Environment.OSVersion.Platform = PlatformID.Win32NT Then
  79. handle = CreateFile("\\.\PhysicalDrive" & CStr(driveNumber), GENERIC_READ Or GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0)
  80. Else ' 9X
  81. handle = CreateFile("\\.\Smartvsd", 0, 0, 0, CREATE_NEW, 0, 0)
  82. End If
  83.  
  84. If handle <> INVALID_HANDLE_VALUE Then
  85.  
  86. sci.DriveNumber = CType(driveNumber, Byte)
  87. sci.BufferSize = Marshal.SizeOf(sco)
  88. 'sci.DriveRegs.DriveHead = CType(&HA0 Or (driveNumber << 4), Byte)
  89. 'sci.DriveRegs.Command = &HEC
  90. sci.DriveRegs.SectorCount = 1
  91. sci.DriveRegs.SectorNumber = 1
  92.  
  93. sci.DriveRegs.Features = &HD0 '&HDA
  94. sci.DriveRegs.CylinderLow = SMART_CYL_LOW
  95. sci.DriveRegs.CylinderHigh = SMART_CYL_HI
  96. sci.DriveRegs.DriveHead = DRIVE_HEAD_REG
  97. sci.DriveRegs.Command = SMART_CMD
  98.  
  99. 'If DeviceIoControl(handle, DFP_RECEIVE_DRIVE_DATA, sci, Marshal.SizeOf(sci), sco, Marshal.SizeOf(sco), returnSize, 0) <> 0 Then
  100. ' _serialNumber = SwapChars(sco.IDS.SerialNumber)
  101. ' _model = SwapChars(sco.IDS.ModelNumber)
  102. ' _firmware = SwapChars(sco.IDS.FirmwareRevision)
  103. ' _numberCylinders = sco.IDS.NumberCylinders
  104. ' _numberHeads = sco.IDS.NumberHeads
  105. ' _sectorsPerTrack = sco.IDS.SectorsPerTrack
  106. ' _bufferSize = sco.IDS.BufferSize * 512
  107. ' If (sco.IDS.GenConfig And &H80) = &H80 Then
  108. ' _driveType = DriveTypes.Removable
  109. ' ElseIf (sco.IDS.GenConfig And &H40) = &H40 Then
  110. ' _driveType = DriveTypes.Fixed
  111. ' Else
  112. ' _driveType = DriveTypes.Unknown
  113. ' End If
  114. 'End If
  115.  
  116. If DeviceIoControl(handle, SMART_SEND_DRIVE_COMMAND, sci, Marshal.SizeOf(sci), sco, Marshal.SizeOf(sco), returnSize, 0) <> 0 Then
  117. _serialNumber = SwapChars(sco.IDS.SerialNumber)
  118. _model = SwapChars(sco.IDS.ModelNumber)
  119. _firmware = SwapChars(sco.IDS.FirmwareRevision)
  120. _numberCylinders = sco.IDS.NumberCylinders
  121. _numberHeads = sco.IDS.NumberHeads
  122. _sectorsPerTrack = sco.IDS.SectorsPerTrack
  123. _bufferSize = sco.IDS.BufferSize * 512
  124. If (sco.IDS.GenConfig And &H80) = &H80 Then
  125. _driveType = DriveTypes.Removable
  126. ElseIf (sco.IDS.GenConfig And &H40) = &H40 Then
  127. _driveType = DriveTypes.Fixed
  128. Else
  129. _driveType = DriveTypes.Unknown
  130. End If
  131. End If
  132.  
  133.  
  134. CloseHandle(handle)
  135.  
  136. End If
  137. End Sub
  138.  
  139. #End Region
  140.  
  141. #Region "Win32 Interop"
  142.  
  143. <StructLayout(LayoutKind.Sequential, Size:=8)> _
  144. Private Class IDEREGS
  145. Public Features As Byte
  146. Public SectorCount As Byte
  147. Public SectorNumber As Byte
  148. Public CylinderLow As Byte
  149. Public CylinderHigh As Byte
  150. Public DriveHead As Byte
  151. Public Command As Byte
  152. Public Reserved As Byte
  153. End Class
  154.  
  155. <StructLayout(LayoutKind.Sequential, Size:=32)> _
  156. Private Class SENDCMDINPARAMS
  157.  
  158. Public BufferSize As Integer
  159. Public DriveRegs As IDEREGS
  160. Public DriveNumber As Byte
  161. <MarshalAs(UnmanagedType.ByValArray, SizeConst:=3)> _
  162. Public Reserved() As Byte
  163. <MarshalAs(UnmanagedType.ByValArray, SizeConst:=4)> _
  164. Public Reserved2() As Integer
  165.  
  166. Public Sub New()
  167. DriveRegs = New IDEREGS
  168. Reserved = New Byte(2) {}
  169. Reserved2 = New Integer(3) {}
  170. End Sub
  171.  
  172. End Class
  173.  
  174. <StructLayout(LayoutKind.Sequential, Size:=12)> _
  175. Private Class DRIVERSTATUS
  176.  
  177. Public DriveError As Byte
  178. Public IDEStatus As Byte
  179. <MarshalAs(UnmanagedType.ByValArray, SizeConst:=2)> _
  180. Public Reserved() As Byte
  181. <MarshalAs(UnmanagedType.ByValArray, SizeConst:=2)> _
  182. Public Reserved2() As Integer
  183.  
  184. Public Sub New()
  185. Reserved = New Byte(1) {}
  186. Reserved2 = New Integer(1) {}
  187. End Sub
  188.  
  189. End Class
  190.  
  191. <StructLayout(LayoutKind.Sequential)> _
  192. Private Class IDSECTOR
  193.  
  194. Public GenConfig As Short '0
  195. Public NumberCylinders As Short '1
  196. Public Reserved As Short '2
  197. Public NumberHeads As Short '3
  198. Public BytesPerTrack As Short '4
  199. Public BytesPerSector As Short '5
  200. Public SectorsPerTrack As Short '6
  201. <MarshalAs(UnmanagedType.ByValArray, SizeConst:=3)> _
  202. Public VendorUnique() As Short '7
  203. <MarshalAs(UnmanagedType.ByValArray, SizeConst:=20)> _
  204. Public SerialNumber() As Char '10
  205. Public BufferClass As Short '20
  206. Public BufferSize As Short '21
  207. Public ECCSize As Short '22
  208. <MarshalAs(UnmanagedType.ByValArray, SizeConst:=8)> _
  209. Public FirmwareRevision() As Char '23
  210. <MarshalAs(UnmanagedType.ByValArray, SizeConst:=40)> _
  211. Public ModelNumber() As Char '27
  212. Public MoreVendorUnique As Short '47
  213. Public DoubleWordIO As Short '48
  214. Public Capabilities As Short '49
  215. Public Reserved1 As Short '50
  216. Public PIOTiming As Short '51
  217. Public DMATiming As Short '52
  218. Public BS As Short '53
  219. Public NumberCurrentCyls As Short '54
  220. Public NumberCurrentHeads As Short '55
  221. Public NumberCurrentSectorsPerTrack As Short '56
  222. Public CurrentSectorCapacity As Integer '57
  223. Public MultipleSectorCapacity As Short '59
  224. Public MultipleSectorStuff As Short '60
  225. Public TotalAddressableSectors As Integer '61
  226. Public SingleWordDMA As Short '63
  227. Public MultiWordDMA As Short '64
  228. <MarshalAs(UnmanagedType.ByValArray, SizeConst:=382)> _
  229. Public Reserved2() As Byte '65
  230.  
  231. Public Sub New()
  232. VendorUnique = New Short(2) {}
  233. Reserved2 = New Byte(381) {}
  234. FirmwareRevision = New Char(7) {}
  235. SerialNumber = New Char(19) {}
  236. ModelNumber = New Char(39) {}
  237. End Sub
  238.  
  239. End Class
  240.  
  241. <StructLayout(LayoutKind.Sequential)> _
  242. Private Class SENDCMDOUTPARAMS
  243.  
  244. Public BufferSize As Integer
  245. Public Status As DRIVERSTATUS
  246. Public IDS As IDSECTOR
  247.  
  248. Public Sub New()
  249. Status = New DRIVERSTATUS
  250. IDS = New IDSECTOR
  251. End Sub
  252.  
  253. End Class
  254.  
  255. Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Integer) As Integer
  256. 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
  257.  
  258. 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
  259.  
  260. Private Const CREATE_NEW As Integer = 1
  261. Private Const OPEN_EXISTING As Integer = 3
  262.  
  263. Private Const GENERIC_READ As Integer = &H80000000
  264. Private Const GENERIC_WRITE As Integer = &H40000000
  265.  
  266. Private Const FILE_SHARE_READ As Integer = &H1
  267. Private Const FILE_SHARE_WRITE As Integer = &H2
  268.  
  269. Private Const VER_PLATFORM_WIN32_NT As Integer = 2
  270.  
  271. Private Const DFP_RECEIVE_DRIVE_DATA As Integer = &H7C088
  272.  
  273. Private Const INVALID_HANDLE_VALUE As Integer = -1
  274.  
  275. ' Cylinder register defines for SMART command
  276. Private Const SMART_CYL_LOW = &H4F
  277. Private Const SMART_CYL_HI = &HC2
  278.  
  279. ' Construction/Destruction
  280. Private Const DRIVE_HEAD_REG = &HA0
  281.  
  282. ' Valid values for the bCommandReg member of IDEREGS.
  283. Private Const ATAPI_ID_CMD = &HA1 ' Returns ID sector for ATAPI.
  284. Private Const ID_CMD = &HEC ' Returns ID sector for ATA.
  285. Private Const SMART_CMD = &HB0 ' Performs SMART cmd.
  286. ' Requires valid bFeaturesReg,
  287. ' bCylLowReg, and bCylHighReg
  288.  
  289. ' SMART Command constants
  290. Private Const SMART_GET_VERSION = &H74080
  291. Private Const SMART_SEND_DRIVE_COMMAND = &H7C084
  292. Private Const SMART_RCV_DRIVE_DATA = &H7C088
  293.  
  294.  
  295. Private Shared Function SwapChars(ByVal chars() As Char) As String
  296. For i As Integer = 0 To chars.Length - 2 Step 2
  297. chars.Reverse(chars, i, 2)
  298. Next
  299. Return New String(chars).Trim
  300. End Function
  301.  
  302. #End Region
  303.  
  304. #Region "Property"
  305. ReadOnly Property SerialNumber() As String
  306. Get
  307. Return _serialNumber
  308. End Get
  309. End Property
  310.  
  311. ReadOnly Property Model() As String
  312. Get
  313. Return _model
  314. End Get
  315. End Property
  316.  
  317. ReadOnly Property Firmware() As String
  318. Get
  319. Return _firmware
  320. End Get
  321. End Property
  322.  
  323. ReadOnly Property NumberCylinders() As Integer
  324. Get
  325. Return _numberCylinders
  326. End Get
  327. End Property
  328.  
  329. ReadOnly Property NumberHeads() As Integer
  330. Get
  331. Return _numberHeads
  332. End Get
  333. End Property
  334.  
  335. ReadOnly Property SectorsPerTrack() As Integer
  336. Get
  337. Return _sectorsPerTrack
  338. End Get
  339. End Property
  340.  
  341. ReadOnly Property BufferSize() As Integer
  342. Get
  343. Return _bufferSize
  344. End Get
  345. End Property
  346.  
  347. ReadOnly Property DriveType() As DriveTypes
  348. Get
  349. Return _driveType
  350. End Get
  351. End Property
  352. #End Region
  353.  
  354. End Class

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: prashant_rasal_73yahoocom on September 3, 2009

but how to use this code whenever i try to call GetHDDSerialNo function on button clik event it not shown me

You need to login to post a comment.