Get registry string from ruby 2.0
-
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?
-
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 theDL
class (which is more C-like,) which in Ruby 2.x, becomes a wrapper toFiddle
.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.)
-
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
orFiddle
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! ) -
Thanks Dan, this looks great.
Let me try it out
-
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 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.)
-
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).aspxSomething 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]. -
@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
Advertisement