Reading Windows 10 product ID with code.
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Reading Windows 10 product ID with code.

  1. #1
    Join Date
    Sep 2015
    Location
    South East England, UK
    Posts
    3

    Post Reading Windows 10 product ID with code.

    I am trying to read the product ID with Windows 10.

    With Windows 95 to windows 7, I could read the OEM number from the registry.
    Using RegEdit.exe, I can read the value and it is still at the old Windows 7 OEM number location.

    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProductID
    HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProductID

    The same code that worked with Windows 7 now returns an error value of 2.

    I can create a WMIC file to read the product ID and save it to a file,
    but this will display the WMIC file for a fraction of a second.

    Dose anybody know of a good way to get the product ID?

    Or code to read the registry at this location.

    I am using Visual Basic 5.

    Code:
        regErrorValue = fReadValue("HKLM", "SOFTWARE\Microsoft\Windows NT\CurrentVersion", "ProductID", "S", "<Unknown ProductID>", sSetting)
        OEM_number = sSetting
        If OEM_number = "<Unknown ProductID>" Then
          OEM_number = OEM_number_from_WMIC()
        End If
    
    '''
    
    
    
    Private Function OEM_number_from_WMIC(Optional ByVal errorValue As String = "") As String
      ' Get the OEM / Product ID / OS Serial number.
      ' Note: This code will not work with Windows 7 or before.
      '       This code works with Windows 10.
      '       Not tested with Windows 8.0 or Windows 8.1
      Const FileBytesOver = 10
      Const FileOverXHours = 2
      Dim InfoFile As String
      Dim WMIC_bat_File As String
      Dim fileHandle As Integer
      Dim fileData As String
      OEM_number_from_WMIC = errorValue
      On Error GoTo WMIC_Error
      InfoFile = "OEM_Information.txt"
      WMIC_bat_File = App.path + "\_Read_OEM_value.bat"
      ' Step 1: Check if the 'InfoFile' file exists and is not too old.
      ' If GetFileSize(InfoFile) < 0 Then ' <0 = not found
      ' If GetFileSize(InfoFile) = 0 Then ' =0 = found but empty
      If (frm_MainWindow.GetFileSize(InfoFile) < FileBytesOver) Or (DateDiff("H", frm_MainWindow.GetFileDate(InfoFile), Now) > FileOverXHours) Then
        ' File not found or under 10 bytes or over 5 hours old.
        ' Step 2: Create the WMIC_bat_File.
        fileHandle = FreeFile
        Open WMIC_bat_File For Output As #fileHandle
        Print #fileHandle, "@REM Add @ to start of line to hide command line."
        Print #fileHandle, "@REM Batch file " + WMIC_bat_File
        Print #fileHandle, "@REM reads and saves the computer product id to file " + InfoFile
        Print #fileHandle, "@REM This file was created on; " + Format$(Now, "d/mm/yyyy - h:nn:ssampm")
        Print #fileHandle, ""
        Print #fileHandle, "@REM Code by Mark A. Agius, (c)2015"
        Print #fileHandle, ""
        Print #fileHandle, "@CLS"
        Print #fileHandle, "@WMIC /OUTPUT:" + InfoFile + " OS GET SERIALNUMBER"
        ' Print #fileHandle, "@ECHO File created..."
        ' Print #fileHandle, "@pause"
        Close #fileHandle
        fileHandle = 0
        ' Step 3: Run the WMIC_bat_File. (This will create the InfoFile file)
        Call mod_FileOpen.Load_File_or_URL(WMIC_bat_File, "", 0)
      End If
      ' Step 4: Read the InfoFile file and return the last line of text.
      fileHandle = FreeFile
      Open App.path + "\" + InfoFile For Input As #fileHandle
      While Not EOF(fileHandle)
        'Line Input #fileHandle, fileData ' Line input keeps spaces at end of line.
        Input #fileHandle, fileData
        fileData = Trim$(fileData)
        If fileData <> "" Then
          ' Computer product id is 23 characters long with Windows 10.
          If Len(fileData) >= 20 And Len(fileData) <= 45 Then
            OEM_number_from_WMIC = fileData
          End If
        End If
      Wend
      Close #fileHandle
      fileHandle = 0
      Exit Function
    WMIC_Error:
      ' Error code.
      ' OEM_number_from_WMIC = "<" + Error + " (" & Err & ")>"
      
      Exit Function
    End Function

  2. #2
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,066

    Re: Reading Windows 10 product ID with code.

    I haven't tried this under Windows 10 but this VBS script works on XP and 7
    Code:
    Set WshShell = CreateObject("WScript.Shell")
    MsgBox ConvertToKey(WshShell.RegRead("HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\DigitalProductId"))
     
    Function ConvertToKey(Key)
        Const KeyOffset = 52
        i = 28
        Chars = "BCDFGHJKMPQRTVWXY2346789"
        Do
            Cur = 0
            x = 14
            Do
                Cur = Cur * 256
                Cur = Key(x + KeyOffset) + Cur
                Key(x + KeyOffset) = (Cur \ 24) And 255
                Cur = Cur Mod 24
                x = x -1
            Loop While x >= 0
            i = i -1
            KeyOutput = Mid(Chars, Cur + 1, 1) & KeyOutput
            If (((29 - i) Mod 6) = 0) And (i <> -1) Then
                i = i -1
                KeyOutput = "-" & KeyOutput
            End If
        Loop While i >= 0
        ConvertToKey = KeyOutput
    End Function
    Always use [code][/code] tags when posting code.

  3. #3
    Join Date
    Sep 2015
    Location
    South East England, UK
    Posts
    3

    Re: Reading Windows 10 product ID with code.

    Thank you for your code. It works with Windows 10 but..
    I am trying to get the Computer Product ID (Known as the OEM number with Windows 95 - Windows 7)
    Your code returns the digital product ID.

    Digital product ID format = XXXXX-XXXXX-XXXXX-XXXXX-XXXXX (Don't want this)
    OEM / Product ID format = 00000-OEM-0000000-00000 (Windows 7)
    OEM / Product ID format = 00000-00000-00000-XXXXX (Windows 10)


    The following VBS script will get this and works with Windows 10.
    Code:
    Set WshShell = CreateObject("WScript.Shell")
    MsgBox WshShell.RegRead("HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProductID")
    I can read the registry at; HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\DigitalProductId
    but not at; HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProductID

    So I need the Visual Basic 5 code for
    Set WshShell = CreateObject("WScript.Shell")
    Var$ = WshShell.RegRead(...)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


HTML5 Development Center