简体   繁体   中英

Console application starting another process environment variables not accessible

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 .

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

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

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

Code to set the environment vars .NET Cosnole app:

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);
    }
}

Code to run the VB6 app from .NET Cosnole app:

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.

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.

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.

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 .

At the command prompt. Type

set MyCat=PewResearch
cmd /k echo %MyCat%

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

This is the message that notifies

WM_SETTINGCHANGE

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM