locked
how can i create computer unique ID RRS feed

  • Question

  • hi

    i need to create unique serial for any computer , So I used the motherboard serialnumber and cpu serial number

    and By mixing these, my computer's serial produced

          
                Dim searcher0 As New ManagementObjectSearcher("root\CIMV2", "SELECT * FROM Win32_BaseBoard")
                For Each wmi As ManagementObject In searcher0.[Get]()
                    Dim MBBSer As String = wmi.GetPropertyValue("SerialNumber").ToString.Trim()
                    Exit For
                Next
          
      Dim searcher1 As New ManagementObjectSearcher("root\CIMV2", "SELECT * FROM Win32_Processor")
                For Each wmi As ManagementObject In searcher1.[Get]()
                    Dim CPUSer As String = wmi.GetPropertyValue("processorid").ToString.Trim()
                    Exit For
                Next

    But I've noticed that some computers have the same motherboard serial number or same cpu serialnumber

    So I could produce unique hardware ID.

    can i help me:

    How do I generate Serialnumber only for each computer, so that is not reproducible on any other computer?

    thanks


    Wednesday, December 23, 2015 5:12 AM

Answers

  • i need to create unique serial for any computer , So I used the motherboard serialnumber and cpu serial number

    You can create a 'unique' id using this:
    https://msdn.microsoft.com/en-us/library/system.guid.newguid%28v=vs.110%29.aspx

    It's not actually guaranteed unique, but the chance of duplication is extremely small.

    Wednesday, December 23, 2015 5:37 AM
  • Every time  Guid.NewGuid()   runs, produces a different output

    So ,It can not be used as a Computer specific serialnumber

    Serial computer must have two characteristics:

    1- Be unique and not be repeated for another computer

    2- Serial production will have on the computer each time you run, the serial has not changed

    Did you really think about what you wrote or did you want to disqualificate yourself as programmer.

    Of course an unique number is always created when it is new. But it can be used endless times. 


    Success
    Cor

    Wednesday, December 23, 2015 7:22 AM
  • Ashkan,

    In addition to what Acamar suggested, you can also use the MAC address of the computer. Obviously if certain hardware components get replaced then it could change, but that said the following shows a way to do this using System.Management:

    Option Strict On Option Explicit On Option Infer Off Imports System.Management Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles MyBase.Load Dim myMAC As String = GetMACAddress() Stop End Sub Private Function GetMACAddress() As String Dim retVal As String = Nothing Try Using mc As New ManagementClass("Win32_NetworkAdapterConfiguration") Using moc As ManagementObjectCollection = mc.GetInstances() Const MACAddress As String = "" For Each mo As ManagementObject In moc If String.IsNullOrWhiteSpace(MACAddress) Then ' Only return MAC Address from first card: If CBool(mo("IPEnabled")) Then retVal = mo("MacAddress").ToString().Replace(":", "") mo.Dispose() Exit For End If End If mo.Dispose() Next End Using End Using Catch ex As Exception retVal = String.Empty End Try Return retVal End Function End Class



    Note that you'll need to add a reference to System.Management or it won't compile.



    If I had eight hours to chop down a tree, I'd spend six sharpening my axe. -- Abraham Lincoln




    Wednesday, December 23, 2015 4:01 PM

All replies

  • i need to create unique serial for any computer , So I used the motherboard serialnumber and cpu serial number

    You can create a 'unique' id using this:
    https://msdn.microsoft.com/en-us/library/system.guid.newguid%28v=vs.110%29.aspx

    It's not actually guaranteed unique, but the chance of duplication is extremely small.

    Wednesday, December 23, 2015 5:37 AM
  • Every time  Guid.NewGuid()   runs, produces a different output

    So ,It can not be used as a Computer specific serialnumber

    Serial computer must have two characteristics:

    1- Be unique and not be repeated for another computer

    2- Serial production will have on the computer each time you run, the serial has not changed

    Wednesday, December 23, 2015 6:56 AM
  • Every time  Guid.NewGuid()   runs, produces a different output

    So ,It can not be used as a Computer specific serialnumber

    Serial computer must have two characteristics:

    1- Be unique and not be repeated for another computer

    2- Serial production will have on the computer each time you run, the serial has not changed

    Did you really think about what you wrote or did you want to disqualificate yourself as programmer.

    Of course an unique number is always created when it is new. But it can be used endless times. 


    Success
    Cor

    Wednesday, December 23, 2015 7:22 AM
  • Serial computer must have two characteristics:

    1- Be unique and not be repeated for another computer

    2- Serial production will have on the computer each time you run, the serial has not changed

    Your question was about a unique serial number.  An ID that uniquely identifies a specific computer is a different task. 

    It depends on how you want to define the 'computer'.  You can use hardware details, but these can change.  See here for a list of what MS uses:
    https://en.wikipedia.org/wiki/Microsoft_Product_Activation

    A computer can also be identified by the software it is using, such as the OS product key, but of course the same problem applies - the software can be upgraded.   It really comes down to what it is that you are trying to identify.

    • Edited by Acamar Wednesday, December 23, 2015 7:27 PM sp
    Wednesday, December 23, 2015 7:24 AM
  • Ashkan,

    In addition to what Acamar suggested, you can also use the MAC address of the computer. Obviously if certain hardware components get replaced then it could change, but that said the following shows a way to do this using System.Management:

    Option Strict On Option Explicit On Option Infer Off Imports System.Management Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles MyBase.Load Dim myMAC As String = GetMACAddress() Stop End Sub Private Function GetMACAddress() As String Dim retVal As String = Nothing Try Using mc As New ManagementClass("Win32_NetworkAdapterConfiguration") Using moc As ManagementObjectCollection = mc.GetInstances() Const MACAddress As String = "" For Each mo As ManagementObject In moc If String.IsNullOrWhiteSpace(MACAddress) Then ' Only return MAC Address from first card: If CBool(mo("IPEnabled")) Then retVal = mo("MacAddress").ToString().Replace(":", "") mo.Dispose() Exit For End If End If mo.Dispose() Next End Using End Using Catch ex As Exception retVal = String.Empty End Try Return retVal End Function End Class



    Note that you'll need to add a reference to System.Management or it won't compile.



    If I had eight hours to chop down a tree, I'd spend six sharpening my axe. -- Abraham Lincoln




    Wednesday, December 23, 2015 4:01 PM
  • Ashkan,

    I simplified the code and put things into Using blocks where it made sense to, but in the process I see now that I've eliminated the need for the string variable "MACaddress". As I currently have it, it may as well be "If 1 = 1 Then...".

    Please modify that and eliminate it all together. Sorry about that...


    If I had eight hours to chop down a tree, I'd spend six sharpening my axe. -- Abraham Lincoln

    Wednesday, December 23, 2015 5:39 PM
  • If you use the hardware (MAC address, Hard Drive Serial Numbers, etc) - what do you do when a user gets a new hard drive, or Network Card, or Video Card ?????

    Thursday, December 24, 2015 5:37 AM
  • I think I did not express my idea as well

    I decided to build a unique Serial for every computer ,because I want use it to register my application .

    So for me it is important that:

    1- Serial Be unique and not be repeated for another computer

    2- Serial production will have on the computer each time you run, the serial has not changed

    Thursday, December 24, 2015 6:59 AM
  • Use MAC address is a good idea to produce a unique Serialnumber

    But if the computer does not have network card, what to do?

    Thursday, December 24, 2015 7:02 AM
  • I think I did not express my idea as well

    I decided to build a unique Serial for every computer ,because I want use it to register my application .

    So for me it is important that:

    1- Serial Be unique and not be repeated for another computer

    2- Serial production will have on the computer each time you run, the serial has not changed

    Then you can use the Guid likewise Acamar has shown and create a program to store and read that from the registry.

    Be aware a real standard is not available, some tried it with giving a processor an unique ID. But like you can see what NSA with the Mac address (and probably others too) for spying you understand why that was rejected.  

    To store it in the registry you can use this code on our website (although you can use it forever and not for 15 days like this sample does)

    http://www.vb-tips.com/RegistryExpirirantionKey.aspx


    Success
    Cor


    • Edited by Cor Ligthert Thursday, December 24, 2015 2:19 PM
    Thursday, December 24, 2015 7:43 AM
  • Almost all computer motherboards nowadays have at a least one integrated NIC. But you can also query the machine for a UUID. Devon posted an example in another thread:

    https://social.msdn.microsoft.com/Forums/en-US/15298114-f297-4ebc-8e36-9a7d292c759d/how-to-get-uuid-using-vbnet?forum=vbgeneral

    There probably isn't going to be a perfect solution, so you may want to use multiple methods.


    Paul ~~~~ Microsoft MVP (Visual Basic)

    Thursday, December 24, 2015 1:15 PM
  • Use MAC address is a good idea to produce a unique Serialnumber

    But if the computer does not have network card, what to do?

    You have many good ideas here. It seems to me that you're making perfect the enemy of good.

    I'd suggest that you choose one and go with it.


    If I had eight hours to chop down a tree, I'd spend six sharpening my axe. -- Abraham Lincoln

    Thursday, December 24, 2015 1:33 PM
  • The first thing you need is a clear definition of what a unique hardware ID is.  In the example below I have defined that ID as a string in the registry that is created and written once.  When created it is composed of the two items the OP specified in the original post and a GUID.  The questions I would be asking about the code are:

    1. what happens if the OS disk is moved to another machine
    2. what happens if the OS is updated / restored
    3. what happens if the registry is restoed

    Before using the code decide on the keyvalue pair for the registry, and backup the registry.

    Imports System.Management, Microsoft.Win32
    
    Public Class Form1
    
        Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
            Dim PCID As String = CheckRegistryForPCID()
            If PCID = "" Then
                PCID = CreatePCID()
            End If
        End Sub
    
        Private KeyNameForPCID As String = "HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION" 'example
        Private KeyValueNameForPCID As String = "PCID" 'example
    
        ''' <summary>
        ''' check registry for existing value
        ''' </summary>
        ''' <returns>ID if set or "" for error</returns>
        ''' <remarks>error is no key or keyvalue not set</remarks>
        Private Function CheckRegistryForPCID() As String
            Dim PCID As String = ""
            Try
                PCID = Registry.GetValue(KeyNameForPCID, KeyValueNameForPCID, "").ToString
            Catch secex As Security.SecurityException
                Stop
            End Try
            Return PCID
        End Function
    
        Private Function CreatePCID() As String
    
            Const sepCH As Char = "∞"c
            '
            Dim PCID As String = ""
    
            Dim searcher0 As New ManagementObjectSearcher("root\CIMV2", "SELECT * FROM Win32_BaseBoard")
            For Each wmi As ManagementObject In searcher0.[Get]()
                PCID = wmi.GetPropertyValue("SerialNumber").ToString.Trim()
                Exit For
            Next
            searcher0.Dispose()
    
            PCID &= sepCH
    
            Dim searcher1 As New ManagementObjectSearcher("root\CIMV2", "SELECT * FROM Win32_Processor")
            For Each wmi As ManagementObject In searcher1.[Get]()
                PCID &= wmi.GetPropertyValue("processorid").ToString.Trim()
                Exit For
            Next
            searcher1.Dispose()
    
            PCID &= sepCH
    
            Dim gid As Guid = Guid.NewGuid
            'convert guid to string
    
            PCID &= gid.ToString
    
            Try
                'make sure you know what you are doing before
                'un-commenting the next line (backup the registry)
                ' Registry.SetValue(KeyNameForPCID, KeyValueNameForPCID, PCID)
            Catch secex As Security.SecurityException
                Stop
            Catch ex As Exception
                Stop
            End Try
            Return PCID
        End Function
    End Class


    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it." - MSDN User JohnWein    Multics - An OS ahead of its time.

    Thursday, December 24, 2015 2:51 PM