Setting environment variables of current process by batch file

Z

Zach

I have a batch file that executes some very complicated logic. I want
to execute this batch file programatically from a console application
I've written and have the resulting environment be part of the
original process. This seems simple -- cmd.exe does it trivially by
just typing "foo.bat". But whenever I spawn a process from my console
application, only the environment of this new process is affected by
the setting of environment variables, so I seem to be missing
something.

As an ultimate hack, I thought about spawning a new process, running
the batch file and then issuing the "SET" command via redirected
Standard Input. Then capturing the output from the parent, parsing
the resulting table by splitting on the equal sign, and using
System.Environment.* to manually construct the environment I wish.

But even that depends on me being able to use the
Process.OutputDataChanged event which either is broken, or doesn't do
what I think it does. I thought it would get called back every time
it detected new data on standard out, but this does not seem to be the
case. If I manually read standard out through Process.StandardOut.Read
(), this works but it relies on some magic harcoded text to determine
the start and endpoints of the environment block since it captures all
text of the entire console window. I was hoping OutputDataChanged
would allow me to detect only text that appeared as a result of me
issuing the SET command.

Any clever ideas on how I can make this work?
 
P

Peter Duniho

Zach said:
I have a batch file that executes some very complicated logic. I want
to execute this batch file programatically from a console application
I've written and have the resulting environment be part of the
original process. This seems simple -- cmd.exe does it trivially by
just typing "foo.bat". But whenever I spawn a process from my console
application, only the environment of this new process is affected by
the setting of environment variables, so I seem to be missing
something.

[...]
Any clever ideas on how I can make this work?

Nothing clever. Just read the docs:
http://msdn.microsoft.com/en-us/lib...cs.processstartinfo.environmentvariables.aspx

I can think of at least two other ways to accomplish the goal of setting
the environment variables for the new process, but given that doing so
is already supported by the System.Diagnostics.Process class, why bother?

Pete
 
H

Helmut Giese

Hi,
I have a batch file that executes some very complicated logic. I want
to execute this batch file programatically from a console application
I've written and have the resulting environment be part of the
original process.
you mean "the child process changes _its_ environment" and the
parent's environment should see / be influenced by these changes?

If this is a correct description, it won't work - OSes are not
designed this way. Probably for good reasons (e.g. security): Just
imagine the PATH for the parent process has suddenly changed.

Look at it this way: If a process could influence another process's
environment, it could influence the other process behind its back -
and that's certainly not a good idea.
This seems simple -- cmd.exe does it trivially by
just typing "foo.bat".
How do you come to this conclusion? I don't believe it.
But whenever I spawn a process from my console
application, only the environment of this new process is affected by
the setting of environment variables, so I seem to be missing
something.
No, you had just a wrong assumption, that's all.
Any clever ideas on how I can make this work?
If you want 2 processes to exchange information there are lots of ways
to achieve this: files, pipes, messages, ...
HTH
Helmut Giese
 
P

Peter Duniho

Helmut said:
Hi,

you mean "the child process changes _its_ environment" and the
parent's environment should see / be influenced by these changes?

Ah, I think I misread the original question.
[...]
This seems simple -- cmd.exe does it trivially by
just typing "foo.bat".

How do you come to this conclusion? I don't believe it.

It does what he says. But it does because that's exactly what batch
files are. They are executed _within_ the original cmd.exe process, and
a "set" statement is _interpreted_ by the cmd.exe process to change that
process's environment.

The obvious conclusion is that the most effective approach would be to
reimplement the batch file interpretation in the original process, doing
the same work cmd.exe would do. Of course, that could be very
complicated, at least if you are dealing with non-trivial batch files
(one filled just with "set" statements would be easy).

IMHO, the easier alternative would be to simply examine the environment
state of the spawned process after it's completed, setting the local
environment to match. Depending on the exact needs, it could as simple
as copying the spawned process's environment variables, or one might
also need to handle deletions by comparing the current process's
environment to the spawned process's environment and removing any
variables that no longer exist. Either way, it should not be that
difficult.

Pete
 
H

Helmut Giese

Hi Peter,
It does what he says. But it does because that's exactly what batch
files are. They are executed _within_ the original cmd.exe process, and
a "set" statement is _interpreted_ by the cmd.exe process to change that
process's environment.
yes, you are right - I was thinking of a program called from within
the batch file which then modifies its environment - and those changes
wouldn't be visible any more once the batch file terminated.
Best regards
Helmut Giese
 

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

Top