Welcome to the new DelphiDabbler Code Library Documentation.

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.

EnumVars class method

Project: Environment Variables Unit

Unit: PJEnvVars

Class: TPJEnvironmentVars

Applies to: ~>3.0

class procedure EnumVars(Callback: TPJEnvVarsEnumEx; Data: Pointer);

Description

Enumerates all the environment variables in the current process.

Parameters:

The Callback method or (anonymous procedure) must be implemented by the caller.

Important: Environment variables should not be modified while EnumVars is executing. This is because the method takes a snap-shot of the environment variables before it starts calling Callback. Any environment variables added, deleted or modified while EnumVars is running will not be reflected in the enumeration and could cause obscure bugs.

Examples

Here are two implementation of methods that add the names and values of all environment variables to a user-provided string list.

The first implementation works with all compilers and uses a callback method. The methods we create could be part of any class, but here we assume they are part of a form class named TForm1. Implement the following methods:

procedure TForm1.EnvVarCallback(const EnvVar: TPJEnvironmentVar; Data: Pointer):
begin
  TStrings(Data).Add(
    Format('Name="%s", Value="%s"', [EnvVar.Name, EnvVar.Value])
  );
end;

procedure TForm1.EnvVarsToStrings(const Strings: TStrings);
begin
  TPJEnvironmentVars.EnumVars(EnvVarCallback, Pointer(Strings));
end;

Here our method that adds the environment variables to a given string list is called EnvVarsToStrings and the callback method is EnvVarCallback.

In EnvVarsToStrings we call EnumVars and pass a reference to the callback to it. We also pass a pointer to the string list via the Data parameter.

Now EnumVars calls EnvVarsToStrings once for each environment variable, passing the environment variable’s details as a TPJEnvironmentVar value in the EnvVar parameter and the string list pointer in the Data parameter. EnvVarsToStrings casts the Data pointer back to a TStrings object and adds the environment variable’s name and value to it.

The second example requires Delphi 2009 or later. We again implement an EnvVarsToStrings method of TForm1, but this time we dispense with the callback method and use use an anonymous procedure instead. Here’s the method:

procedure TForm1.EnvVarsToStrings(const Strings: TStrings);
begin
  TPJEnvironmentVars.EnumVars(
    procedure (const EnvVar: TPJEnvironmentVar; Data: Pointer)
    begin
      SL.Add(
        Format('Name="%s", Value="%s"', [EnvVar.Name, EnvVar.Value])
      );
    end,
    nil
  );
end;

Because we are using an anonymous procedure that is in the same scope as EnvVarsToStrings’s string list parameter we can access the string list object directly. This means there is no need to pass a reference to the string list to the callback via EnumVar’s Data parameter, so we just pass nil.