C++ Builder has a lot of specific methods in its SysUtils library that are included in the VCL and FMX libraries. Some of these are grouped as Path Manipulation Routines which allow users to edit, extract, get and set drive name, directory name, file name, file extensions. These methods are combined in Vcl.FileCtrl, System.IOUtils, System.SysUtils libraries. These all methods are easy to use and easy to get or set file path strings in that operating system. These can be used with other component properties like FileName property of OpenDialog, SaveDialog components. We can also check drives, files, or directories to see if they exist or not in that given path. And we can get Windows Environment Variables like temp directory, Windows directory, and Computer Name.
In this post, we explain how we can get Windows Environment Variables in C++ Builder. Thus, we answer your questions below:
- How can I get a Get Environment Variable of the System?
- Which Environment Variables can I retrieve?
- How we can use GetEnvironmentVariable Method?
- How can I easily find the location of Temp Directory?
- How can I easily get the location of Installed Windows Directory?
- How can I get Computer Name in Windows?
- How can I get Processor (CPU) specifications?
- How can I get User Name?
- How can I get User Domain?
- How can I get Session Name?
- How can I get information about the ALLUSERSPROFILE environment variable?
- How can I get information about the APPDATA environment variable?
- How can I get information about the CLIENTNAME environment variable?
- How can I get information about the COMMONPROGRAMFILES environment variable?
- How can I get information about the COMPUTERNAME environment variable?
- How can I get information about the COMSPEC environment variable?
- How can I get information about the HOMEDRIVE environment variable?
- How can I get information about the HOMEPATH environment variable?
- How can I get information about the LOGONSERVER environment variable?
- How can I get information about the NUMBER_OF_PROCESSORS environment variable?
- How can I get information about the OS environment variable?
- How can I get information about the PATH environment variable?
- How can I get information about the PATHEXT environment variable?
- How can I get information about the PROCESSOR_ARCHITECTURE environment variable?
- How can I get information about the PROCESSOR_IDENTIFIER environment variable?
- How can I get information about the PROCESSOR_LEVEL environment variable?
- How can I get information about the PROCESSOR_REVISION environment variable?
- How can I get information about the PROGRAMFILES environment variable?
- How can I get information about the SESSIONNAME environment variable?
- How can I get information about the SYSTEMDRIVE environment variable?
- How can I get information about the SYSTEMROOT environment variable?
- How can I get information about the TEMP environment variable?
- How can I get information about the TMP environment variable?
- How can I get information about the USERDOMAIN environment variable?
- How can I get information about the USERNAME environment variable?
- How can I get information about the USERPROFILE environment variable?
- How can I get information about the WINDIR environment variable?
By learning how to get Windows Environment Variables in C++ Builder as well as how to compile C++ in Windows. It will help you to easily build C++ applications.
Table of Contents
What does the GetTempPath method do?
GetEnvironmentVariable Method (System:SysUtils:GetEnvironmentVariable) is a Path Manipulation Routine that returns the path to a directory to store temporary files. This directory is a system-managed location; files saved here may be deleted between application sessions or system restarts.
In another term GetEnvironmentVariable Method retrieves an environment variable value. we can call the GetEnvironmentVariable Method to retrieve the value of an environment variable, passed as Name, for the current process.
Note: The case of the specified environment variable Name is ignored.
What is the syntax of the GetEnvironmentVariable method in modern C++?
Here is the Syntax of the GetEnvironmentVariable() Method,
Is there a simple example of how to use the GetEnvironmentVariable method in modern C++ ?
The GetEnvironmentVariable() Method returns a UnicodeString, We can get Temp Path of the system from a given path string as below,
Now we have Installed Window Directory, we can use this path value in other Path Operations.
Is there a full example of how to use the GetEnvironmentVariable method in modern C++?
We can create a specific get_envinfo() function to get an environment variable and print out its value. Here is a simple example for this,
Now we can use our get_envinfo() function to get information about every environment variable and we can list them in a Memo (TMemo) box. Here is the full C++ Builder VCL example with the Memo (TMemo) component below,
What are the standard Windows Environment variables?
The following table defines the standard environment variables for Microsoft Windows.
Environment variable name | Environment value |
---|---|
ALLUSERSPROFILE | Generic user profile. |
APPDATA | Path of the application data folder. |
CLIENTNAME | Name of Client machine. |
COMMONPROGRAMFILES | Path of common program files folder. |
COMPUTERNAME | Name of Computer code is running on. |
COMSPEC | Path of the cmd.exe program. |
HOMEDRIVE | Current home drive designation, such as ‘C:’ |
HOMEPATH | Path to current location for document storage. |
LOGONSERVER | Specifies a domain controller for user logon authentication. |
NUMBER_OF_PROCESSORS | Number of processors on current machine. |
OS | Base name of the Operating System. Note that Windows XP is given as Windows_NT. |
PATH | The current program path. |
PATHEXT | Extension types of executable files. |
PROCESSOR_ARCHITECTURE | Type of CPU architecture. For example, X86 for Intel Pentium processors. |
PROCESSOR_IDENTIFIER | ID number of current machine. |
PROCESSOR_LEVEL | More detailed description of the CPU architecture. |
PROCESSOR_REVISION | Processor revision level. |
PROGRAMFILES | Path of the program files folder. |
SESSIONNAME | Name of the current OS session. |
SYSTEMDRIVE | Drive the OS operates from. |
SYSTEMROOT | Sets the system directory. |
TEMP | Path of the temporary files folder. |
TMP | Directory to store temporary files to. |
USERDOMAIN | Specifies the domain of the current machine. |
USERNAME | Name of the current user. |
USERPROFILE | Path of the folder holding the current user’s information. |
WINDIR | Path of the Windows folder. |
What does the output of our example Windows Environment Variable list program look like?
Here is the output of our VCL Project,
In addition, we can get Home Folder Path by using GetHomePath Method. We can also get Temp Folder Path by using the GetTempPath Method. These methods will be explained in other posts.
If you are looking to Paths and Directories of C++ Builder IDE Options, they are listed here. C++ Builder Compiler Directories and Conditionals are listed here Environment variables are listed here
C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.
There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.