C# get Windows CD Key

Go To StackoverFlow.com

2

I've been using the below code in order to get the Windows License Key. It worked pretty well a long time. But now I discovered that it works on Windows XP (x86) but not on Windows 7 x64.

Reason: The DigitalProductID regisitry value contains only zeroes within the range we are looking for on the 64 bit operating system. Therefore the result it BBBBB-BBBBB-BBBBB-BBBBB-BBBBB. Why is it so and how can I fix this?

    public static string LicenseCDKey
    {
        get
        {
            try
            {
                byte[] rpk = (byte[])Registry.LocalMachine
                   .OpenSubKey(@"Software\Microsoft\Windows NT\CurrentVersion")
                   .GetValue("DigitalProductId");
                string serial = "";
                const string possible = "BCDFGHJKMPQRTVWXY2346789";
                for (int i = 0; i < 25; i++)
                {
                    int accu = 0;
                    for (int a = 0; a < 15; a++)
                    {
                        accu <<= 8;
                        accu += rpk[66 - a];
                        rpk[66 - a] = (byte)(accu / 24 & 0xff);
                        accu %= 24;
                    }
                    serial = possible[accu] + serial;
                    if (i % 5 == 4 && i < 24)
                    {
                        serial = "-" + serial;
                    }
                }
                return serial;
            }
            catch
            {
                return ErrorString;
            }
        }
    }
2012-04-04 18:41
by bytecode77
Did you change the output to x64 instead of Any CPU for the platform target - Bryan Crosby 2012-04-04 18:56
Nope, as I replied to both answers below I'm using AnyCPU - bytecode77 2012-04-04 19:43


4

As user287107 pointed out x86 applications (32 bit) running on a x64 operating system are using a different registry (registry view).

In order to access the x64 registry you have a few options:

  1. Change your platform target to x64 (Visual Studio project settings).
  2. If you are using .Net Framework 4.0 you could use the RegistryKey class and RegistryView enum to access the x64 registry.

    RegistryKey key = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine,
                                              RegistryView.Registry64);
    
    string keyPath = @"Software\Microsoft\Windows NT\CurrentVersion";
    byte[] rpk = (byte[])key.OpenSubKey(keyPath).GetValue("DigitalProductId");
    
  3. If you are not using the .Net Framework 4.0 and you do not want to set your platform target to x64 you have to use Interop (RegOpenKeyEx() Win32 API function with the KEY_WOW64_32KEY flag) to access the x64 registry.

BEGIN EDIT

I've just found an interesting post explaining why the DigitialProductId key could be null/empty:

  1. You are using an Volume License Key to activate your Windows 7 operating system. The VLC key is deleted from the registry after activation.
  2. Someone deleted the registry key (modified the content of this key) manually using the command slmgr –cpky

END EDIT

2012-04-04 19:10
by Hans
And, as posted in user287107's answer, I do use AnyCPU, but it is still not working. Try the code yourself and see - bytecode77 2012-04-04 19:23
@DevilsChild: Is the client computer activated using an Volume License Key - Hans 2012-04-04 19:24
@DevilsChild: On my x64 bit Windows 7 machine, the code above returns the correct license key - Hans 2012-04-04 19:26
@DevilsChild: I've updated my answer - Hans 2012-04-04 19:36
I'm not using a volume license key. This is a screenshot (partially censored) from my DigitalProductId http://xload.dev-ch.com/95a4322173acc41f/screen396.jp - bytecode77 2012-04-04 19:42
@DevilsChild: If you do not use a volume license key then I think the DigitalProductId was deleted with the slmgr -cpky command. Note, the registry key still exists but the DigitalProductId is modified. As a result you get BBBBB-BBBBB-BBBBB-BBBBB-BBBBB - Hans 2012-04-04 20:24
Ok, so it's not an error in my code, but on the user's machine. Thanks for the help - bytecode77 2012-04-04 20:28


1

32 bit applications use a different registry path

a 32 bit application accesses the registry path in HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion where it does not find the product key.

changing the processor type to x64 worked for me to get the real key.

2012-04-04 18:58
by user287107
Use AnyCPU, not x64 - Hans Passant 2012-04-04 19:01
I use AnyCPU indeed, still with no effort. With x86 i would not get any key at all - bytecode77 2012-04-04 19:10
Ads