Check for environment variable in another process?

守給你的承諾、 提交于 2019-12-03 13:24:46

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/1202653/check-for-environment-variable-in-another-process

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!