How to read the windows registry?

How to read the windows registry?

Postby Whaat » Tue Jul 14, 2009 11:31 pm

From the little research I have done, it seems like the only way to ready values from the Windows registry is to use the WinOLE library. Is there a way to do it without using a library?

Can someone post some sample code for how to ready values from the registry using an external library?

Thanks in advance!
0

Whaat 
PluginStore Author
PluginStore Author
 

Re: How to read the windows registry?

Postby thomthom » Wed Jul 15, 2009 12:22 am

You could make calls to the Windows API directly. I just started calling the Windows API to call file functions that support Unicode characters.

Or are you looking for an abstracted way of calling the registry?
0
Thomas Thomassen — SketchUp Monkey & Coding addict
List of my plugins and link to the CookieWare fund
User avatar
thomthom 
PluginStore Author
PluginStore Author
 

Re: How to read the windows registry?

Postby Whaat » Wed Jul 15, 2009 3:23 am

I'm just not familiar with the Windows API and I was wondering if you can access the registry without using it. I am guessing not.
0

Whaat 
PluginStore Author
PluginStore Author
 

Re: How to read the windows registry?

Postby TIG » Wed Jul 15, 2009 9:24 am

http://code.google.com/apis/sketchup/do ... ad_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 ?
0
TIG
User avatar
TIG 
Global Moderator
 

Re: How to read the windows registry?

Postby thomthom » Wed Jul 15, 2009 9:38 am

Seems that the Win32 module already got some wrappers for the registry: http://ruby-doc.org/stdlib/libdoc/Win32 ... istry.html
0
Thomas Thomassen — SketchUp Monkey & Coding addict
List of my plugins and link to the CookieWare fund
User avatar
thomthom 
PluginStore Author
PluginStore Author
 

Re: How to read the windows registry?

Postby Whaat » Thu Jul 16, 2009 2:54 am

thomthom wrote:Seems that the Win32 module already got some wrappers for the registry: http://ruby-doc.org/stdlib/libdoc/Win32 ... istry.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!
0

Whaat 
PluginStore Author
PluginStore Author
 

Re: How to read the windows registry?

Postby Chris Fullmer » Thu Jul 16, 2009 5:15 am

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
0
Lately you've been tan, suspicious for the winter.
All my Plugins I've written
User avatar
Chris Fullmer 
SketchUp Team
SketchUp Team
 

Re: How to read the windows registry? (new problem on Vista)

Postby Whaat » Tue Aug 11, 2009 5:06 am

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. :evil:

If anyone knows how to get around this issue, please let me know!

This is the code I am using:
Code: Select all
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
0

Whaat 
PluginStore Author
PluginStore Author
 

Re: How to read the windows registry?

Postby TIG » Tue Aug 11, 2009 7:58 am

Are you an 'Administrator' ? See Vista Users C'Panel...
0
TIG
User avatar
TIG 
Global Moderator
 

Re: How to read the windows registry?

Postby remus » Tue Aug 11, 2009 8:10 am

TIG wrote:Are you an 'Administrator' ? See Vista Users C'Panel...

Not going to work for a lot of users, though.
0

remus 
 

Re: How to read the windows registry?

Postby TIG » Tue Aug 11, 2009 8:52 am

remus wrote:
TIG wrote: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...
0
TIG
User avatar
TIG 
Global Moderator
 

Re: How to read the windows registry? (new problem on Vista)

Postby thomthom » Wed Nov 04, 2009 11:39 am

Whaat wrote: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. :evil:

If anyone knows how to get around this issue, please let me know!

This is the code I am using:
Code: Select all
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.
0
Thomas Thomassen — SketchUp Monkey & Coding addict
List of my plugins and link to the CookieWare fund
User avatar
thomthom 
PluginStore Author
PluginStore Author
 

Re: How to read the windows registry?

Postby thomthom » Wed Nov 04, 2009 11:45 am

Chris Fullmer wrote: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?
0
Thomas Thomassen — SketchUp Monkey & Coding addict
List of my plugins and link to the CookieWare fund
User avatar
thomthom 
PluginStore Author
PluginStore Author
 

Re: How to read the windows registry?

Postby TIG » Wed Nov 04, 2009 12:24 pm

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...
0
TIG
User avatar
TIG 
Global Moderator
 

Re: How to read the windows registry?

Postby thomthom » Wed Nov 04, 2009 3:37 pm

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?
0
Thomas Thomassen — SketchUp Monkey & Coding addict
List of my plugins and link to the CookieWare fund
User avatar
thomthom 
PluginStore Author
PluginStore Author
 

Re: How to read the windows registry?

Postby RunnerPack » Wed Nov 04, 2009 7:43 pm

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.
0
You might have noticed... I'm a bit of a ferpectionist.
User avatar
RunnerPack 
 

Re: How to read the windows registry?

Postby thomthom » Wed Nov 04, 2009 7:51 pm

RunnerPack wrote: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.
0
Thomas Thomassen — SketchUp Monkey & Coding addict
List of my plugins and link to the CookieWare fund
User avatar
thomthom 
PluginStore Author
PluginStore Author
 

Re: How to read the windows registry?

Postby Al Hart » Wed Nov 04, 2009 9:22 pm

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_location

So you will have to change it to the registry key you are looking for.


Code: Select all


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")
0
Al Hart
IRender nXt from Render Plus
User avatar
Al Hart 
PluginStore Author
PluginStore Author
 

SketchUcation One-Liner Adverts

by Ad Machine » 5 minutes ago



Ad Machine 
Robot
 



 

Return to Developers' Forum

Who is online

Users browsing this forum: wooseabi and 1 guest

Visit our sponsors: