C++Builder How To Get Windows Environment Variables In C++ Builder

FireWind

Свой
Регистрация
2 Дек 2005
Сообщения
1,957
Реакции
1,203
Credits
4,034
How To Get Windows Environment Variables In C++ Builder
By Yilmaz Yoru October 1, 2021

C++ Builder has a lot of specific methods in its Для просмотра ссылки Войди или Зарегистрируйся library that is included in VCL and FMX libraries. Some of these are grouped as Для просмотра ссылки Войди или Зарегистрируйся which allow users to edit, extract, get and set drive name, directory name, file name, file extensions. These methods are combined in Для просмотра ссылки Войди или Зарегистрируйся, Для просмотра ссылки Войди или Зарегистрируйся, Для просмотра ссылки Войди или Зарегистрируйся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 Для просмотра ссылки Войди или Зарегистрируйся 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,
C++:
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,
C++:
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,
C++:
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,
C++:
#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 Для просмотра ссылки Войди или Зарегистрируйся 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,
1633076757502.png
In addition, we can get Home Folder Path by using Для просмотра ссылки Войди или Зарегистрируйся Method. We can also get Temp Folder Path by using the Для просмотра ссылки Войди или Зарегистрируйся 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 Для просмотра ссылки Войди или Зарегистрируйся. C++ Builder Compiler Directories and Conditionals are listed Для просмотра ссылки Войди или Зарегистрируйся Environment variables are listed Для просмотра ссылки Войди или Зарегистрируйся