This is a new site that's currently running on alpha code. There are going to be bugs. If you discover any, please report them on the site's issues page (GitHub account required). Thanks.
Warning: Many URLs are going to change. Refer to the README file to discover which library project's documentation has been completed.
Project: Environment Variables Unit
Unit: PJEnvVars
Applies to: ~>3.0
⚠️ All the routines described here are deprecated. Each routine has analogue in the TPJEnvironmentVars static class that should be used in preference.
PJEnvVars contains several functions and procedures to assist in interogating and manipulating environment variables. They are:
⚠️ Deprecated – use TPJEnvironmentVars.CreateBlock instead.
function CreateEnvBlock(const NewEnv: TStrings;
const IncludeCurrent: Boolean; const Buffer: Pointer;
const BufSize: Integer): Integer;
Creates a new environment block that can be used to pass to another process.
Pass a string list in NewEnv containing any environment variables that are to be included in the new block. Set IncludeCurrent to True
to include the current environment in the new block. Buffer should point to a block of memory of sufficient size to contain the new block and BufSize should be the size of this buffer.
The routine returns the buffer size in characters. If the supplied buffer is too small or is nil
then no data is created and the return value is the required buffer size.
Normal usage is to call CreateEnvBlock with Buffer set to nil
to get the required buffer size, then to create the memory block of that size and finally to call CreateEnvBlock again with Buffer pointing to the newly created memory block and BufSize set to the size returned from the previous call to CreateEnvBlock.
⚠️ Deprecated – use TPJEnvironmentVars.Delete instead.
function DeleteEnvVar(const VarName: string): Integer;
Deletes the environment variable with the given name.
Returns a Windows error code on failure or 0
on success.
⚠️ Deprecated – use TPJEnvironmentVars.BlockSize instead.
function EnvBlockSize: Integer;
This function returns the size of the current environment block in characters.
Important. Because the size of the environment block is returned in characters, not bytes, you must multiply the returned value by
SizeOf(Char)
to get the size in bytes.
⚠️ Deprecated – use TPJEnvironmentVars.Expand instead.
function ExpandEnvVars(const Str: string): string;
Replaces any environment variables embedded in the given string with their values and returns the modified string.
⚠️ Deprecated – use TPJEnvironmentVars.GetAllNames instead.
procedure GetAllEnvVarNames(const Names: TStrings); overload;
function GetAllEnvVarNames: TStringDynArray; overload;
Both overloaded versions of this routine fetch a list of the current environment variable names. Any blank names are excluded from the list.
The first version of the routine stores the environment variable names in the given string list. Any existing contents of the string list are discarded.
The second version creates and returns a dynamic string array containing the environment variable names.
⚠️ Deprecated – use TPJEnvironmentVars.GetAll instead.
function GetAllEnvVars(const Vars: TStrings): Integer;
Copies all the environment variables available to the current process in to the given string list. Each item placed in the string list represents one environment variable in the form NAME=VALUE
. Any previous contents of the string list are lost.
The size of the environment block, in characters, is returned.
⚠️ Deprecated – use TPJEnvironmentVars.GetValue instead.
function GetEnvVarValue(const VarName: string): string;
Returns the value of environment variable with the given name.
⚠️ Deprecated – use TPJEnvironmentVars.SetValue instead.
function SetEnvVarValue(const VarName, VarValue: string): Integer;
Sets the value of a given environment variable.
Returns a Windows error code on failure or 0
on success.