Changing registry values with Win32::Registry

Discussion in 'Ruby' started by Collin Miller, Jun 14, 2007.

  1. I'm trying to change a registry value for an IPTV SDK

    This code:

    Win32::Registry::HKEY_LOCAL_MACHINE.open("SOFTWARE\\PATH_TO_KEY\
    \").write("IgnoreTinyIFrames",Win32::Registry::REG_DWORD,0)

    Returns this error:

    Win32::Registry::Error: Access is denied.
    from c:/ruby/lib/ruby/1.8/win32/registry.rb:743:in `write'
    from (irb):150
    from ♥:0

    Anybody know how I can ensure access to this key?
    Collin Miller, Jun 14, 2007
    #1
    1. Advertising

  2. Collin Miller

    Jano Svitok Guest

    T24gNi8xNC8wNywgQ29sbGluIE1pbGxlciA8Y29sbGludG1pbGxlckBnbWFpbC5jb20+IHdyb3Rl
    Ogo+IEknbSB0cnlpbmcgdG8gY2hhbmdlIGEgcmVnaXN0cnkgdmFsdWUgZm9yIGFuIElQVFYgU0RL
    Cj4KPiBUaGlzIGNvZGU6Cj4KPiBXaW4zMjo6UmVnaXN0cnk6OkhLRVlfTE9DQUxfTUFDSElORS5v
    cGVuKCJTT0ZUV0FSRVxcUEFUSF9UT19LRVlcCj4gXCIpLndyaXRlKCJJZ25vcmVUaW55SUZyYW1l
    cyIsV2luMzI6OlJlZ2lzdHJ5OjpSRUdfRFdPUkQsMCkKPgo+IFJldHVybnMgdGhpcyBlcnJvcjoK
    Pgo+IFdpbjMyOjpSZWdpc3RyeTo6RXJyb3I6IEFjY2VzcyBpcyBkZW5pZWQuCj4gICAgICAgICBm
    cm9tIGM6L3J1YnkvbGliL3J1YnkvMS44L3dpbjMyL3JlZ2lzdHJ5LnJiOjc0MzppbiBgd3JpdGUn
    Cj4gICAgICAgICBmcm9tIChpcmIpOjE1MAo+ICAgICAgICAgZnJvbSCivjowCj4KPiBBbnlib2R5
    IGtub3cgaG93IEkgY2FuIGVuc3VyZSBhY2Nlc3MgdG8gdGhpcyBrZXk/Cgp1c2UgV2luMzI6OlJl
    Z2lzdHJ5OjpLRVlfV1JJVEUgb3IgYW5vdGhlciBLRVlfKiBhY2Nlc3Mgc3BlY2lmaWVyIGFzCnRo
    ZSBuZXh0IHBhcmFtZXRlcjoKCldpbjMyOjpSZWdpc3RyeTo6SEtFWV9MT0NBTF9NQUNISU5FLm9w
    ZW4oIlNPRlRXQVJFXFxQQVRIX1RPX0tFWVxcIixXaW4zMjo6UmVnaXN0cnk6OktFWV9XUklURSku
    d3JpdGUoIklnbm9yZVRpbnlJRnJhbWVzIixXaW4zMjo6UmVnaXN0cnk6OlJFR19EV09SRCwwKQoK
    Si4K
    Jano Svitok, Jun 15, 2007
    #2
    1. Advertising

  3. Collin Miller

    dima Guest

    On Jun 15, 9:51 am, "Jano Svitok" <> wrote:
    > On 6/14/07, Collin Miller <> wrote:
    >
    > > I'm trying to change a registry value for an IPTV SDK

    >
    > > This code:

    >
    > > Win32::Registry::HKEY_LOCAL_MACHINE.open("SOFTWARE\\PATH_TO_KEY\
    > > \").write("IgnoreTinyIFrames",Win32::Registry::REG_DWORD,0)

    >
    > > Returns this error:

    >
    > > Win32::Registry::Error: Access is denied.
    > > from c:/ruby/lib/ruby/1.8/win32/registry.rb:743:in `write'
    > > from (irb):150
    > > from ♥:0

    >
    > > Anybody know how I can ensure access to this key?

    >
    > use Win32::Registry::KEY_WRITE or another KEY_* access specifier as
    > the next parameter:
    >
    > Win32::Registry::HKEY_LOCAL_MACHINE.open("SOFTWARE\\PATH_TO_KEY\\",Win32::Registry::KEY_WRITE).write("IgnoreTinyIFrames",Win32::Registry::REG_DWORD,0)
    >
    > J.


    Look at https://www.ruby-forum.com/topic/76585
    dima, Jun 15, 2007
    #3
  4. How do we handle null value from opened HKEY instance. if
    (reg.read_i(name)!="") in below line I have tried but it doesn't work
    properly.

    Ex:
    Win32::Registry::HKEY_LOCAL_MACHINE.open(REG_PATH_1,Win32::Registry::Constants::KEY_ALL_ACCESS)
    do |reg|
    if (reg.read_i(name)!="")
    return reg.read_i(name)
    end
    end

    I need another instance to open for different REG_PATH. The reason
    behind that is if the reg key is in first path then return the value
    other wise open the key to read the reg key from different path.

    Thanks
    --
    Posted via http://www.ruby-forum.com/.
    Navaneet Kumar, Jul 8, 2010
    #4
  5. On Thu, Jul 8, 2010 at 10:52 AM, Navaneet Kumar <> wrot=
    e:
    > How do we handle null value from opened HKEY instance. if
    > (reg.read_i(name)!=3D"") in below line I have tried but it doesn't work
    > properly.
    >
    > Ex:
    > Win32::Registry::HKEY_LOCAL_MACHINE.open(REG_PATH_1,Win32::Registry::Cons=

    tants::KEY_ALL_ACCESS)
    > do |reg|
    > =A0 =A0if (reg.read_i(name)!=3D"")
    > =A0 =A0 =A0 =A0 =A0 =A0 =A0return reg.read_i(name)
    > =A0 =A0 =A0 =A0 =A0 =A0end
    > =A0 =A0end
    >
    > I need another instance to open for different REG_PATH. The reason
    > behind that is if the reg key is in first path then return the value
    > other wise open the key to read the reg key from different path.


    I have no idea about that API, but if read_i is returning nil for
    non-existing keys, you can try this:

    unless (key =3D reg.read_i(name)).nil?
    return key
    end

    Jesus.
    Jesús Gabriel y Galán, Jul 8, 2010
    #5
  6. Jesús Gabriel y Galán wrote:
    > On Thu, Jul 8, 2010 at 10:52 AM, Navaneet Kumar <>
    > wrote:
    >> � �end
    >>
    >> I need another instance to open for different REG_PATH. The reason
    >> behind that is if the reg key is in first path then return the value
    >> other wise open the key to read the reg key from different path.

    >
    > I have no idea about that API, but if read_i is returning nil for
    > non-existing keys, you can try this:
    >
    > unless (key = reg.read_i(name)).nil?
    > return key
    > end
    >
    > Jesus.


    Thanks for your reply.

    Just clarification, The specified key is not exist not the value is
    having null. I mean the 'name' key is not exist in that path. How do we
    handle this in ruby.

    --
    Posted via http://www.ruby-forum.com/.
    Navaneet Kumar, Jul 8, 2010
    #6
  7. On Thu, Jul 8, 2010 at 11:45 AM, Navaneet Kumar <> wrot=
    e:
    > Jes=C3=BAs Gabriel y Gal=C3=A1n wrote:
    >> On Thu, Jul 8, 2010 at 10:52 AM, Navaneet Kumar <>
    >> wrote:
    >>> =EF=BF=BD =EF=BF=BDend
    >>>
    >>> I need another instance to open for different REG_PATH. The reason
    >>> behind that is if the reg key is in first path then return the value
    >>> other wise open the key to read the reg key from different path.

    >>
    >> I have no idea about that API, but if read_i is returning nil for
    >> non-existing keys, you can try this:
    >>
    >> unless (key =3D reg.read_i(name)).nil?
    >> =C2=A0 return key
    >> end
    >>
    >> Jesus.

    >
    > Thanks for your reply.
    >
    > Just clarification, The specified key is not exist not the value is
    > having null. I mean the 'name' key is not exist in that path. How do we
    > handle this in ruby.


    I don't know what the API returns in that instance. Can you try this
    and show us?

    key =3D reg.read_i(name)
    p key

    Jesus.
    Jesús Gabriel y Galán, Jul 8, 2010
    #7
  8. Collin Miller

    Heesob Park Guest

    Hi,

    2010/7/8 Navaneet Kumar <>:
    > Jes=C3=BAs Gabriel y Gal=C3=A1n wrote:
    >> On Thu, Jul 8, 2010 at 10:52 AM, Navaneet Kumar <>
    >> wrote:
    >>> =EF=BF=BD =EF=BF=BDend
    >>>
    >>> I need another instance to open for different REG_PATH. The reason
    >>> behind that is if the reg key is in first path then return the value
    >>> other wise open the key to read the reg key from different path.

    >>
    >> I have no idea about that API, but if read_i is returning nil for
    >> non-existing keys, you can try this:
    >>
    >> unless (key =3D reg.read_i(name)).nil?
    >> =C2=A0 return key
    >> end
    >>
    >> Jesus.

    >
    > Thanks for your reply.
    >
    > Just clarification, The specified key is not exist not the value is
    > having null. I mean the 'name' key is not exist in that path. How do we
    > handle this in ruby.
    >

    You can handle it like this:

    begin
    if (reg.read_i(name)!=3D"")
    return reg.read_i(name)
    end
    rescue Win32::Registry::Error =3D> e
    if e.code =3D=3D 2
    puts "#{name} key not exist!"
    end
    end


    Regards,

    Park Heesob
    Heesob Park, Jul 8, 2010
    #8
  9. Heesob Park wrote:
    > Hi,
    >
    > 2010/7/8 Navaneet Kumar <>:
    >>> non-existing keys, you can try this:

    >> having null. I mean the 'name' key is not exist in that path. How do we
    >> handle this in ruby.
    >>

    > You can handle it like this:
    >
    > begin
    > if (reg.read_i(name)!="")
    > return reg.read_i(name)
    > end
    > rescue Win32::Registry::Error => e
    > if e.code == 2
    > puts "#{name} key not exist!"
    > end
    > end
    >
    >
    > Regards,
    >
    > Park Heesob


    Thanks Park...

    I was trying the same and it works for me. Is is possible to handle
    without handling the exception?
    --
    Posted via http://www.ruby-forum.com/.
    Navaneet Kumar, Jul 8, 2010
    #9
  10. On 8 July 2010 12:37, Navaneet Kumar <> wrote:
    > Heesob Park wrote:
    >> Hi,
    >>
    >> 2010/7/8 Navaneet Kumar <>:
    >>>> non-existing keys, you can try this:
    >>> having null. I mean the 'name' key is not exist in that path. How do we
    >>> handle this in ruby.
    >>>

    >> You can handle it like this:
    >>
    >> =C2=A0 begin
    >> =C2=A0 if (reg.read_i(name)!=3D"")
    >> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0return reg.read_i(name)
    >> =C2=A0 =C2=A0 =C2=A0 =C2=A0 end
    >> =C2=A0 rescue Win32::Registry::Error =3D> e
    >> =C2=A0 if e.code =3D=3D 2
    >> =C2=A0 =C2=A0 puts "#{name} key not exist!"
    >> =C2=A0 end
    >> =C2=A0 end
    >>
    >>
    >> Regards,
    >>
    >> Park Heesob

    >
    > Thanks Park...
    >
    >


    I would do something like

    reg.read_i(name) rescue nil

    unless I wanted to know exactly what went wrong.

    I wrote some hacks for replacing key values in registry so I can dig
    them up if you want.

    HTH

    Michal
    Michal Suchanek, Jul 8, 2010
    #10
  11. Collin Miller

    Roger Pack Guest

    Roger Pack, Jul 9, 2010
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. HK
    Replies:
    1
    Views:
    3,631
    Cowboy \(Gregory A. Beamer\)
    Apr 1, 2004
  2. Replies:
    0
    Views:
    751
  3. Leny
    Replies:
    3
    Views:
    16,923
    Daniel
    Feb 1, 2005
  4. benjamin schollnick

    Remote Win32 Registry confusion?

    benjamin schollnick, Oct 25, 2003, in forum: Python
    Replies:
    1
    Views:
    398
    Bjorn Pettersen
    Oct 25, 2003
  5. MoshiachNow
    Replies:
    2
    Views:
    116
    Mumia W. (reading news)
    Nov 21, 2006
Loading...

Share This Page