Windows environment variable programming setting

zhaozj2021-02-16  81

Windows environment variable programming setting

This article explains the definition of environmental variables from shallow depths. Applications under different systems of Win98, WinMe, NT, XP, Win2003 have been fully analyzed. And programmatically implemented Windows environment variables. This is important for general programming and software installation programming.

What is a user environment variable, what is system environment variable:

Definition of environment variables (Reference MSDN): The environment variable is an Array consisting of String. It is a series of sets of computers and environment variables to specify options for files, temporary file directories, specific application (Application-Specific), and other similar information. By default, each process (Process) environment variable is copied from the father process. Of course, the parent process can also specify different environment variables for the child process.

Environment variables control a variety of procedures. For example, a TEMP environment variable specifies the location of a temporary file. Any user can add, modify, or delete the user's environment variable. However, only administrators can add, modify, or delete system environment variables. Using "System" in Control Panel You can customize the following variables: (1) User environment variables used to log in to the username (logged_on_user_name); for each user of a particular computer, the user environment variable is different. Variables include any content set by the user, and all variables defined by the application, such as the path to the application file. (2) System environment variables; administrators can change or add applications to systems (thus applying to all users in the system). During installation, the Windows installer configures the default system variable, such as the path to the Windows file.

The role of the two user environment variables, the role of the system environment:

For example, it is often an explanation of a problem. Under Win2000, we observe "path" environment variables.

Path =% systemroot% / system32. But "SystemRoot" is another environment variable, systemRoot = D: / Winnt, so the path of the final characterization of the environment variable "TMP" is "D: / WinNT / System32". If we manually edit the system environment variable Path. After modifying path =% systemroot% / system32; c: / program files. Then design a code to implement the browser to open the directory "C: / Program Files". This only involves a simple Shellexecute API:

Shellexecute (NULL, "Explore", "Internet Explorer", NULL, "C: // Program Files", SW_SHOWNORMAL);

Equivalent to

Shellexecute (NULL, "Explore", "Internet Explorer", NULL, "% PATH%",

SW_SHOWNORMAL);

But the latter has great flexibility, and does not modify the code only modify the environment variable Path to complete the new function. This has a good meaning for the software installer. Skilled application environment variables help to write flexible, modular software.

Although the Win2000 is explained as an example, it is equally applicable to other Windows systems, just the way the Win98 series sets the environment variables. Details are as follows: Set the algorithm for environment variables.

Third to set the algorithm for environmental variables:

For Win98, WinMe, NT, XP, Win2003 package operating system, can be divided into two categories: Win98 series, NT series. They set the system environment variables. But it is the same for setting the environment variable of the current process. A About system environment variable

1) Algorithm for setting system environment variables in Win98 series:

Familiar with Win98 is clear, there is a "autoexec.bat" file in the C drive, the system starts to execute it, so this is where the system environment variable is set, adding an environment variable just to add a line

SET PATH = C: / Folder1 / folder2 Similar statements. If you add a path, then similar statement

SET PATH = C: / Folder1 / Folder2; D: / Folder3. The system is responsible to restart changes.

2) NT series set system environment variable algorithm:

First analyze the principle of manually editing environment variables, by reading the MSDN document, discovering the system in registration

HKEY_LOCAL_MACHINE / SYSTEM / CURRENTCONTROLSET / CONTROL / Session Manager / Environment

Save the system environment variable, when manually modified, select the "OK" button to exit and send a WM_SETTINGCHANGE message to all upper level windows of the system. The system does not need to be restarted.

[Episode]: How to send a WM_SETTINGECHANGE message to all upper-level windows of the system? I have not given an example of MSDN code. I can only find a way, I have written a message that is specifically accepting the WM_SETTINGECHANGE sent to it, the code is as follows:

OnsettingChange (uint uflags, lpctstr lpszsection) {....

Then manually modify the system environment variable, which is the program to receive the message, analyze the two parameters of UFlags, LPSZSECTION, to know uflags = 0, lpszsection = "environment";

Then imitate the message, send a WM_SETTINGECHANGE message to all upper level windows of the system.

B Environment variables about the current process

This range of environment variables are simple, and the variable is valid when the process is executed, and it will be invalid when exiting. The APIs involved are limited to GetEnvironmentvariable, SetENVIRONMENTVARIABLE, GETENVIRONMENTSTRINGS, and STENVIRONMENTSTRINGS, and so on.

Four Setting Environment Variables Programming:

Email: charnquan@163.com

转载请注明原文地址:https://www.9cbs.com/read-12640.html

New Post(0)