Read Registry on Windows 7 64bit

Can anyone tell me what is wrong with this method? I’ve been using this to generate a machine ID, and it was working fine for a long time. Now I have a customer with a windows 7 64 machine and gives and error. I don’t have the exact error but something about a sketchup/tools/win32/registry.rb not found.

Does anyone have a windows 7 machine to test this on?

    def computer_system_uuid(testing = false)
      if Sketchup.platform == :platform_osx
        `ioreg -ad2 -c IOPlatformExpertDevice | xmllint --xpath '//key[.="IOPlatformUUID"]/following-sibling::*[1]/text()' -`
      else
        require 'win32/registry'
        key  = 'SYSTEM\HardwareConfig'
        uuid = ''
        config = ''
        hardware = nil
        begin
          hardware = Win32::Registry::HKEY_LOCAL_MACHINE.open(key)
          return '' if hardware.nil?

          config = hardware['LastConfig']
        rescue StandardError => e
          puts e.inspect
        else
          begin
            keys = hardware.keys
            puts keys.inspect if testing
            uuid = config[/[^{}]+/] if keys.include?(config)
          rescue StandardError => e
            puts e.inspect
          else
            uuid
          end
        ensure
          hardware.close unless hardware.nil?
        end
        uuid
      end
    end

The "win32" folder is at this path (the first in the $LOAD_PATH array):
"C:/Program Files/SketchUp/SketchUp 20nn/Tools/RubyStdLib"
(where nn is the major version number.)

However, the standard library was only distributed with SketchUp 2014 and higher.

I do not get any error running require 'win32/registry' at the console, and afterward Win32 is accessible. (if defined?(Win32) would be the simplest test post require to see that the library was loaded.)

OK it seems like to problem is that the registry key doesn’t exist.

Here is the error.

Now I just need to find a way to get a machine ID on windows 7.

I believe that when dealing with the registry, you need to use double backslashes …

key  = 'SYSTEM\\HardwareConfig'

EDIT: Works on my (Win10) machine …

key  = 'SYSTEM\\HardwareConfig'

hardware = Win32::Registry::HKEY_LOCAL_MACHINE.open(key)
#<Win32::Registry:0x0000023c86d0ea20>

hardware.class
#=> Win32::Registry

On my machine it works with a single or double backslash. Are you on windows 7? Does a single backslash fail on your machine?

Win10. My Win 7 machine is packed away in the closet. (And I removed all SU installs from it.)

OK the only machine I was having a problem with is a win 7 machine. I’m trying to get a win 7 VM running, but that isn’t a 5 minute project either.

NOTE: This is a continuation of …

1 Like

Sorry I couldn’t find the original thread (because I didn’t look :face_with_monocle:).

Posted solution here:

1 Like