Detailed Windows does not restart to make environment variable changes take effect (classic)

Source: Internet
Author: User
Tags microsoft help

After you add or modify an environment variable in the environment variable, advanced, properties, my computer, you need to restart the system for it to take effect. Is there any way to make it effective immediately? Here's a way to do this:

To modify the environment variable "PATH" as an example, after the modification is completed, enter the DOS command prompt, enter: Set Path=c:, Close the DOS window. Open the DOS window again and enter: Echo%PATH%, you can see that the PATH value set in the environment variables, properties, My computer, is already in effect.

Instead of worrying about changes in the DOS window that affect the values of environment variables, the environment variables in the DOS window are just a copy of the Windows environment variable. But modifications to the copy will cause the Windows environment variable to refresh, which is exactly what we want!

Strange Ah, the use of Win+r->cmd boot cmd.exe will find in the computer properties set in the environment variables immediately take effect, in other modes of the start of the CMD did not happen, it's strange!! After reviewing the data, setting the environment variables in the computer properties, the programs and threads that are started later take effect, and the programs that previously resided in memory do not work, and some say that kill Explorer.exe restart Explorer.exe can trigger the setting of its function.

The understanding here is that when a program starts, the environment variable is copied to the environment where the program is located, and it is not changed by programs other than that program during the execution of the program. That is, suppose we start a cmd program and then modify the environment variable settings through the Control Panel, but the environment variables owned by the CMD that have been started are not changed. If we start the CMD program after modifying the environment variable, the program will have the new environment variable.
So the conclusion is obvious: after modifying an environment variable, if the application is affected, the modification of the environment variable will be reflected in the program without restarting the computer if it is simply restarted, but if the system service is affected, A reboot is required to reflect the modification of the environment variable to the system service (because there is no way to restart the system Service Manager without restarting the computer).

Scenario One: Modify the registry and broadcast messages to the system

Methods that do not restart for immediate effect after modifying Windows environment variables

Many programs need to add their own environment variables to facilitate the use of custom. The most used is the user specified directory such as java_home variables, the program can be obtained according to the variable%java_home%, to obtain the corresponding set of strings.


The general practice is to specify when installing, or in the program settings.

The temporary setting of environment variables with batch processing does not mention that the environment variables are always in effect.

The general practice is to modify the environment variable registry.

Environment variables that are valid throughout Windows are
Hkey_local_machinesystemcontrolset001controlsession managerenvironment
Set in

The environment variables that are valid for the current user are
Hkey_current_userenvironment
Set in

It's really just a simple meta-data

But often modified after play can not be effective immediately, often need to restart the system.

To resolve the immediate issue, you can then set the environment variable in My computer's properties to determine it.

In this case, the process must have read the registry and then called a system function to update the entire system's variables. Must be programmed to solve.

Unfortunately, the vast expanse of the sea, not receiving.

Pound, direct download open innosetup Code, search environment, indeed found.

Very simple:

SendMessageTimeout (Hwnd_broadcast, Wm_settingchange, 0,lparam ("Environment"), Smto_abortifhung, & Msgresult);

Send a global broadcast, waiting for their respective corresponding, immediately effective.

{Note:we originally used SendNotifyMessage to broadcast the message but it
Turned out this while it worked fine on NT 4 and the IT didn ' t work on XP
--The string "Environment" in LParam would is garbled on the receiving
End (why I ' m not exactly sure). We now use SendMessageTimeout as directed
In the KB article 104011. It isn ' t as elegant since it could cause us to
Be delayed if another app was hung, but it ' ll has to do. }

Scenario two: After setting in Advanced properties, set the value of path at any CMD to achieve the purpose of global broadcast

Allow environment variables to take effect without restarting Windows

Many installers do not require a restart of Windows after the installation is complete, and the new environment variable is already in effect, which makes it perfectly possible to make the environment variable effective without restarting Windows.

I can search for a method that does not need to restart Windows for environment variables to take effect, only three types of scenarios are found:

Download a dedicated software
Make a small program
Modify the Registration Form
Since I am a person who is too lazy to restart Windows, will I bother to follow the three methods mentioned above? No way, I only have to grope. Finally find a very simple way to get the environment variable to work directly under Windows:

Go to My Computer > Properties > Advanced > Environment variables, add a new environment variable or modify an existing environment variable, and then run "DOS command Prompt" or run cmd, assuming you want to modify the path variable, regardless of the original value of path, In the DOS window directly modify PATH to any value, such as: input: Set path=c:, Close the DOS window. Open the DOS window again, enter: Echo%PATH%, you can see that my Computer > properties > Advanced > Environment Variables in the PATH is already in Windows global effect.

Do not worry about the changes in the DOS window will affect my Computer > properties > Advanced > Environment variable changes, DOS window environment variable is only a copy of the Windows environment variables, copy changes will not affect the original, but will trigger the original refresh, this is what I want-- Let the environment variable take effect.

Programme III:

Modify the Windows environment variables and make them effective immediately

You want to programmatically modify the value of an environment variable under windows, and you want it to take effect as soon as you finish modifying it.
I. Modification of environment variables
(1) Modify an environment variable for the current process: setenvironmentvariable.
(2) Modify the system environment variable: Modify in Registry Hkey_local_machinesystemcurrentcontrolsetcontrolsession Managerenvironment.
(3) Modify user Environment variables: Hkey_current_userenvironment Modify in the registry.
Second, the effect of environmental variables
Search on the Internet, there are two kinds of statements:
(1) SendMessage (hwnd_broadcast,wm_settingchange,0, (LPARAM) TEXT ("Environment"));
(2) SendMessageTimeout (Hwnd_broadcast, Wm_settingchange, 0,lparam ("Environment"), Smto_abortifhung, & Msgresult);
Don't try, just remember here.

==========================================================================================

The following is a web-read of the official Windows profile, which explains how the environment variable will be effective immediately after the change.

Source:

Microsoft Help and technical support

Http://support.microsoft.com/kb/104011/zh-cn

Jonne:

Note: (Chinese version-machine translation) does not guarantee the correctness of the original text, the Chinese version has been attached to the original English

You can modify the user environment variables by editing the following registry key:

HKEY_CURRENT_USER/
Environment

, you can modify the system environment variables by editing the following registry key:

HKEY_LOCAL_MACHINE/
SYSTEM/
CurrentControlSet/
Control/
Session Manager/
Environment

Note All environment variables that are required to be extended (for example, using%system%) must be stored as REG_EXPAND_SZ registry values in the registry. Do not expand any values of type REG_SZ when reading from the registry.

Note that RegEdit.exe does not have a way to add REG_EXPAND_SZ. When you manually edit these values by using RegEdt32.exe.

However, be aware that changes made to environment variables do not cause immediate changes. For example, if you start another command prompt after a change has been made, the environment variable reflects the previous (not current) value. The changes do not take effect until you log off and then sign in again.

To affect these changes without logging off, wm_settingchange all windows to the broadcast message in the system, so that any interested applications (such as Windows Explorer, Program Manager, Task Manager, Control Panel, and so on) can perform the update.

For example, the code snippet for Windows NT-based systems should propagate changes made to the environment variables used in the command prompt:

SendMessageTimeout (Hwnd_broadcast, Wm_settingchange, 0,
(LPARAM) "Environment", Smto_abortifhung,
&dwreturnvalue);

No response to this message is included with Windows 95 and Windows 98, including Windows Explorer and the project manager application. This way, on Windows 95 and Windows 98, it is technically possible to implement this article when the third-party application except to be notified does not work. The only way to change the global environment variables in Windows 95 is to modify the Autoexec.bat file and restart it.

English Original:

Address: http://support.microsoft.com/kb/104011/en-us/

How to propagate environment variables to the system

You can modify user environment variables by editing the following Registry key:

Hkey_current_user/environment

You can modify system environment variables by editing the following Registry key:

Hkey_local_machine/system/currentcontrolset/control/session manager/environmentnote that any environment variabl E that needs to is expanded (for example, when you use%system%) must is stored in the registry as a REG_EXPAND_SZ Registr Y value. Any values of type REG_SZ'll is expanded when read from the registry.

Note that RegEdit.exe does not has a to add REG_EXPAND_SZ. Use RegEdt32.exe when editing these values manually.

However, note that modifications to the environment variables does not result in immediate change. For example, if you start another Command Prompt after making the changes, the environment variables would reflect the Prev IOUs (not the current) values. The changes does not take effect until you log off and then log back on.

To effect these changes without have to log off, broadcast a WM_SETTINGCHANGE message to all windows in the system, so T Hat any interested applications (such as Windows Explorer, program Manager, Task Manager, Control Panel, and so forth) can Perform an update. For example, on Windows nt-based systems, the following code fragment should propagate the changes to the environment Vari Ables used in the Command prompt:sendmessagetimeout (hwnd_broadcast, Wm_settingchange, 0,
(LPARAM) "Environment", Smto_abortifhung,
   , &dwreturnvalue); None of the applications, ship with Windows 98, including Windows Explorer and program Manager, respond to the message. Thus, while this article can technically being implemented on Windows and Windows 98, there are no effect except to notify Third-party applications. The only method of changing global environment variables on Windows are to modify the Autoexec.bat file and reboot.&nbsp ; Applies tomicrosoft Windows Server 2003, Datacenter Edition (32-bit x86) Microsoft Windows Server 2003, Enterprise Edition (32-bit x86) Microsoft Windows Server 2003, Standard Edition (32-bit x86), Microsoft Windows Server 2003, Web editionmicrosoft Windows XP Home editionmicrosoft Windows XP Media Center editionmicrosoft Windows XP Professionalmicrosoft Windows Advanced Ser Vermicrosoft Windows Datacenter servermicrosoft Windows $ Professional editionmicrosoft Windows Servermicros OFT Windows NT 4.0Microsoft Windows NT Server 3.51  Source  http://blog.csdn.net/lioncode/article/details/6037683

Detailed Windows does not restart to make environment variable changes effective (classic)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.