控制台应用程序启动另一个无法访问的环境变量 [英] Console application starting another process environment variables not accessible

查看:180
本文介绍了控制台应用程序启动另一个无法访问的环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个VB6可执行文件,该文件正在访问某些系统环境变量.我已经实现了一个.NET控制台应用程序,该应用程序检查这些环境变量是否存在,并根据需要创建它们,然后通过调用Process.Start运行VB6应用程序.

I have a VB6 executable which is accessing some system environment variables. I have implemented a .NET console application which checks if those environment variables exist, creates them if needed, and then runs the VB6 application by calling Process.Start.

这样做,VB6应用程序找不到环境变量,并说它们不存在.

Doing this, the VB6 application cannot find the environment variables and it says they don't exist.

如果我从Windows资源管理器中运行VB6应用程序,它将运行良好,并且可以找到变量.

If I run the VB6 application from Windows Explorer it works fine and can find the variables.

因此,似乎VB6应用程序在.NET控制台应用程序的上下文中运行,并且无法访问系统环境变量!

So it seems the VB6 app is running under the context of .NET console app and cannot access the system environment variables!

设置环境vars .NET Cosnole应用程序的代码:

foreach(var varObject in Variables)
{
    var envVar = Envrionment.GetEnvironmentVariable(varObject.Name , 
                      EnvironmentVariableTarget.Machine);
    if(string.IsNullOrEmpty(envVar)
    {
        Environment.SetEnvironmentVariable(varObject.Name,varObject.Value,
             EnvironmentVariableTarget.Machine);
    }
}

从.NET Cosnole应用程序运行VB6应用程序的代码:

var processInfo = new ProcessStartInfo(VB6ApplicationFilePath);
processInfo.UseShellExecute = true
processInfo.WindwoStyle= ProcessWindowStyle.Hidden;
Process.Start(processInfo);

推荐答案

程序环境的副本将传递给启动它的程序.由于它是一个副本,因此第二个程序仅在获得第二个程序的状态(并对其进行更改)时才能看到其状态.没有其他程序可以更改其他程序的环境.

A copy of a program's environment is passed to a program that it starts. As it is a copy the second program only sees the state it was in when given it (and changes it made). No other program can change another program's environment.

使用ShellExecute(告诉ProcessStart时)是在要求Explorer为您启动程序.该程序将获得资源管理器环境的副本.

When using ShellExecute (which you tell ProcessStart to) you are asking Explorer to start the program for you. The program will get a copy of Explorer's environment.

更改系统环境时,程序可以向所有打开的窗口发送消息,说明环境已更改(如setx一样-请参见setx /?).但是只有Explorer.exe会注意此消息.因此,只有在资源管理器收到此消息后由资源管理器启动的程序才能看到更改.

When changing the system environment, programs can send a message to all windows open saying environment has changed (as setx does - see setx /?). But ONLY Explorer.exe pays attention to this message. So only programs started by explorer after explorer receives this message will see the changes.

这些是.NET调用的API调用.在Windows中,所有程序均由CreateProcessEx(或更旧的程序CreateProcess)启动. ShellexecuteShellexecuteEx会像在资源管理器的开始-运行"对话框(Winkey + R)中键入命令一样处理该命令,然后对其进行更改并调用CreateProcessEx.

These are the API calls that .NET calls. In Windows all programs are started by CreateProcessEx (or older programs CreateProcess). Shellexecute and ShellexecuteEx process the command like you typed it in Explorer's Start - Run dialog (Winkey + R) then changes it and calls CreateProcessEx.

在命令提示符下.输入

set MyCat=PewResearch
cmd /k echo %MyCat%

我们设置了一个环境变量,启动一个新的命令提示符,打印该变量.

We set an environment variable, start a new command prompt that prints that variable.

这是通知的消息

WM_SETTINGCHANGE

系统将WM_SETTINGCHANGE消息发送给所有 SystemParametersInfo函数更改一个顶级窗口 系统范围的设置或策略设置已更改.

The system sends the WM_SETTINGCHANGE message to all top-level windows when the SystemParametersInfo function changes a system-wide setting or when policy settings have changed.

应用程序应将WM_SETTINGCHANGE发送到所有顶级窗口 当他们更改系统参数时. (此消息不能是 直接发送到窗口.)将WM_SETTINGCHANGE消息发送给 在所有顶级窗口中,请将SendMessageTimeout函数与 hwnd参数设置为HWND_BROADCAST.

Applications should send WM_SETTINGCHANGE to all top-level windows when they make changes to system parameters. (This message cannot be sent directly to a window.) To send the WM_SETTINGCHANGE message to all top-level windows, use the SendMessageTimeout function with the hwnd parameter set to HWND_BROADCAST.

这篇关于控制台应用程序启动另一个无法访问的环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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