views:

64

answers:

1

I'm retrieving Windows XP license key with this function but it does not work for Vista and Seven. How can I get the license key at both of these Windows versions?

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
+2  A: 

Rather than using the registry I'd suggest that you should use WMI. Specifically the Win32_OperatingSystem class as described here. As can be seen on that page there is a property called SerialNumber.

This page contains a complete sample (with explanations) for how to do it.

ho1
I'm getting error when trying to define ManagementObjectSearcher, Management.ManagementObjectCollection and Management.ManagementObject.
HasanGursoy
@HasanGursoy: I think you probably need to add a reference to `System.Management` (In case you haven't done that before, on the `Project` menu, choose `Add Reference` and then scroll down to the one you need).
ho1
Hmm yes you're right. I though Imports System.Management is enough.
HasanGursoy
@HasanGursoy: That's enough for the common parts of the framework, but a few of them you need to add manually (Management and DirectoryServices are the only ones I can think of right now that I've used that needed that).
ho1