Site icon Learn C++

How To Get Windows Environment Variables In C++ Builder

How To Get Windows Environment Variables In C++ Builder

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.IOUtilsSystem.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:

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.

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,

[crayon-673fc2bcc9d68869172627/]

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,

[crayon-673fc2bcc9d70318482808/]

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,

[crayon-673fc2bcc9d71361330458/]

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,

[crayon-673fc2bcc9d72362992522/]

What are the standard Windows Environment variables?

The following table defines the standard environment variables for Microsoft Windows.

Environment variable nameEnvironment value
ALLUSERSPROFILEGeneric user profile.
APPDATAPath of the application data folder.
CLIENTNAMEName of Client machine.
COMMONPROGRAMFILESPath of common program files folder.
COMPUTERNAMEName of Computer code is running on.
COMSPECPath of the cmd.exe program.
HOMEDRIVECurrent home drive designation, such as ‘C:’
HOMEPATHPath to current location for document storage.
LOGONSERVERSpecifies a domain controller for user logon authentication.
NUMBER_OF_PROCESSORSNumber of processors on current machine.
OSBase name of the Operating System. Note that Windows XP is given as Windows_NT.
PATHThe current program path.
PATHEXTExtension types of executable files.
PROCESSOR_ARCHITECTUREType of CPU architecture. For example, X86 for Intel Pentium processors.
PROCESSOR_IDENTIFIERID number of current machine.
PROCESSOR_LEVELMore detailed description of the CPU architecture.
PROCESSOR_REVISIONProcessor revision level.
PROGRAMFILESPath of the program files folder.
SESSIONNAMEName of the current OS session.
SYSTEMDRIVEDrive the OS operates from.
SYSTEMROOTSets the system directory.
TEMPPath of the temporary files folder.
TMPDirectory to store temporary files to.
USERDOMAINSpecifies the domain of the current machine.
USERNAMEName of the current user.
USERPROFILEPath of the folder holding the current user’s information.
WINDIRPath 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.

Exit mobile version