• Login
sketchucation logo sketchucation
  • Login
⚠️ Libfredo 15.4b | Minor release with bugfixes and improvements Update

Get registry string from ruby 2.0

Scheduled Pinned Locked Moved Developers' Forum
8 Posts 2 Posters 872 Views 2 Watching
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    Al Hart
    last edited by 1 May 2014, 19:22

    There are a lot of threads about this, but I am getting confused.

    I took a look at Win32Api.rb in SketchUp 2014, and it says:

    "Win32API is deprecated after Ruby 1.9.1; use dl directly instead"

    Do I took a look at DL, and it says:

    "DL is deprecated, please use Fiddle"

    So I took a look at Fiddle, which does have some Win32 stuff, but I couldn't find an example on how to get a string from the registry using Fiddle.

    Can someone post, or point me to a Ruby 2.0 example which will run in SketchUp and read a Windows Registry String?

    Al Hart

    http:wiki.renderplus.comimageseefRender_plus_colored30x30%29.PNG
    IRender nXt from Render Plus

    1 Reply Last reply Reply Quote 0
    • D Offline
      Dan Rathbun
      last edited by 1 May 2014, 20:27

      The standard library has a Registry class BUT it is currently bugged with regard to string encodings.

      If you wish to be backward compatible you can still use the Win32API class, which in Ruby 1.9.1+, becomes a wrapper to the DL class (which is more C-like,) which in Ruby 2.x, becomes a wrapper to Fiddle.

      There should be examples in the CHM or embedded in the ruby scripts that load them (the CHM help docs are generated from inline documentation.)

      I'm not here much anymore.

      1 Reply Last reply Reply Quote 0
      • D Offline
        Dan Rathbun
        last edited by 1 May 2014, 21:04

        If you wish to do this from a C/C++ extension, you can:

        include "Winreg.h" (include "Windows.h"),
        load the "Advapi32.dll" library,
        and use the registry functions.

        The pages on individual functions have examples.

        http://msdn.microsoft.com/en-us/library/ms724875(v=VS.85).aspx

        These are the functions you'd call using DL, Win32API or Fiddle if you go that route.

        Add: These ways are more complicated, as you must first open a handle to the registry key before a calling read, and be sure to call close on the key when done (like in an ensure clause! )

        I'm not here much anymore.

        1 Reply Last reply Reply Quote 0
        • A Offline
          Al Hart
          last edited by 2 May 2014, 12:56

          Thanks Dan, this looks great.

          Let me try it out

          Al Hart

          http:wiki.renderplus.comimageseefRender_plus_colored30x30%29.PNG
          IRender nXt from Render Plus

          1 Reply Last reply Reply Quote 0
          • A Offline
            Al Hart
            last edited by 2 May 2014, 13:46

            Thanks Dan - that works!

            Is there anything similar for Ruby 1.8, or do I need to continue to use Win32API.so to read the registry?

            Al Hart

            http:wiki.renderplus.comimageseefRender_plus_colored30x30%29.PNG
            IRender nXt from Render Plus

            1 Reply Last reply Reply Quote 0
            • D Offline
              Dan Rathbun
              last edited by 2 May 2014, 22:48

              @al hart said:

              Thanks Dan - that works!

              Is there anything similar for Ruby 1.8, or do I need to continue to use Win32API.so to read the registry?

              WHAT works ?

              (I talked about at least 3 ways.)

              IF you went with the low level system calls, via Win32API class then you use the "Win32API.rb" wrapper script that comes with Ruby 1.9.x and higher.

              As I said, ... for Ruby 1.8.6, you use the "Win32API.so" (and/or "win32ole.so",) files ("lifted from the Windows Installer package",) that I posted in the "Plugins" forum. (Use the Author Index Sticky post to find it.)

              I'm not here much anymore.

              1 Reply Last reply Reply Quote 0
              • D Offline
                Dan Rathbun
                last edited by 9 May 2014, 12:03

                You can ALSO instead use the WIN32OLE class ("win32ole.rb" all lowercase,) and instantiate a ref to a Windows Scripting Host object, which has SAFE functions to access the registry.
                http://msdn.microsoft.com/en-us/library/x05fawxd(v=vs.84).aspx

                Something like this (tested):

                module MyCode
                
                  def self.sketchup_install_location()
                
                    require "win32ole"
                
                    wsh = WIN32OLE.new("WScript.Shell")
                    begin
                      val = wsh.RegRead("HKLM\\Software\\Wow6432Node\\SketchUp\\SketchUp 2014\\InstallLocation\\")
                    rescue WIN32OLERuntimeError
                      val = wsh.RegRead("HKLM\\Software\\SketchUp\\SketchUp 2014\\InstallLocation\\")
                    end
                
                    wsh.ole_free
                
                    return val
                
                  end
                
                end
                

                Add: The value returned (in my test.) is "UTF-8" encoded, which is a good thing.

                FYI: The path to the key for SketchUp < 2013 is under:
                "HKLM\Software\Google\Google SketchUp 8\InstallLocation\"
                etc. for older versions.
                But "win32ole.so" does not come with older version of SketchUp. You will need to provide a 1.8.6 compiled versions of the so file. [url=http://sketchucation.com/forums/viewtopic.php?t=42732:27osqac0]It is posted (by me,) in the "Plugins" forum[/url:27osqac0].

                💭

                I'm not here much anymore.

                1 Reply Last reply Reply Quote 0
                • A Offline
                  Al Hart
                  last edited by 16 Jul 2014, 15:05

                  @dan rathbun said:

                  @al hart said:

                  Thanks Dan - that works!

                  WHAT works ?

                  (I talked about at least 3 ways.)

                  We wound up using win32ole and RegRead

                  However we ran into problems with an uncaught exception when the Registry Key we passed did not exist, so we added a second rescue for the second registry key.

                  Like this (I added XX to the key to intentionally make it fail)

                  
                  require "win32ole"
                  
                  wsh = WIN32OLE.new("WScript.Shell")
                  begin
                  	val = wsh.RegRead("HKLM\\Software\\Wow6432Node\\SketchUpXX\\SketchUp 2014\\InstallLocation\\")
                  rescue WIN32OLERuntimeError
                  	val = ""
                  end#begin/rescue
                  
                  if (val == "")
                  	begin
                  		val = wsh.RegRead("HKLM\\Software\\SketchUpXX\\SketchUp 2014\\InstallLocation\\")
                  	rescue WIN32OLERuntimeError
                  		val = ""
                  	end#begin/rescue
                  end#if
                  
                  wsh.ole_free
                  
                  puts "VAL; " + val
                  
                  

                  Al Hart

                  http:wiki.renderplus.comimageseefRender_plus_colored30x30%29.PNG
                  IRender nXt from Render Plus

                  1 Reply Last reply Reply Quote 0
                  • 1 / 1
                  • First post
                    Last post
                  Buy SketchPlus
                  Buy SUbD
                  Buy WrapR
                  Buy eBook
                  Buy Modelur
                  Buy Vertex Tools
                  Buy SketchCuisine
                  Buy FormFonts

                  Advertisement