locked
export system environment variables RRS feed

  • Question

  • Hi Friends,

    I want to append 'Path' environment variable with our application path and this new Path variable needs to be exported to all applications immediately and it should get stored in the system-path permanently.

    The API 'SetEnvironmentVariable()' is Sets the contents of the specified environment variable for the current process. So this is not going to help me, as user may open another command shell but will not get these changes.

    So I thought to do something with registry "SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment"

    RegOpenKeyExA(HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment", 0, KEY_SET_VALUE, &temp);
    RegSetValueExA(hRegKey, "Path", 0, REG_SZ, (CONST BYTE*)(const char*)data, data.GetLength());

    Soon after execution of above 2 lines, it is reflecting over My_computer-properties-Advanced-environment variables but not available for new shell. However if you click 'OK' on above properties window, this is new path available to new cmd/application.

    Could someone help me how to export this new 'Path' to come into effect immediately for all new and existing command-shell/applications.

    Thanks in advance
    - Krishna
    system environment variables or the environment variables of other processes
    Tuesday, June 24, 2008 5:46 AM

Answers

  • As documented, you have to broadcast the WM_SETTINGCHANGE message to notify other processes of the change you've made.  This is no doubt only going to affect Windows Explorer and makes your path changes only effective to new processes, not existing processes.

    Please use this forum only to ask questions about the BCL.
    Hans Passant.
    • Marked as answer by nobugz Sunday, June 29, 2008 6:56 PM
    Tuesday, June 24, 2008 11:45 AM