Skip over navigation

Environment Variables Unit

Code to help manage and access environment variables

Version 3.0.0
Released 2014-01-30
Platforms Win32 & Win64
Framework VCL & FMX
Compatibilty Delphi 7 & later

Overview

This unit provides a static class for reading and modifying the environment variables available to the current process, along with a class that can be used to enumerate environment variable names.

The unit also contains a component and a set of routines that duplicate some of the features provided by the static class. These are now deprecated and are provided for backward compatibility reasons only: they should no longer be used in new code and may be removed from future releases.

The static class has methods to:

  • Read, write and delete environment variables.
  • Query whether an environment variable exists.
  • Get the number of environment variables in the environment block along with the size of the block.
  • List and enumerate the names, or names and values, of all environment variables in the environment block.
  • Create a new environment block suitable for passing to a child process.

There is also an enumerator class the provides an alternative way to enumerate environment variable names.

Two different demo programs are included in the Demos\1 and Demos\2 sub-directories. There are VCL and FireMonkey 2 versions of each demo.

The demo programs are documented in the file DemosReadMe.htm that can be found in the Demos directory.

This unit uses ideas from my article "How to access environment variables". The article shows how to develop routines similar to some of the methods in this unit and discusses their operation.

Get The Code

You can download the latest version of the Environment Variables Unit directly from SourceForge as dd-envvars-3.0.0.zip.

Download v3.0.0

The Environment Variables Unit source code is hosted in the ddablib/envvars GitHub repository. You can git clone or fork the repository as required.

Installation

The Environment Variables Unit is supplied in a zip file. Before installing you need to extract all the files from the zip file, preserving the directory structure. The following files will be extracted:

  • PJEnvVars.pas – contains all source code, including the deprecated component, but excluding the component registration code.
  • PJEnvVars.dcr – component palette glyph for the deprecated component. Deprecated
  • PJEnvVarsDsgn.pas – registration code for the deprecated component. Deprecated
  • ReadMe.htm – read-me file.
  • ChangeLog.txt – project change log.
  • MPL-2.txt – the Mozilla Public License v2.0.
  • Documentation.URL – short-cut to the unit's online documentation.

In addition to the above files you will find the source code for various demo projects in the Demos directory.

There are three possible ways to use the unit:

  1. The simplest way is the add a copy of PJEnvVas.pas to your projects whenever the unit is needed.
  2. To make the unit easier to re-use you can either copy it to a folder on your Delphi search path, or add the folder containing the unit to the Delphi Search path. You then simply use the unit as required without needing to add it to your project.
  3. For maximum portability you can add PJEnvVars.pas to a Delphi package.

None of the above methods will install the deprecated component into the Delphi IDE. To do this you need to add PJEnvVars.pas, PJEnvVars.dcr and PJEnvVarsDsgn.pas should be added to a design time package. If you need help doing this see here.

Documentation

The Environment Variables Unit is comprehensively documented online here

The project's change log can be viewed here.

A read-me file (ReadMe.htm) is included in the project download.

Feedback & Queries

If you find any bugs or want to suggest a new feature please report them using the Environment Variables Unit issue tracker.

If you have created a bug fix or have implemented a new feature please open a pull request for it.

Should you have any queries about using the unit please read the documentation. If you can't find an answer in the documentation then post a message in the discussion group.

License

The Environment Variables Unit is open source. It is copyright © 2001-2014 by Peter Johnson. The source code is made available under the terms of the Mozilla Public License v2.0. All relevant trademarks are acknowledged.