traverse windows registry

S

Shea Martin

Is there an example of traversing the window registry? I can't seem to
find any documentation on Win32::Registry. The ruby install came with
some docs for Windows::Registry, but that seems to be a different animal...

THanks,
~S
 
S

Shea Martin

Shea said:
Is there an example of traversing the window registry? I can't seem to
find any documentation on Win32::Registry. The ruby install came with
some docs for Windows::Registry, but that seems to be a different animal...

THanks,
~S


I have traversal code working. Just simple recursion. But I am having
trouble looping over values. An exception is thrown whenever a value of
type REG_NONE is found. This prevents me from iterating over other
values, due to the exception. Even if I rescue the exception, I am
still kicked out of the each_value loop.

The problem is that each_value does a read, and read throws the
exception if the type is REG_NONE. see win32/registry.rb line 633.

Is there anyway around this?

Thanks,
~S
 
J

Jeremy

From looking at the source the read bit should already be in a
begin/rescue so it might be worth checking you have the latest version
of ruby and the win32 stuff? I think that the problem is actually in the
read method of registry.rb, doesn't seem to handle REG_NONE. You could
change this method to make it work. Here's the original:

def read(name, *rtype)
type, data = API.QueryValue(@hkey, name)
unless rtype.empty? or rtype.include?(type)
raise TypeError, "Type mismatch (expect #{rtype.inspect} but #{type} present)"
end
case type
when REG_SZ, REG_EXPAND_SZ
[ type, data.chop ]
when REG_MULTI_SZ
[ type, data.split(/\0/) ]
when REG_BINARY
[ type, data ]
when REG_DWORD
[ type, API.unpackdw(data) ]
when REG_DWORD_BIG_ENDIAN
[ type, data.unpack('N')[0] ]
when REG_QWORD
[ type, API.unpackqw(data) ]
else
raise TypeError, "Type #{type} is not supported."
end
end
 
S

Shea Martin

Yeah,

I notices that it would be dead simple to make it work. Just add
TypeError to the rescue clause. But I like to write portable code that
doesn't depend on changing standard libs.

I would say that this is a design flaw though, of the win32/registry.rb
code. It makes it unusable for iteration, as it is inevitable that one
will come across a REG_NONE at some point.

I ended up just copying the each_value function, and making my own
global version with took a registry entry as a parameter.

~S
From looking at the source the read bit should already be in a
begin/rescue so it might be worth checking you have the latest version
of ruby and the win32 stuff? I think that the problem is actually in the
read method of registry.rb, doesn't seem to handle REG_NONE. You could
change this method to make it work. Here's the original:

def read(name, *rtype)
type, data = API.QueryValue(@hkey, name)
unless rtype.empty? or rtype.include?(type)
raise TypeError, "Type mismatch (expect #{rtype.inspect} but
#{type} present)"
end
case type
when REG_SZ, REG_EXPAND_SZ
[ type, data.chop ]
when REG_MULTI_SZ
[ type, data.split(/\0/) ]
when REG_BINARY
[ type, data ]
when REG_DWORD
[ type, API.unpackdw(data) ]
when REG_DWORD_BIG_ENDIAN
[ type, data.unpack('N')[0] ]
when REG_QWORD
[ type, API.unpackqw(data) ]
else
raise TypeError, "Type #{type} is not supported."
end
end


Shea said:
I have traversal code working. Just simple recursion. But I am having
trouble looping over values. An exception is thrown whenever a value
of type REG_NONE is found. This prevents me from iterating over other
values, due to the exception. Even if I rescue the exception, I am
still kicked out of the each_value loop.

The problem is that each_value does a read, and read throws the
exception if the type is REG_NONE. see win32/registry.rb line 633.

Is there anyway around this?

Thanks,
~S
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,060
Latest member
BuyKetozenseACV

Latest Threads

Top