1

I need to add a new dword (32-bit) value '1001' in the following registry path:

path = "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\Zones\\3\\" 

After adding, the registry structure should look like this: enter image description here

How can achieve the same using ruby?

owgitt
  • 313
  • 5
  • 22

2 Answers2

1

This is a way to modify user environment variables, have you tried this ?

require 'win32/registry.rb'

path = "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\Zones\\3\\"

Win32::Registry::HKEY_CURRENT_USER.open('Environment', Win32::Registry::KEY_WRITE) do |reg|
  reg[path] = '1001'
end

The documentation also says you should log off and log back on or broadcast a WM_SETTINGCHANGE message to make changes seen to applications. This is how broadcasting can be done in Ruby:

require 'Win32API'  

SendMessageTimeout = Win32API.new('user32', 'SendMessageTimeout', 'LLLPLLP', 'L') 
HWND_BROADCAST = 0xffff
WM_SETTINGCHANGE = 0x001A
SMTO_ABORTIFHUNG = 2
result = 0
SendMessageTimeout.call(HWND_BROADCAST, WM_SETTINGCHANGE, 0, 'Environment', SMTO_ABORTIFHUNG, 5000, result)

You may take a look at this original answer and this usefull article.

Community
  • 1
  • 1
ZazOufUmI
  • 3,212
  • 6
  • 37
  • 67
  • Executed the above lines. But it did not make any changes in registry. – owgitt May 07 '15 at 09:12
  • @ ZazOufUmI - Even though the above lines did not answer my question exactly, it gave some hint me to find exact solution for the problem. – owgitt May 07 '15 at 09:32
1

Following lines solved the problem for me.

    Win32::Registry::HKEY_CURRENT_USER.create(path) do |reg|
       reg.write('1001', Win32::Registry::REG_DWORD, 0)
    end

Here I'm setting the value 0 for newly created dword (32-bit) value '1001' in registry.

owgitt
  • 313
  • 5
  • 22