Click here to Skip to main content
15,509,916 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I am testing a way presented in this link^.

I created a Windows Form application quickly with one button and four labels. two labels are used to display Windows UUID and Machine UUID.

It turns out this call returns an empty string:

Dim readValue = My.Computer.Registry.GetValue("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Cryptography", "MachineGUID", Nothing)

What I have tried:

here is my code snippet in VB.NET for your quick reference:
Imports System.Management
Imports Microsoft.Win32

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Label2.Text = Get_WinGUID()
        Label4.Text = Get_PC_UUID()

    End Sub

    Public Function Get_WinGUID() As String
        Dim sRet As String = ""
            Dim readValue = My.Computer.Registry.GetValue("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Cryptography", "MachineGUID", Nothing)
            sRet = readValue
        Catch ex As Exception
            sRet = ""
        End Try

        Return sRet
    End Function

    Public Function Get_PC_UUID() As String
        Dim UUID As String = ""
            Dim searcher As New ManagementObjectSearcher("root\CIMV2", "SELECT * FROM Win32_ComputerSystemProduct")

            For Each queryObj As ManagementObject In searcher.Get()
                UUID += queryObj("UUID")

        Catch err As ManagementException
        End Try
        Return UUID
    End Function

End Class
Updated 6-Oct-20 0:50am
Richard MacCutchan 5-Oct-20 15:51pm    
Look in your registry to see if that key and value is present.
Southmountain 5-Oct-20 17:15pm    
but I used registry editor to browse this item((MachineGUID)), there is a value there. it should not an empty string.

This is happening because your app is running on 64-bit Windows and you're trying to read a value from the 64-bit registry, but your app is running as a 32-bit app.

Double-click "My Project" in the Solution Explorer and click on the Compile tab. Under "Target CPU", you'll see AnyCPU with a "Prefer 32-bit" checkbox that is checked under it. Uncheck that box and recompile the app and run it.

That checkbox tells the runtime that if running on a 64-bit machine, run the app as a 32-bit app anyway.
Share this answer
Southmountain 5-Oct-20 20:53pm    
you are right about my current settings. I built my app targeting x86 and still get empty string...
Dave Kreskowiak 5-Oct-20 23:24pm    
That's because x86 means "32-bit only" and you can't read the 64-bit registry from a 32-bit app.

The value you're trying to read only exists on the 64-bit side of the registry.
Southmountain 6-Oct-20 13:19pm    
thank you very much! I got it now...
As the previous solutions have already told you, this is because you are running a 32-bit process on a 64-bit OS. You are being bitten by the "registry redirector" - the keys visible in regedit won't match the keys visible to your code.

Registry Redirector - Win32 apps | Microsoft Docs[^]

If you can't change your application to run as a 64-bit application, you'll need to open the key with a specific RegistryView:
using (var hklm = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64))
using (var key = hklm.OpenSubKey(@"SOFTWARE\Microsoft\Cryptography"))
   // key now points to the 64-bit key
c# - OpenSubKey() returns null for a registry key that I can see in regedit.exe - Stack Overflow[^]
Share this answer
Southmountain 6-Oct-20 13:18pm    
thank you very much! now I got it.
As Richard MacCutchan[^] already mentioned, this behavior is not necessarily an error!
Take a look at Registry.GetValue(String, String, Object) Method:

Retrieves the value associated with the specified name, in the specified registry key. If the name is not found in the specified key, returns a default value that you provide, or null if the specified key does not exist.

As you set the default value to "Nothing", you get back an empty string.
Share this answer
Southmountain 5-Oct-20 17:15pm    
but I used registry editor to browse this item, there is a value there. it should not an empty string.

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