[C#] - Compatible Windows 8 Product Key Decoder

Discussion in 'Mixed Languages' started by Josh Cell, Aug 8, 2012.

  1. Calistoga

    Calistoga MDL Senior Member

    Jul 25, 2009
    421
    199
    10
    #21 Calistoga, Aug 13, 2012
    Last edited by a moderator: Apr 20, 2017
    @FreeStyler, you can do all of it in one line like this:
    Code:
    RegistryKey registry = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, Environment.Is64BitOperatingSystem ? RegistryView.Registry64 : RegistryView.Registry32).OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion", false);
    
    Formatted (since nobody likes long lines):
    Code:
    RegistryKey registry = RegistryKey.OpenBaseKey(
        RegistryHive.LocalMachine, Environment.Is64BitOperatingSystem 
            ? RegistryView.Registry64 
            : RegistryView.Registry32
    ).OpenSubKey(
        @"SOFTWARE\Microsoft\Windows NT\CurrentVersion", false
    );
    
    Not that it really matters, but now we avoid writing @"SOFTWARE\Microsoft\Windows NT\CurrentVersion" twice! ;)

    Disclaimer: Code has not been tested.
     
  2. CODYQX4

    CODYQX4 MDL Developer

    Sep 4, 2009
    4,813
    45,775
    150
    #22 CODYQX4, Aug 14, 2012
    Last edited: Apr 12, 2019
    .
     
  3. Alphawaves

    Alphawaves Super Moderator/Developer
    Staff Member

    Aug 11, 2008
    6,222
    22,279
    210
    #23 Alphawaves, Aug 15, 2012
    Last edited by a moderator: Apr 20, 2017
    Deleted for being drunk and also remembering x86 was not needed in x64, as Josh posted.. :)
     
  4. FreeStyler

    FreeStyler MDL Guru

    Jun 23, 2007
    3,557
    3,832
    120
  5. user_hidden

    user_hidden MDL Expert

    Dec 18, 2007
    1,034
    1,061
    60
  6. Muerto

    Muerto MDL Debugger

    Mar 7, 2012
    1,855
    2,102
    60
    #26 Muerto, Aug 19, 2012
    Last edited: Aug 22, 2021
    ...
     
  7. Josh Cell

    Josh Cell MDL Developer

    Jan 8, 2011
    3,515
    7,170
    120
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  8. DrCoolZic

    DrCoolZic MDL Member

    Jan 15, 2011
    247
    326
    10
    #28 DrCoolZic, Mar 10, 2013
    Last edited by a moderator: Apr 20, 2017
    First thanks for this very interesting post.
    I am working on using this code in an application but I have a strange problem:
    In the above snippet I am always getting null returned for digitalProductId ???
    I can access most other values in the same key but impossible to read digitalProductId & digitalProductId4

    I have been thinking about access right problem and therefore I have run Visual studio in administrator mode but in did not help? I can read the same digitalProductId from Office keys but I cant from Windows key ???

    I am running on a windows 8 system. Any idea about what I am doing wrong?
    I am stuck and running out of ideas please help - thanks in advance
     
  9. DrCoolZic

    DrCoolZic MDL Member

    Jan 15, 2011
    247
    326
    10
    #29 DrCoolZic, Mar 11, 2013
    Last edited by a moderator: Apr 20, 2017
    OK I foun d the problem ;)

    For reference I put the solution here.
    What is happening is that I am using a 32 bits program on a 64 bit machine!
    64-bit Windows mirrors 32-bit content in a way that is transparent to applications. 32-bit applications that use the registry access a mirrored hive inside HKLM\WOW6423Node, which is why I wasn't seeing the expected result when querying a key inside HKLM. Fortunately, .NET 4.0 includes an easy way that lets a 32-bit application see and use the 64-bit registry (and vice-versa).

    I just had to change the code as following:
    Code:
    RegistryKey RegistryBase = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
    RegistryKey registry = RegistryBase.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\", false);