Get Hard Disk Serial and CPUID in .Net

When we sell a software we may decide to make it work only on a specific computer generating a license key that works only for that PC. When the customer changes his hardware he has to ask for a new license key. We can identify a computer retrieving it’s CPU ID and it’s primary disk serial number. In .Net we can do this using the win32 APIs or WMI.
WMI stands “Windows Management Instrumentation”. WMI is a new management technology allowing scripts to monitor and control managed resources throughout the network. Resources include hard drives, file systems, operating system settings, processes, services, shares, registry settings, networking components, event logs, users, and groups. WMI is built into clients with Windows 2000 or above, and can be installed on any other 32-bit Windows client. (description from http://www.rlmueller.net/terms.htm)

Here is the code to query the infos we need:

Public Function GetHDSerial() As String
   Dim disk As New ManagementObject(_
       "Win32_LogicalDisk.DeviceID=""C:""")
   Dim diskPropertyA As PropertyData = _
       disk.Properties("VolumeSerialNumber")
   Return diskPropertyA.Value.ToString()
End Function
 
Public Function GetCPUId() As String
   Dim cpuInfo As String = String.Empty
   Dim temp As String = String.Empty
   Dim mc As ManagementClass = _
       New ManagementClass("Win32_Processor")
   Dim moc As ManagementObjectCollection =_
       mc.GetInstances()
   For Each mo As ManagementObject In moc
     If cpuInfo = String.Empty Then
        cpuInfo = _
         mo.Properties("ProcessorId").Value.ToString()
     End If
   Next
   Return cpuInfo
End Function

Download this code: hdserialandcpuid.vb.txt

there are a lot of ways to query devices info thowards WMI but it’s really difficult to find a good and complete documentation but i found on the web a good tool to explore wmi (WMI Explorer), you can download it here http://www.ks-soft.net/hostmon.eng/wmi/index.htm



Leave a Reply