System.Diagnostics.Process.StartInfo.EnvironmentVariables is lying to me

  • Thread starter Thread starter ceh
  • Start date Start date
C

ceh

Hi, I'm using c# vs 2005 win xp pro sp2.

I create a cmd prompt with pInvoke via CreateProcessWithLogonW
Taskmgr shows that cmd instance running as that user.
I type set in that cmd window and it shows me the env vars.
Most importantly, it also shows a custom env var I have set.

Later, I open that proc via
StringDictionary env = myProcess.StartInfo.EnvironmentVariables;
I look at this value in the debugger and all it over shows me is the
standard system env vars.
It never shows me the ones that the process actually has.
It's as though, MS just opens the registry env vars for the user
profile and returns that when myProcess.StartInfo.EnvironmentVariables
is called.

Does anyone know if this is the case?

If so, myProcess.StartInfo.EnvironmentVariables, is lying to us.

Can anyone shed some light?

Thanks.
 
Most importantly, it also shows a custom env var I have set.
This custom environment variable... when and how have you set it?

Marc
 
As per the documentation:

http://msdn2.microsoft.com/en-us/library/system.diagnostics.process.startinfo.aspx
<q>If you did not use the Start method to start a process, the
StartInfo property does not reflect the parameters used to start the
process. For example, if you use GetProcesses to get an array of
processes running on the computer, the StartInfo property of each
Process does not contain the original file name or arguments used to
start the process.</q>

Although original file name and arguments are the example in this
case, it seems a reasonable extension for this to include the (start)
environment variables. I've looked in reflector, and it does pretty
much do as you say - i.e. when first fetched it looks at the (local)
Environment.GetEnvironmentVariables(), which would be normal for a new
Process.

You may need to use a lower level API to extract the current env. vars
from a running process - not sure which; I've never wanted to do this.

Marc
 
Well, thanks for pointing that out.
As unfortunate as it may be.
I think MS, in this scenario should just return an empty dictionary,
or provide some programmatic info that the env it's returning is not
the processes real env.

I know how to get that info the hard way, It looks like I'm going to
have to take that route.

Thanks.
 
Well, thanks for pointing that out.
As unfortunate as it may be.
I think MS, in this scenario should just return an empty dictionary,
or provide some programmatic info that the env it's returning is not
the processes real env.

I know how to get that info the hard way, It looks like I'm going to
have to take that route.

Thanks.

Hello,

I have the same problem as you. I can't get the env variables that the
process actually has.
I I have noi idea, to get the info.
Do you have actually a solution for this problem?
Could you tell me, how does you solve it?
Could you send me a code snippet ?

Thanks and best regards

Stephan
 
Back
Top