Click here to Skip to main content
15,743,179 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:

I have a problem. I think anyone among you can help me. My Problem is that I have a function which reads a value from registry and returns a string value. But this function works well in 32 -Bit OS and in 64-Bit OS it has a null reference. How to get red of this problem ?
My Function is :
Public Shared Function GetOSProductKey() As String
        If GetOSIs64Bit() = False Then
                Dim regKey As RegistryKey = _
                    Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows NT\CurrentVersion", False)
                Dim digitalPid() As Byte = CType(regKey.GetValue("DigitalProductID"), Byte())
                If digitalPid IsNot Nothing Then
                    Dim key(14) As Byte '0-14 = 15 bytes
                    Array.Copy(digitalPid, 52, key, 0, 15)
                    Dim characters As String = "BCDFGHJKMPQRTVWXY2346789"
                    Dim productKey As String = ""
                    For j As Integer = 0 To 24
                        Dim curValue As Short = 0
                        For i As Integer = 14 To 0 Step -1
                            curValue = CShort(curValue * 256 Xor key(i))
                            key(i) = CByte(Int(curValue / 24))
                            curValue = CShort(curValue Mod 24)
                        productKey = characters.Substring(curValue, 1) & productKey
                    For i As Integer = 4 To 1 Step -1
                        productKey = productKey.Insert(i * 5, "-")

                    Return productKey
                    Return mUnknown
                End If
            Catch ex As Exception
                Return mUnknown
            End Try
End Function
Updated 29-Oct-13 9:04am
Member 741145 14-May-14 9:58am    
Is there a way, once the product key has been returned, to use code to revert it back?

On 64-bit windows you can use the RegistryKey.OpenBaseKey[^] with the RegistryView[^] as Registry64. This allows you to read the 64-bit registry.

Just a side-note, you can use this method and pass Registry64 whether you are on a 32-bit system or a 64-bit system and you will get the proper result. The key text is this in the second link I posted:

Quote: MSDN Article
If you request a 64-bit view on a 32-bit operating system, the returned keys will be in the 32-bit view.
Share this answer
Sergey Alexandrovich Kryukov 28-Oct-13 23:03pm    
5ed. I recently answered to something similar, please see Solution 2 with some more detail.
Joezer BH 29-Oct-13 2:21am    
ledtech3 29-Oct-13 12:04pm    
I see the RegistryView Start working with .Net 4.0 and not available in .net 3.5
Good to know about RegistryView it can save some code then.
Dinesh Kumar Dora 6-Feb-19 8:53am    
I need the solution for dotnet 2.0 framework + someone help here please.
This is because part of the registry has separate 64-bit and 32-bit parts, with 32-bit part used by the 32-bit application executed under WoW64. This is called "views".
Please see my answer: Getting issue to read registry value . It always returning "null"[^].

You can also use the node "HKLM\Software\Wow6432Node" from a 64-bit application:
Getting issue to read registry value . It always returning "null"[^].

Share this answer
Ron Beyer 28-Oct-13 23:14pm    
5'd too, it can be very confusing on how to access the registry and that there are two running side-by-side...
Sergey Alexandrovich Kryukov 29-Oct-13 0:23am    
Thank you, Ron. "Confusing" is the immanent part of this topic. :-)
Friends use this Format to Read 64-Bit Registry values :
Dim rk1 As RegistryKey
Dim rk2 As RegistryKey
rk1 = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64)
rk2 = rk1.OpenSubKey("HARDWARE\DESCRIPTION\System\BIOS")
Dim PID As String = rk2.GetValue("SystemProductName").ToString
Share this answer
[no name] 10-Dec-15 12:36pm    
thanks a ton.. i was about to give up in the next 5 minutes after struggling for more than 24 hours.
Thank you .. solution was a lifesaver :)
Share this answer
Deepu S Nair 14-May-15 1:54am    
Please don't post your comment as solution.
CHill60 14-May-15 3:42am    
Further to the comment from Deepu S Nair, you should use the "Have a Question or comment?" link next to the relevant post. As it is we have no idea which of the 3 surviving solutions was a lifesaver! Use the star rating system to upvote solutions which work (Note that a star rating of 1 or 2 is a down-vote = not a good solution, 4 or 5 is an up-vote = a good solution)

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

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900