0

Here is the context. We use our app to set many things by WMI, including environment variables.

We use this code (simplified) to set an environment Variable

ManagementClass envClass = new ManagementClass(this.oConnector.Scope, new ManagementPath(WMIHelper.Win32Environment), null);
ManagementObject mo = envClass.CreateInstance();
mo["Name"] = variable;
mo["UserName"] = user;
mo["VariableValue"] = value.Trim();
mo.Put();

Now when we open a command prompt we don't see the value of the environment variable. If I go and open computer properties and go into the Environment Variables page it's there. I press ok twice, reopen a command prompt and the variable all of a sudden is there.

Does anybody know why this is happening? It's almost as if after putting the environment variable we have to do something else to force a refresh or something.

Thanks

1
  • In raw windows apps you need to do this. Broadcast a WM_ SETTINGCHANGE message with lParam set to the string "Environment". Not sure what the WMI equivalent is. Nov 21, 2011 at 21:52

1 Answer 1

0

Are you sure that you are starting the process that reads the environment variables after setting them?

Env vars are copied when a process starts, and any changes to the system's env vars after the process starts will never be seen by the process. AFAIK, there is no way to bypass that behavior. By opening a new cmd window, you are starting a new cmd.exe process, which gets the new values of the variables.

1
  • For sure we are starting the cmd.exe after setting them. But remember we're setting the env var by WMI, not by entering it in the Env Var UI from microsoft. I've done a lot of research on this and it seems the only way to force an upgrade is to call a WinSendMessage. So now I'm trying to find a way to call a WinSendMessage by wmi
    – Escristian
    Nov 22, 2011 at 14:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.