如何使用C#获取其他进程的环境变量 [英] How to get other process's environment variable using c#

查看:48
本文介绍了如何使用C#获取其他进程的环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个提升的进程,并且我正在尝试使用C#从另一个进程中提取环境变量.我知道该变量通过使用Process Explorer存在.

I have an elevated process and I'm trying to extract an environment variable from another process using C#. I know that the variable exists by using Process Explorer.

我可以获取如下流程环境变量:

I can get the process environment variables like the following:

Process process = Process.GetProcessesByName("someprocess").First();
string value = process.StartInfo.EnvironmentVariables["var_name"];

但是

But StartInfo only returns a subset of the process env variables, passed to process.Start().

如何获取在流程运行期间设置了蜜蜂的环境变量?

How can I get environment variables that have bee set during process run time?

推荐答案

该文档没有充分记录,但是

It is not well documented, but ProcessStartInfo.EnvironmentVariables does not return the environment of a specific process.

如果您查看参考源,您会发现 调用GetEnvironmentStrings 函数.该函数返回一个指向内存块的指针,该内存块包含调用进程的环境变量 (系统变量和用户环境变量)".

If you look at the reference source, you will see that under the hood the GetEnvironmentStrings function is called. This function returns "a pointer to a block of memory that contains the environment variables of the calling process (both the system and the user environment variables)".

访问另一个进程的环境块比较棘手.Oleksiy Gapotchenko已撰写了优秀博客文章,并提供了示例解决方案.

Accessing the environment block of another process is a bit more tricky. Oleksiy Gapotchenko has written an excellent blog post and also provides a sample solution.

这篇关于如何使用C#获取其他进程的环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆