views:

299

answers:

3

In Windows, is there a way to check for the existence of an environment variable for another process? Just need to check existence, not necessarily get value.

I need to do this from code.

A: 

With a utility:

You can use Process Explorer.

Right click on the process, go to Properties... and there is an Environment tab which lists the environment variables for that process.

With code:

There doesn't appear to be a Win32 API call to do this directly, but apparently you get fiddle with the results of GetProcessStrings to get access to this information. This CodeProject article has some code to get you started.

Sean Bright
Thanks, but should have clarified looking for a way to do this from code ...
jalex
A: 

You can use the 'tasklist' command from a dos prompt or bat. See this dupe: http://stackoverflow.com/questions/302609/inside-a-batch-file-how-can-i-tell-whether-a-process-is-running

Noah
+2  A: 

If you know the virtual address at which the environment is stored, you can use OpenProcess and ReadProcessMemory to read the environment out of the other process. However, to find the virtual address, you'll need to poke around in the Thread Information Block of one of the process' threads.

To get that, you'll need to call GetThreadContext() after calling SuspendThread(). But in order to call those, you need a thread handle, which you can get by calling CreateToolhelp32Snapshot with the TH32CS_SNAPTHREAD flag to create a snapshot of the process, Thread32First to get the thread ID of the first thread in the process, and OpenThread to get a handle to the thread.

Adam Rosenfield
+1 great answer. You'll still need to dig around a bit to find a definition of the thread context structure. The NT DDK and Wine are good places for that kind of thing. Looks like maybe it's the 8th pointer from the beginning of the structuree?
Marsh Ray