Discussion:
Refresh Environment Variables
(too old to reply)
Carl Daniel [VC++ MVP]
2004-06-22 18:17:18 UTC
Permalink
I am using VC++ developed in MS Visual Studio. I can get the value
of an environmnent variable using GetEnvironmentVariable(...). The
trouble is when running from the debugger in VS it is the original
value sent to MS when it first started. How can I refresh the
Environment Variables at runtime within the application.
On reading this question several times, I think you're asking: "How can I
make my program see environment changes made outside the development
environment when I run my program under the debugger". If that's what
you're asking, I believe the answer is "you can't".

Another reading of this question might be "How can I make the debugger see
environment variable changes that I make in my program". If that's not the
question you're asking, please ask again!

In general, you can't change the environment of another process. Each
running process has it's own copy of the environment that's created with the
process. In order to change the environment of another running process,
that process must provide some facility for you to use - there's no
system-supplied way to tinker with another process's environment (imagine
the gigantic security hole that would be!).

That said, some applications, including Explorer.exe will reload their
environment from the registry if you send a WM_SETTINGCHANGE with
LPARAM="Registry". I have no idea if devenv.exe listens to
WM_SETTINGCHANGE, but I would NOT expect it to.

-cd
Carl Daniel [VC++ MVP]
2004-06-22 19:24:57 UTC
Permalink
Carl
What I desire is the first one ("How can I make my program see
environment changes made outside the development environment when I
run my program under the debugger"). Another post I read stated that
if an application changes an environment variable, it can let the
other applications know by broadcasting a message to that effect. It
went on to state that the broadcast message would not update their
variables, that would have to purposely do that themselves.
Unfortunately, that post did not state how to accomplish that. It
sounds like you do not think an application running under the Visual
Studio debugger can "refresh" its environment variables. What about
a compiled/linked stand alone application? Can it refresh its
environment variables?
See the second part of my earlier reply - you send WM_SETTINGCHANGE. You
can look it up on MSDN to see an example.

ANY application CAN act on WM_SETTINGCHANGE, but very few actually do. In
your own application you're free to do it, but you'll have to write code to
read the environment from the registry and re-define your local environment
to match the registry settings.
(HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session
Manager\Environment and HKEY_CURRENT_USER\Environment are where environment
settings are stored in the registry).

Note that you'd have to always load the environment from the registry when
your program is started since devenv.exe doesn't listen to WM_SETTINGCHANGE.
In general, this is, in my opinion, a bad path to follow - it mean your
program won't respect the environment it's given but will always start from
the default. Of course, you could make this environment re-loading behavior
only occur in debug builds, or only occur when running under the debugger
(see IsDebuggerAttached in MSDN).

-cd

Continue reading on narkive:
Loading...