windows - How to propagate environment variables to the system

A

armin

Hi windows-experts,

I would like to write a setup script that sets
some variables for the entire system like an installer.
When a new command prompt is opened, the values are now.

I found this microsoft link and it seems possible via the
registry.
http://support.microsoft.com/kb/q104011/

Does anybody happen to know the MS-World and have a working
example?

Thanks,
-Armin
 
G

gregarican

Armin said:
I would like to write a setup script that sets
some variables for the entire system like an installer.
When a new command prompt is opened, the values are now.



You don't have to dig that deep in order to accomplish. This a quick
way to get this done is through using the Ruby built-in ENV method. For
example to create a new environmental variable called 'foo' and assign
it the value 'bar' just issue the following:

ENV['foo']='bar'

then you can check the value by either issuing:

ENV['foo']

or else using the system method, passing along the set command:

system('set foo')

Does this help?
 
A

armin

Sorry, but if you open a new command prompt window, foo is not known.

Try this:
set foo="34" in one command prompt

open a new comand prompt
type
set
foo will not show up.
 
M

Michael Neumann

Armin,

Take a look at ruby/ext/Win32API/lib/win32/registry.rb

I guess this is what you want.

Regards,

Michael
 
G

gregarican

armin said:
Try this:
set foo="34" in one command prompt


open a new comand prompt
type
set
foo will not show up.

Oh sorry. Didn't see that you wanted these environmental variable to
persist between separate NTVDM shells. Michael's advice regarding the
registry.rb is on target...
 
A

Alan Chen

To get the registry settings having to do with environment to stick
without rebooting you also need to broadcast a message to notify
processes of the change. Here's my revant code snippet: (Watch the urls
in my comments. they may wrap badly)

# broadcast a settings change
# http://support.microsoft.com/?kbid=104011 the exact issue
# WM_SETTINGCHANGE info
#
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/wm_settingchange.asp
# SendMessage info
#
http://msdn.microsoft.com/library/d...agesandmessagequeuesfunctions/sendmessage.asp

require 'Win32API'
SendMessage = Win32API.new("user32", "SendMessage", ['L', 'L', 'P',
'P'], 'L')
HWND_BROADCAST = 0xffff
WM_SETTINGCHANGE = 0x001A
puts SendMessage.Call(HWND_BROADCAST,WM_SETTINGCHANGE, 0,
"Environment")

It took me a week of head-scratching to figure out that registry.rb
made the right changes, but they weren't being
accepted quite correctly - especially when you want to use other Env
variables in your definition. e.g. SRCTREE = %HOME%/blah/blah

HTH,
- alan
 
S

Steve Callaway

You need to use setx.exe (downloadable from
microsoft.com) and then flush a msg to
WM_SETTINGCHANGE, an example of how to do this is at
http://rubyforge.org/snippet/detail.php?type=3Dsnippet&id=3D56

rgds

Steve Callaway

--- Michael Neumann said:
Armin,
=20
Take a look at
ruby/ext/Win32API/lib/win32/registry.rb
=20
I guess this is what you want.
=20
Regards,
=20
Michael
=20

=20
=20
=20



=09
____________________________________________________
Start your day with Yahoo! - make it your home page=20
http://www.yahoo.com/r/hs=20
=20
 

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

Forum statistics

Threads
474,175
Messages
2,570,946
Members
47,498
Latest member
yelene6679

Latest Threads

Top