'--------------------------------
'This is vb.net code. It will not work with vb6
'or earlier.
'--------------------------------
Option Strict On
Imports System.management
'--------------------------------
'If System.management (above) is underlined in blue, then you need
'to add it to the project references. Click on Project, Add Reference
'Click on the .NET tab, then select System.management from the table.
'Click on Select then OK.
'--------------------------------
Module Module1
Function GetProcessorUniqueID() As String
'PURPOSE: This function returns the UniqueID of
' the processor. (The serial number of the
' CPU.) It is useful for software activation
' and protection routines that need to uniquely
' identify a processor. Note that not all
' processors return this number, so you may need
' to fallback to a network card MAC number or
' other unique identifier. But this is the best
' thing to use, if the processor provides it.
'
' Quickly hacked together by Brian Hawley
' and hardly tested at all. Could probably use
' a lot of improvement and testing. Any suggestions,
' flaming or praise to VB Q & A on [url]www.vbforums.com[/url]
'
' Note that Win32_Processor/UniqueID is only
' one example of a whole load of stuff available in
' System.management. It's worth exploring.
Dim mc As ManagementClass
Dim moc As ManagementObjectCollection
Dim mo As ManagementObject
Dim UniqueIDs As String()
mc = New ManagementClass("Win32_Processor") 'Create the management class
moc = mc.GetInstances() 'Get all the instances. (May be more than one CPU)
For Each mo In moc 'Check each one
'Increase the size of the array as required.
'(Yes, I know there is a better way, but I wanted to play
'with Try Catch.)
Try
ReDim Preserve UniqueIDs(UBound(UniqueIDs) + 1)
Catch
ReDim UniqueIDs(0)
End Try
'Stick the result in the array
UniqueIDs(UBound(UniqueIDs)) = Convert.ToString(mo.Item("UniqueId"))
mo.Dispose() ' Not sure if I need this, but it can`t hurt.
Next
'Not that UniqueIDsis an array as there may be more than one processor.
'We only return the first element here, which is the first processor,
'but there could be several (if you or your client are rich).
Return UniqueIDs(0)
End Function
End Module