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,
1 2 3 |
UnicodeString __fastcall GetEnvironmentVariable(const System::UnicodeString Name); // overload |
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,
1 2 3 |
String value = GetEnvironmentVariable( L"WINDIR" ); |
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,
1 2 3 4 5 6 7 |
void get_envinfo(String env_name) { String value = GetEnvironmentVariable( env_name ); Form1->Memo1->Lines->Add( env_name + ": "+ value ); } |
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,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
#include <vcl.h> #pragma hdrstop #include "Environment_Variables_Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void get_envinfo(String env_name) { String value = GetEnvironmentVariable( env_name ); Form1->Memo1->Lines->Add( env_name + ": "+ value ); } //--------------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject *Sender) { get_envinfo( L"ALLUSERSPROFILE" ); // Generic user profile. get_envinfo( L"APPDATA" ); // Path of the application data folder. get_envinfo( L"CLIENTNAME" ); // Name of Client machine. get_envinfo( L"COMMONPROGRAMFILES" ); // Path of common program files folder. get_envinfo( L"COMPUTERNAME" ); // Name of Computer code is running on. get_envinfo( L"COMSPEC" ); // Path of the cmd.exe program. get_envinfo( L"HOMEDRIVE" ); // Current home drive designation, such as 'C:' get_envinfo( L"HOMEPATH" ); // Path to current location for document storage. get_envinfo( L"LOGONSERVER" ); // Specifies a domain controller for user logon authentication. get_envinfo( L"NUMBER_OF_PROCESSORS" ); // Number of processors on current machine. get_envinfo( L"OS" ); // Base name of the Operating System. Note that Windows XP is given as Windows_NT. get_envinfo( L"PATH" ); // The current program path. get_envinfo( L"PATHEXT" ); // Extension types of executable files. get_envinfo( L"PROCESSOR_ARCHITECTURE" ); // Type of CPU architecture. For example, X86 for Intel Pentium processors. get_envinfo( L"PROCESSOR_IDENTIFIER" ); // ID number of current machine. get_envinfo( L"PROCESSOR_LEVEL" ); // More detailed description of the CPU architecture. get_envinfo( L"PROCESSOR_REVISION" ); // Processor revision level. get_envinfo( L"PROGRAMFILES" ); // Path of the program files folder. get_envinfo( L"SESSIONNAME" ); // Name of the current OS session. get_envinfo( L"SYSTEMDRIVE" ); // Drive the OS operates from. get_envinfo( L"SYSTEMROOT" ); // Sets the system directory. get_envinfo( L"TEMP" ); // Path of the temporary files folder. get_envinfo( L"TMP" ); // Directory to store temporary files to. get_envinfo( L"USERDOMAIN" ); // Specifies the domain of the current machine. get_envinfo( L"USERNAME" ); // Name of the current user. get_envinfo( L"USERPROFILE" ); // Path of the folder holding the current user's information. get_envinfo( L"WINDIR" ); // Path of the Windows folder. } |
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.
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition