Skip to content

Instantly share code, notes, and snippets.

@bosko
Created August 28, 2009 13:17
Show Gist options
  • Save bosko/176980 to your computer and use it in GitHub Desktop.
Save bosko/176980 to your computer and use it in GitHub Desktop.
A way to change environment variables in Windows and notify processes about change.
# Not my idea. Don't remember where I've picked it up.
# Just stored here for future reference.
require 'win32/registry.rb'
require 'Win32API'
Win32::Registry::HKEY_CURRENT_USER.open('Environment', Win32::Registry::KEY_WRITE) do |reg|
reg['my_value'] = '123'
end
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment