C++Introduction to C++Language FeatureLearn 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 is included in 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 drive, files or directories if they are 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 these 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?

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

close

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

33+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux and some other operating systems. Dr. Yilmaz Yoru was born in 1974, Eskisehir-Turkey. He graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. One year later he started to work in the same university as an assistant. He received his MSc and PhD degrees from the same department of the same university. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics and Artificial Intelligence. He also likes the graphical 2D & 3D design and high-end innovations.
Related posts
C++ComponentsDatabaseIntroduction to C++Language FeatureLearn C++

Get To Know The Powerful C++ Data Bindings In Windows Apps

C++Introduction to C++Language FeatureLearn C++

How to Create a New Windows FMX Static Library In C++

C++C++11C++14C++17Introduction to C++Learn C++

How To Use C++ front() And back() Methods Of Vectors

C++Generic ProgrammingIntroduction to C++Language FeatureLearn C++

5 Things You Need To Know About Optimization In C++ Builder

en_USEnglish