VB.NET - Get product key for Windows 7 and 8

Hi,

For a software reinstallation program i'm making I would like to collect the current product key from Windows 7 and 8 so it can be automatically be re-used by my application.

Now I have found multiple examples but none of them seem to work...

I have tried below code but this always results in "N/A", the variable Hexbuf is empty but I don't understand why.

Public Function GetProductKey(ByVal KeyPath As String, ByVal ValueName As String) As String

        Dim HexBuf As Object = My.Computer.Registry.GetValue(KeyPath, ValueName, 0)

        If HexBuf Is Nothing Then Return "N/A"

        Dim tmp As String = ""

        For l As Integer = LBound(HexBuf) To UBound(HexBuf)
            tmp = tmp & " " & Hex(HexBuf(l))
        Next

        Dim StartOffset As Integer = 52
        Dim EndOffset As Integer = 67
        Dim Digits(24) As String

        Digits(0) = "B" : Digits(1) = "C" : Digits(2) = "D" : Digits(3) = "F"
        Digits(4) = "G" : Digits(5) = "H" : Digits(6) = "J" : Digits(7) = "K"
        Digits(8) = "M" : Digits(9) = "P" : Digits(10) = "Q" : Digits(11) = "R"
        Digits(12) = "T" : Digits(13) = "V" : Digits(14) = "W" : Digits(15) = "X"
        Digits(16) = "Y" : Digits(17) = "2" : Digits(18) = "3" : Digits(19) = "4"
        Digits(20) = "6" : Digits(21) = "7" : Digits(22) = "8" : Digits(23) = "9"

        Dim dLen As Integer = 29
        Dim sLen As Integer = 15
        Dim HexDigitalPID(15) As String
        Dim Des(30) As String

        Dim tmp2 As String = ""

        For i = StartOffset To EndOffset
            HexDigitalPID(i - StartOffset) = HexBuf(i)
            tmp2 = tmp2 & " " & Hex(HexDigitalPID(i - StartOffset))
        Next

        Dim KEYSTRING As String = ""

        For i As Integer = dLen - 1 To 0 Step -1
            If ((i + 1) Mod 6) = 0 Then
                Des(i) = "-"
                KEYSTRING = KEYSTRING & "-"
            Else
                Dim HN As Integer = 0
                For N As Integer = (sLen - 1) To 0 Step -1
                    Dim Value As Integer = ((HN * 2 ^ 8) Or HexDigitalPID(N))
                    HexDigitalPID(N) = Value \ 24
                    HN = (Value Mod 24)

                Next

                Des(i) = Digits(HN)
                KEYSTRING = KEYSTRING & Digits(HN)
            End If
        Next

        Return StrReverse(KEYSTRING)

    End Function

Open in new window



I also tried to use below code which is actually for Windows XP, this gives met the result "bbbbb-bbbbb-bbbbb-bbbbb-bbbbb"

Public Function sGetXPKey() As String
    Dim result As String = String.Empty

    Dim RegKey As RegistryKey = _
    Registry.LocalMachine.OpenSubKey("Software\Microsoft\Windows NT\CurrentVersion", False)
    Dim bytDPID() As Byte = RegKey.GetValue("DigitalProductID")
    Dim bytKey(14) As Byte
    Array.Copy(bytDPID, 52, bytKey, 0, 15)
    Dim strChar As String = "BCDFGHJKMPQRTVWXY2346789"
    Dim strKey As String = ""

    For j As Integer = 0 To 24
        Dim nCur As Short = 0
        For i As Integer = 14 To 0 Step -1
            nCur = CShort(nCur * 256 Xor bytKey(i))
            bytKey(i) = CByte(Int(nCur / 24))
            nCur = CShort(nCur Mod 24)
        Next
        strKey = strChar.Substring(nCur, 1) & strKey
    Next

    For i As Integer = 4 To 1 Step -1
        strKey = strKey.Insert(i * 5, "-")
    Next

    Return strKey
End Function

Open in new window


Can someboday please help me? i'm a database application programmer so this is new to me.

Thanks
juriangAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

KimputerCommented:
Do you really need the code or is an external program also possible (why invent the wheel again?). By that, I mean using Magic Jelly Bean or Produkey (both are free).
http://www.magicaljellybean.com/keyfinder/
http://www.nirsoft.net/utils/product_cd_key_viewer.html
0
juriangAuthor Commented:
Thanks for your suggestions.

I really need the code. I want to save it to a local database.

Your help is appreciated
Thanks
0
KimputerCommented:
Check this link http://www.intelliadmin.com/index.php/2011/04/get-product-keys-from-computers-across-your-network/
Produkey can do remote, and command line output, so you can grab the results there. Just in case you still can't get the vb code to work.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

juriangAuthor Commented:
@Kimputer: Thanks, this could work though i'm in favor of having the code myself instead of depending on a 3rt party tool.

Who can help me to get above code to work or has another working example?

Thanks
0
KimputerCommented:
Okay, first code works on Windows XP btw.
Use this to test it:
TextBox1.Text =  GetProductKey("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\", "DigitalProductId")

Open in new window

0
KimputerCommented:
Actually, just tested the second code, it also works??? (again on XP)
0
KimputerCommented:
Just tested on Windows 8, wrong code. Meaning your code is not correct for Win7/8, only for Windows XP.
0
juriangAuthor Commented:
Like the topic title mention I need this functionality for Windows 7 and 8.

I did the same testing as you recommended already
TextBox1.Text =  GetProductKey("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\", "DigitalProductId")

Open in new window


Also checked on Windows 7 and 8 if the same registry key exists and it does so I can't think of any reason why the script is not working on Windows 7 and 8.

Please help
0
juriangAuthor Commented:
Anyone who could please help me in providing the code for Windows 7 and 8?

Your help would be very much appreciated.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.