# Traverse windows registry

**URL:** <https://www.ruby-forum.com/t/traverse-windows-registry/89542>\
**Category:** Ruby\
**Created:** [March 21, 2007, 2:41am UTC](https://www.ruby-forum.com/t/traverse-windows-registry/89542 "2007-03-21T02:41:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shea\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecccb3/32.png) [@Shea\_M](https://www.ruby-forum.com/u/Shea_M)\
**Post date:** [March 21, 2007, 2:41am UTC](https://www.ruby-forum.com/t/traverse-windows-registry/89542/1 "2007-03-21T02:41:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Shea\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecccb3/32.png) [@Shea\_M](https://www.ruby-forum.com/u/Shea_M)\
**Post date:** [March 21, 2007, 5:55pm UTC](https://www.ruby-forum.com/t/traverse-windows-registry/89542/2 "2007-03-21T17:55:56Z")

</div>

Shea M. wrote:

> 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

---

<div class="post-metadata">

**Author:** ![Shea\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecccb3/32.png) [@Shea\_M](https://www.ruby-forum.com/u/Shea_M)\
**Post date:** [March 21, 2007, 10:56pm UTC](https://www.ruby-forum.com/t/traverse-windows-registry/89542/3 "2007-03-21T22:56:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Shea\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecccb3/32.png) [@Shea\_M](https://www.ruby-forum.com/u/Shea_M)\
**Post date:** [March 22, 2007, 1:08am UTC](https://www.ruby-forum.com/t/traverse-windows-registry/89542/4 "2007-03-22T01:08:04Z")

</div>

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
