1 #Region "Microsoft.VisualBasic::1ea143024c7b53f0c1612bae5127ed5d, Microsoft.VisualBasic.Core\ApplicationServices\Tools\Win32\HardwareInfo.vb"
2
3     ' Author:
4     
5     '       asuka (amethyst.asuka@gcmodeller.org)
6     '       xie (genetics@smrucc.org)
7     '       xieguigang (xie.guigang@live.com)
8     
9     ' Copyright (c) 2018 GPL3 Licensed
10     
11     
12     ' GNU GENERAL PUBLIC LICENSE (GPL3)
13     
14     
15     ' This program is free software: you can redistribute it and/or modify
16     ' it under the terms of the GNU General Public License as published by
17     ' the Free Software Foundation, either version 3 of the License, or
18     ' (at your option) any later version.
19     
20     ' This program is distributed in the hope that it will be useful,
21     ' but WITHOUT ANY WARRANTY; without even the implied warranty of
22     ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23     ' GNU General Public License for more details.
24     
25     ' You should have received a copy of the GNU General Public License
26     ' along with this program. If not, see <http://www.gnu.org/licenses/>.
27
28
29
30     ' /********************************************************************************/
31
32     ' Summaries:
33
34     '     Module HardwareInfo
35     
36     '         Function: CPU_Id, HarddriveInfo, SystemSerialNumber
37     
38     '     Class HardDrive
39     
40     '         Properties: Model, SerialNo, Type
41     
42     '         FunctionToString
43     
44     
45     ' /********************************************************************************/
46
47 #End Region
48
49 Imports System.Management
50 Imports Microsoft.VisualBasic.Language
51 Imports Microsoft.VisualBasic.Serialization.JSON
52
53 Namespace Win32
54
55     ''' <summary>
56     ''' Get mother board serial numbers and CPU IDs in Visual Basic .NET
57     ''' </summary>
58     Public Module HardwareInfo
59
60         ''' <summary>
61         ''' The following function gets a WMI object and then gets a collection of WMI_BaseBoard objects 
62         ''' representing the system's mother boards. It loops through them getting their serial numbers. 
63         ''' </summary>
64         ''' <returns></returns>
65         Public Function SystemSerialNumber() As String
66             Get the Windows Management Instrumentation object.
67             Dim wmi As Object = GetObject("WinMgmts:")
68             ' Get the "base boards" (mother boards).
69             Dim serial_numbers As New List(Of String)
70             Dim mother_boards As Object = wmi.InstancesOf("Win32_BaseBoard")
71
72             For Each board As Object In mother_boards
73                 serial_numbers += CStr(board.SerialNumber)
74             Next
75
76             Dim uid As String = String.Join("-", serial_numbers.ToArray)
77             Return uid
78         End Function
79
80         ''' <summary>
81         ''' The following code gets a WMI object and selects Win32_Processor objects. It loops through them getting their processor IDs. 
82         ''' </summary>
83         ''' <returns></returns>
84         Public Function CPU_Id() As String
85             Dim computer As String = "."
86             Dim wmi As Object = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & computer & "\root\cimv2")
87             Dim processors As Object = wmi.ExecQuery("Select * from Win32_Processor")
88             Dim cpu_ids As New List(Of String)
89
90             For Each cpu As Object In processors
91                 cpu_ids += CStr(cpu.ProcessorId)
92             Next
93
94             Dim uid As String = String.Join("-", cpu_ids.ToArray)
95             Return uid
96         End Function
97
98         ''' <summary>
99         ''' How to Retrieve the REAL Hard Drive Serial Number.
100         ''' 
101         ''' Shows you how to obtain the hardware serial number set by the manufacturer and 
102         ''' not the Volume Serial Number that changes after you format a hard drive.
103         ''' </summary>
104         ''' <returns></returns>
105         ''' <remarks>
106         ''' http://www.codeproject.com/Articles/6077/How-to-Retrieve-the-REAL-Hard-Drive-Serial-Number
107         ''' </remarks>
108         Public Function HarddriveInfo() As HardDrive()
109             Dim hds As New List(Of HardDrive)
110             Dim searcher As New ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive")
111             Dim i As Pointer = 0
112
113             For Each wmi_HD As ManagementObject In searcher.[Get]()
114                 Dim model As String = wmi_HD("Model").ToString()
115                 Dim type As String = wmi_HD("InterfaceType").ToString()
116
117                 hds += New HardDrive With {
118                     .Model = model,
119                     .Type = type
120                 }
121             Next
122
123             searcher = New ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia")
124
125             For Each wmi_HD As ManagementObject In searcher.[Get]()
126                 ' get the hard drive from collection
127                 ' using index
128                 Dim hd As HardDrive = hds(++i)
129
130                 ' get the hardware serial no.
131                 If wmi_HD("SerialNumber"Is Nothing Then
132                     hd.SerialNo = "None"
133                 Else
134                     hd.SerialNo = wmi_HD("SerialNumber").ToString()
135                 End If
136             Next
137
138             Return hds
139         End Function
140     End Module
141
142     Public Class HardDrive
143
144         Public Property Model As String
145         Public Property Type As String
146         Public Property SerialNo As String
147
148         Public Overrides Function ToString() As String
149             Return Me.GetJson
150         End Function
151     End Class
152 End Namespace