Skip over navigation

Welcome to the all new DelphiDabbler website.

I hope you like it.

There are going to be bugs. Please report any you find on the website's issues page (GitHub account required). Thanks.

Click the close button and you won't see this message again.

Environment Variables Unit

Code to help manage and access environment variables

Version 3.0.0
Released 30 Jan 2014
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

Environment Variables Unit is a sub-project of the DDabLib project which is maintained in a Subversion repository. The code is developed in the trunk/projects/envvars branch.

Subversion users can check out the source code of the current release from the repository on SourceForge by doing:

svn checkout http://svn.code.sf.net/p/ddablib/code/tags/projects/envvars/release-3.0.0 codedir

where codedir is the directory where you want to checkout code. To check out other versions, simply specify a different version number.

The latest development tree can be checked out using:

 svn checkout http://svn.code.sf.net/p/ddablib/code/trunk/projects/envvars codedir

Check outs are read only, but you can easily contribute code: see Feedback & Queries below.

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 on SourceForge

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 Issue Tracker.

Click the Create Ticket button to create a ticket then complete the form giving as much information as possible.

The issue tracker is shared among all the projects in the DelphiDabbler Code Library, so please make sure you enter envvars in the Project edit box to make it clear which project you are referring to. You should also choose Defect in the Type drop down list to report a bug or Enhancement to request a new feature.

If you have created a bug fix or have implemented a new feature please attach a zip file to your ticket that contains your source code.

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.