How to read the windows registry?
-
http://code.google.com/apis/sketchup/docs/ourdoc/sketchup.html#read_default ? reads .ini on Mac and Registry on Win/PC ?? But you need to know what you are looking up...
You can also write .bat/.cmd files for Windows that read/write registry values, using them inside SUp might mean they are ineffective - SUp reads the registry settings on startup and writes the current settings on close, so any changes tou have made to the Registry in between might get lost ? -
Seems that the Win32 module already got some wrappers for the registry: http://ruby-doc.org/stdlib/libdoc/Win32API/rdoc/classes/Win32/Registry.html
-
@thomthom said:
Seems that the Win32 module already got some wrappers for the registry: http://ruby-doc.org/stdlib/libdoc/Win32API/rdoc/classes/Win32/Registry.html
Aha...I recently came across that too, Thomthom! I think that 'registry.rb' is the ticket. While it is part of the Win32 API, it looks quite simple to use. Thanks!
-
Dale I recently tried to include the registry.rb and ran into problems. Probably not due to Ruby or SU, but just because I didn't know what I was doing. So I would be interested in what you do to make the registry.rb file work. I felt like there was some circular "require"ing going on, and I never quite figured it out. So if you do, I'd like to know how to do it please. I am looking at writing a script that needs to access the registry. Thanks!
Chris
-
Ok, I am successfully reading the windows registry. However, I have run into a roadblock on Windows Vista systems. It seems as though Vista prevents access to reading the registry unless you are logged in as an administrator.
If anyone knows how to get around this issue, please let me know!
This is the code I am using:
require 'Win32API' require 'registry' Win32;;Registry;;HKEY_CURRENT_USER.open('Software\Glare Technologies\Indigo Renderer') do |reg| #this line works fine... reg_typ, reg_val = reg.read('InstalledVersion') #...but this line fails on Vista only. return reg_val end
-
Are you an 'Administrator' ? See Vista Users C'Panel...
-
@tig said:
Are you an 'Administrator' ? See Vista Users C'Panel...
Not going to work for a lot of users, though.
-
@remus said:
@tig said:
Are you an 'Administrator' ? See Vista Users C'Panel...
Not going to work for a lot of users, though.
Unfortunately, if you are not 'the' [or 'an'] Administrator then Vista is a pig ! SUp Ruby has built-in read/write registry methods, BUT often you can't write stuff that gets kept as SUp reverts on close...
Sketchup.read_default("section","variable","default") Sketchup.write_default("section","variable","my_value")
Have limited uses...
You can also use external .bat or .cmd files to do stuff to the registry but again you could be prompted as user... -
@whaat said:
Ok, I am successfully reading the windows registry. However, I have run into a roadblock on Windows Vista systems. It seems as though Vista prevents access to reading the registry unless you are logged in as an administrator.
If anyone knows how to get around this issue, please let me know!
This is the code I am using:
> require 'Win32API' > require 'registry' > > Win32;;Registry;;HKEY_CURRENT_USER.open('Software\Glare Technologies\Indigo Renderer') do |reg| #this line works fine... > reg_typ, reg_val = reg.read('InstalledVersion') #...but this line fails on Vista only. > return reg_val > end
Did you ever find a solution for that?
Is it for any value you try to read? Any part of the registry? Or the specific part you tried to read? Maybe it was due to the permission set by the application that wrote the keys. -
@chris fullmer said:
Dale I recently tried to include the registry.rb and ran into problems. Probably not due to Ruby or SU, but just because I didn't know what I was doing. So I would be interested in what you do to make the registry.rb file work. I felt like there was some circular "require"ing going on, and I never quite figured it out. So if you do, I'd like to know how to do it please. I am looking at writing a script that needs to access the registry. Thanks!
Chris
Where did you get the registry.rb?
-
If this is a PC based tool then why not write a separate short batch file that examines the registry and then writes its findings to a temp file. You can run the xxx.bat file using UI.openURL("pathtoBATfile") and then read its temp file, then delete it to tidy up...
-
So if you wanted to use the registry, did you copy the file from a standard Ruby installation? I extracted one from an 1.8.0 Build 10 package.
I then managed to access the registry - but how would you package your plugin? What if other plugins also need registry access? Should I modify the registry.rb and wrap the code into my own namespace?Or would it be fine to load only if Win32::Registry isn't defined?
require 'C:\ruby\lib\ruby\1.8\win32\registry.rb' if defined?(Win32::Registry).nil?
-
Why not just use a plain-text configuration file located in your plug-in's ".../Plugins/pluginname" folder. You could use INI, XML, JSON, RON, etc.
This would have the advantages of working on both Vista and OSX transparently, and it would probably be more powerful and easier to implement, to boot.
As a long-time Windows user, I think the registry was a terrible idea that was also quite poorly implemented. I understand that it was designed to make the job of administrators of large corporate networks easier, but as an individual computer user, I tend to prefer programs (and plug-ins ) that use a human-readable ini file of some kind.
-
@runnerpack said:
Why not just use a plain-text configuration file located in your plug-in's ".../Plugins/pluginname" folder. You could use INI, XML, JSON, RON, etc.
This would have the advantages of working on both Vista and OSX transparently, and it would probably be more powerful and easier to implement, to boot.
As a long-time Windows user, I think the registry was a terrible idea that was also quite poorly implemented. I understand that it was designed to make the job of administrators of large corporate networks easier, but as an individual computer user, I tend to prefer programs (and plug-ins ) that use a human-readable ini file of some kind.
In my case I'm reading registry info of an application - not trying to store my settings.
-
This ruby uses Win32API to read the registry, and works on Vista.
But it may not work if you are not an Administrator (who knows).
It is looking for the entry:
HKEY_LOCAL_MACHINE\SOFTWARE\Render Plus Systems\IRender_nxt_locationSo you will have to change it to the registry key you are looking for.
class Your_class def self;;get_reg_functions @@RegCloseKey = Win32API.new('advapi32', 'RegCloseKey', 'L', 'L') if @RegCloseKey == nil @@RegOpenKeyEx = Win32API.new('advapi32', 'RegOpenKeyEx', 'LPLLP', 'L') if @RegOpenKeyEx == nil @@RegQueryValueEx = Win32API.new('advapi32', 'RegQueryValueEx', 'LPLPPP', 'L') if @RegQueryValueEx == nil #@@RegCreateKeyEx = Win32API.new('advapi32'', 'RegCreateKeyEx', 'LPLLLLPPP' 'L') end#def def self;;get_registry_entry(key, sdefault = "") #trace("key; %s",key) get_reg_functions key.tr!('/','\\') sret = sdefault # strip of section spos = key.Find('\\') if (spos < 1) do_error("Cannot parse registry key %s - Key must contain a backslash\n",key) return(sdefault) end#if firstpiece = key.Left(spos) mainkey = key.Mid(spos+1) if (firstpiece == "HKEY_LOCAL_MACHINE") root = HKEY_LOCAL_MACHINE elsif (firstpiece == "HKEY_CURRENT_USER") root = HKEY_CURRENT_USER else printf("Registry key must start with HKEY; %s",key) return(sdefault) end#if # strip off final key spos = mainkey.ReverseFind('\\') if (spos < 1) printf("Cannot parse registry key %s - Key must contain a backslash\n",key) return(sdefault) end#if lastpiece = mainkey.Mid(spos+1) mainkey = mainkey.Left(spos) phkey = [0].pack('L') # a 4 byte string of 0's go get handle ret = @@RegOpenKeyEx.call(root, mainkey, 0, KEY_READ, phkey) printf("ret; %s mainkey; %s\n",ret,mainkey) if (ret != 0) printf("key not found; %s\n",key); return(sdefault) end#if hkey = phkey.unpack('L').first # convert stron pointer to integer # create a buffer for the result buf = 0.chr * 1024 size = [buf.length].pack('L') # store size of buffer ret = @@RegQueryValueEx.call(hkey, lastpiece, 0, 0, buf, size) if (ret != 0) printf("location not found; %s\n",key); return(sdefault) end#if # if ret == ERROR_MORE_DATA, then we should call again with a larger buffer @@RegCloseKey.call(hkey) sret = buf.nstrip # stop at 0 byte printf("sret; %s length; %d key; %s\n",sret,sret.length,key) return sret end#def # this returns the location without a slash def self;;find_application_folder(smodule) #skey = sprintf("HKEY_LOCAL_MACHINE\\SOFTWARE\\Render Plus Systems\\%s_location", smodule) skey = sprintf("HKEY_LOCAL_MACHINE/SOFTWARE/Render Plus Systems/%s_location", smodule) sloc = get_registry_entry(skey) printf("sloc; %s from skey; %s\n",sloc,skey) if (sloc && FileTest.exists?(sloc)) sloc.tr!('/','\\') #printf("RETURNING 1 sloc; %s\n",sloc) return(sloc) if sloc != nil end#if printf("Registry entry not found; %s\n", skey) return nil end#def end#class Your_class Your_class;;find_application_folder("IRender_nxt")
Advertisement