Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET
Hello, i found this script online sometime ago, and i can't convert it entirely so it can work in vb.net too, i'm trying to get the windows serial number from the registry for a "gathering information" app.
 
vbscript:
 const HKEY_LOCAL_MACHINE = &H80000002 
 strKeyPath = "SOFTWARE\Microsoft\Windows NT\CurrentVersion"
 strValueName = "DigitalProductId"
 strComputer = "."
 dim iValues()
 Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _ 
       strComputer & "\root\default:StdRegProv")
 oReg.GetBinaryValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,iValues
 Dim arrDPID
 arrDPID = Array()
 For i = 52 to 66
 ReDim Preserve arrDPID( UBound(arrDPID) + 1 )
 arrDPID( UBound(arrDPID) ) = iValues(i)
 Next
 
 Dim arrChars
 arrChars = Array("B","C","D","F","G","H","J","K","M","P","Q","R","T","V","W","X","Y","2","3","4","6","7","8","9")
 
 For i = 24 To 0 Step -1
 k = 0
 For j = 14 To 0 Step -1
  k = k * 256 Xor arrDPID(j)
  arrDPID(j) = Int(k / 24)
  k = k Mod 24
 Next
 strProductKey = arrChars(k) & strProductKey
 ' <------- add the "-" between the groups of 5 Char -------->
 If i Mod 5 = 0 And i <> 0 Then strProductKey = "-" & strProductKey
 Next
 strFinalKey = strProductKey
 '
 
 strComputer = "."
 Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
 Set colOperatingSystems = objWMIService.ExecQuery _
    ("Select * from Win32_OperatingSystem")
 For Each objOperatingSystem in colOperatingSystems
    strOS   = objOperatingSystem.Caption
    strBuild   = objOperatingSystem.BuildNumber
    strSerial   = objOperatingSystem.SerialNumber
    strRegistered  = objOperatingSystem.RegisteredUser
 Next
 Set wshShell=CreateObject("wscript.shell")
 strPopupMsg = strOS & vbNewLine & vbNewLine
 strPopupMsg = strPopupMsg & "Build Number:  " & strBuild & vbNewLine
 strPopupMsg = strPopupMsg & "PID:  " & strSerial & vbNewLine & vbNewLine
 strPopupMsg = strPopupMsg & "Registered to:  " & strRegistered & vbNewLine & vbNewLine & vbNewLine
 strPopupMsg = strPopupMsg & "Your Windows Product Key is:" & vbNewLine & vbNewLine & strFinalKey
 strPopupTitle = "Microsoft Windows License Information"
 wshShell.Popup strPopupMsg,,strPopupTitle,vbCancelOnly+vbinformation
 WScript.Quit
 
vb.net:
Dim iValues() As Integer
        oReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
              My.Computer.Name & "\root\default:StdRegProv")
        oReg.GetBinaryValue("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\DigitalProductID", False)
        Dim arrDPID() As Int32
        Dim k As Int32 = 0
 
        ReDim arrDPID(50)
        For i = 52 To 66
            ReDim Preserve arrDPID(UBound(arrDPID) + 1)
            arrDPID(UBound(arrDPID)) = iValues(i)
        Next
        Dim arrChars(50) As Char
        Dim strProductKey As String
        Dim strFinalKey As String
        arrChars = {"B", "C", "D", "F", "G", "H", "J", "K", "M", "P", "Q", "R", "T", "V", "W", "X", "Y", "2", "3", "4", "6", "7", "8", "9"}
        For i = 24 To 0 Step -1
            k = 0
            For j = 14 To 0 Step -1
                k = k * 256 Xor arrDPID(j)
                arrDPID(j) = Int(k / 24)
                k = k Mod 24
            Next
            strProductKey = arrChars(k) & strProductKey
            If i Mod 5 = 0 And i <> 0 Then strProductKey = "-" & strProductKey
        Next
        strFinalKey = strProductKey
        '

        Dim objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" _
    & My.Computer.Name & "\root\cimv2")
        Dim colOperatingSystems = objWMIService.ExecQuery _
    ("Select * from Win32_OperatingSystem")
       
        For Each objOperatingSystem In colOperatingSystems
            Dim strOS As String
            Dim strBuild As String
            Dim strSerial As String
            Dim strRegistered As String
            strOS = objOperatingSystem.Caption
            strBuild = objOperatingSystem.BuildNumber
            strSerial = objOperatingSystem.SerialNumber
            strRegistered = objOperatingSystem.RegisteredUser
        Next
 
but i get an runtime error at the 2nd and 3rd line saying that i can't create an activeX component.
any help? thanks
Posted 22-Feb-13 6:06am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

At the start you write:
oReg = Object...
Did you declare oReg correctly and initialized it?
  Permalink  
Comments
Member 9856657 at 22-Feb-13 12:28pm
   
Oh yes, i forgot to post that here, i did declare it in the first place..
Dim oReg As Object at the top. It only gives that error on runtime, no errors on the programming interface.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 278
1 Maciej Los 265
2 Nguyen.H.H.Dang 230
3 Peter Leow 195
4 arvind mepani 187


Advertise | Privacy | Mobile
Web02 | 2.8.140709.1 | Last Updated 22 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid