Windows core programming code analysis based on visual c ++ (21) Obtaining and setting Environment Variables

Source: Internet
Author: User

An environment variable is an object with a specific name. It contains information that one or more applications will use. For example, if the system is required to run a program without telling it the complete path of the program, the system searches for this program under the current directory, you should also find the path specified in the path. You can set environment variables to better run processes.
Environment variables generally refer to the parameters used in the operating system to specify the operating system environment, such as the Temporary Folder location and system folder location. This is a bit similar to the default path in the DOS period. When you run some programs, in addition to searching in the current folder, you will also find it in the Set default path. Simply put, the "Path" is a variable, which stores the directory paths of some common commands.

Environment variables are equivalent to some parameters set for the system or user application. The specific role of environment variables is related to the specific environment variables. for example, path tells the system that when the system is required to run a program without telling it the complete path of the program, the system searches for this program under the current directory, which directories should be searched? in tc or vc ++, set include = path1; path2; indicates where to find the Compilation Program. h-type file. Of course, it does not only specify the path, but also has other functions, for example, set dircmd =/4 sets an environment variable to add/4 to your dir command as the default parameter, just like every command you add the/4 parameter, it is actually an environment variable set for the command Interpretation Program command, and it is set for the dir internal command.

DWORD GetEnvironmentVariable (LPCSTR lpName, LPSTR lpBuffer, DWORD dSize). The lpName parameter is the name of the environment variable you want to query, and lpBuffer returns the value of the environment variable you specified, dSize indicates how many bytes the lpBuffer function can store.

The reason for local failure analysis is probably because the default path in the environment variable is deleted. Once the default path is set, some DLL or EXE files are required for the current system to run the program, and the Active control will be searched in all the default paths. If the corresponding program is found in these directories, it will be automatically loaded. If the corresponding program is not found, an error message for the specified file will be reported.

 

Roles of Environment Variables
Solve the software sharing problem of two systems
Many friends will install dual systems on their computers, such as Windows 98 on drive C and Windows XP on drive D. However, some software is often installed only on Windows 98, but Windows XP cannot be used normally. The more troublesome but effective method is to install it again. When we understand the use of environment variables, we can solve the problem of software sharing in dual systems.
Solve System Operation Problems
Why does the software installed in Windows 98 fail to run in Windows XP (except for green software )? The reason is that when installing software, you often need to copy some files to the system directory, and when using another system, these files cannot be run due to the lack of such files. Therefore, we can solve this problem by setting environment variables.
Edit the environment variable settings in this section
In windows, you can set the system environment variables through my computer-> properties-> advanced. However, does the environment variables set here have corresponding items in the registry? The answer is yes. In. net, a class is provided to obtain the system environment variables and their values. Environment variables are classified into two types: User variables and system variables, which have corresponding items in the registry. Where the user variable is located: HKEY_CURRENT_USER \ Environment; the SYSTEM variable is located at \ HKEY_LOCAL_MACHINE \ SYSTEM \ ControlSet001 \ Control \ Session Manager \ Environment. In addition, you can also right-click my computer-advanced-environment variables-in the system variables, there is a path option-double-click to open-Add a semicolon in the English state based on the original variables-and then change the path name enter. (Do not delete the original system variables. Separate them with semicolons and add them)

 

% ALLUSERSPROFILE % returns all "user configuration files.

% APPDATA % partial return the location where the application stores data by default.

% CD % returns the current directory string.

% CMDCMDLINE % returns the exact command line used to start the current Cmd.exe.

% CMDEXTVERSION % The system returns the version number of the current "command handler extension.

% COMPUTERNAME % the name of the computer returned by the system.

% COMSPEC % The system returns the exact path of the executable program of the command line interpreter.

% DATE % The system returns the current DATE. Use the same format as the date/t command. Generated by Cmd.exe. For more information about the date command, see Date.

% ERRORLEVEL % The system returns the error code of the recently used command. A non-zero value is usually used to indicate an error.

% HOMEDRIVE % The system returns the local workstation drive letter that is connected to the user's home directory. Set Based on the main directory value. The user home directory is specified in "local users and groups.

% HOMEPATH % The system returns the complete path of the user's home directory. Set Based on the main directory value. The user home directory is specified in "local users and groups.

% HOMESHARE % The system returns the network path of the user's shared home directory. Set Based on the main directory value. The user home directory is specified in "local users and groups.

% LOGONSEVER % returns the name of the domain controller that verifies the current logon session.

% NUMBER_OF_PROCESSORS % Number of processors installed on the computer.

% OS % The system returns the name of the operating system. Windows 2000 displays the operating system as Windows_NT.

% PATH % The system specifies the search PATH of the executable file.

% PATHEXT % The system returns a list of file extensions that the operating system deems executable.

% PROCESSOR_ARCHITECTURE % The system returns the processor's chip architecture. Value: x86 and IA64.

% PROCESSOR_IDENTFIER % The system returns the processor description.

% PROCESSOR_LEVEL % The system returns the model of the processor installed on the computer.

% PROCESSOR_REVISION % The system returns the system variable of the processor revision number.

% PROMPT % returns the command PROMPT settings of the current interpreter. Generated by Cmd.exe.

% RANDOM % The system returns any decimal number between 0 and 32767. Generated by Cmd.exe. % SYSTEMDRIVE % The system returns a drive containing the Windows XP root directory (that is, the system root directory.

% SYSTEMROOT % The system returns the location of the Windows XP root directory.

% TEMP % and % TMP % the system and user return the default temporary directory used by the applications available to the current login user. Some applications require TEMP, while other applications require TMP.

% TIME % The system returns the current TIME. Use the same format as the time/t command. Generated by Cmd.exe. For more information about the time command, see Time.

% USERDOMAIN % partial return the name of the domain containing the user account.

% USERNAME % returns the name of the user currently logged on.

% UserProfile % returns the location of the configuration file of the current user.

% WINDIR % the location of the operating system directory returned by the system.

 

How to edit environment variables in the command line
View all currently available environment variables (= system variables + User variables)

Set to view an environment variable, such as PATH set PATH

Add environment variables. For example, xxx = aa set xxx = aa sets the environment variable value (for example, xxx) to null. set xxx =

Add a new value (such as d: \ xxx) after an environment variable (such as PATH) set PATH = % PATH %; d: \ xxx

(Note: operations on environment variables in the dos window using command lines are only valid for applications in the current window)

 

 

 

/* Header file */<br/> # include <windows. h> <br/> # include <stdio. h> <br/> # include "env. h "<br/>/* pre-defined */<br/> # define BUFSIZE 4096 <br/> /*************** * ******************** <br/> * dword winapi EnumEnvironmentVariables () <br/> * function to display all environment variables of a process <br/> ************************ * *************/<br/> dword winapi EnumEnvironmentVariables () <br/>{< br/> // obtain the environment variable <br/> PVOID pEv = GetEnvironmentStrings (); <br/> LPSTR s ZEnvs; <br/> // display <br/> for (szEnvs = (LPSTR) pEv; * szEnvs ;) <br/>{< br/> printf ("% s \ n", szEnvs); <br/> while (* szEnvs ++ ); <br/>}< br/> // release <br/> FreeEnvironmentStrings (pEv); <br/> return 0; <br/>}< br/> /****************************** * ****** <br/> * dword winapi ChangeEnviromentVariables (LPSTR szName, <br/> LPSTR szNewValue, <br/> DWORD dwFlag) <br/> * function to change environment variables <br/> * environment in which the LPSTR szName parameter needs to be changed <br/> * LPS TR szNewValue new variable value <br/> * DWORD dwFlag appending, resetting, or resetting, related constant in env. h. In the definition <br/> ********************************* *****/<br/> dword winapi ChangeEnviromentVariables (LPSTR szName, <br/> LPSTR szNewValue, <br/> DWORD dwFlag) <br/>{< br/> DWORD dwErr; <br/> PVOID szVal; <br/> DWORD dwReturn; <br/> DWORD dwNewValSize; <br/> // if the flag is appended, obtain it first, and then append szNewValue to the end. <br/> if (dwFlag = VARIABLES_APPEND) <br/>{< br/> dwNewValSize = Lstrlen (szNewValue) + 1; // size of the new variable value <br/> // memory allocation <br/> szVal = HeapAlloc (GetProcessHeap (), 0, BUFSIZE + dwNewValSize ); <br/> // obtain the value <br/> dwReturn = GetEnvironmentVariable (szName, szVal, BUFSIZE); <br/> if (dwReturn = 0) // error <br/>{< br/> dwErr = GetLastError (); <br/> if (ERROR_ENVVAR_NOT_FOUND = dwErr) <br/> {<br/> printf ("Environment variable % s does not exist. \ n ", szName); <br/>}< br/> else <br/>{< br/> printf ("Error: % d", dwErr); <br/>}< br/> return FALSE; <br/>}< br/> else if (BUFSIZE <dwReturn) // The buffer size is too small <br/>{< br/> szVal = (LPTSTR) HeapReAlloc (GetProcessHeap (), 0, szVal, dwReturn + dwNewValSize ); <br/> if (NULL = szVal) <br/>{< br/> printf ("Memory error \ n"); <br/> return FALSE; <br/>}< br/> dwReturn = GetEnvironmentVariable (szName, szVal, dwReturn); <br/> if (! DwReturn) <br/>{< br/> printf ("GetEnvironmentVariable failed (% d) \ n", GetLastError (); <br/> return FALSE; <br/>}< br/> lstrcat (szVal, ";"); // delimiter <br/> lstrcat (szVal, szNewValue ); // Add <br/> // set <br/> if (! SetEnvironmentVariable (szName, szVal) <br/>{< br/> printf ("Set Value Error % d", GetLastError ()); <br/>}< br/> // release memory <br/> HeapFree (GetProcessHeap (), 0, szVal); <br/> return TRUE; <br/>}< br/> // if it is a reset, set <br/> else if (dwFlag = VARIABLES_RESET) <br/>{< br/> if (! SetEnvironmentVariable (szName, szNewValue) <br/>{< br/> printf ("Set value error % d", GetLastError ()); <br/>}< br/> // reset, ignore szNewValue <br/> else if (dwFlag = VARIABLES_NULL) <br/>{< br/> if (! SetEnvironmentVariable (szName, NULL) <br/>{< br/> printf ("Set value error % d", GetLastError ()); <br/>}< br/> return TRUE; <br/>}

 

 

 

Related Article

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.