Windows Environment Strings - Felix John COLIBRI. |
- abstract : reading and writing Windows environment variables
- key words : Environment, GetEnvironmentStrings, GetEnvironmentVariable, SetEnvironmentVariable
- software used : Windows XP, Delphi 6
- hardware used : Pentium 1.400Mhz, 256 M memory, 140 G hard disc
- scope : Delphi 1 to 8 for Windows
- level : Delphi developer
- plan :
1 - Introduction For a simple cgi server, we had to read and create environment strings. This simple unit shows how to get values from the environment, and create new environment blocks.
2 - The Environment block Each Windows Process uses its own environment block. When a child process is created, the parent can specify which environment block should be used. The
syntax of CreateProcess is (Windows.Pas):
function CreateProcess(lpApplicationName: PChar;
lpCommandLine: PChar;
lpProcessAttributes, lpThreadAttributes: PSecurityAttributes;
bInheritHandles: BOOL; dwCreationFlags: DWORD;
lpEnvironment: Pointer; lpCurrentDirectory: PChar;
const lpStartupInfo: TStartupInfo;
var lpProcessInformation: TProcessInformation): BOOL; stdcall;
| and lpEnvironment is a pointer to a block with the following syntax: - a zero terminated list of key / values
- each key / value is a zero terminated string with key=value strings
So to create an environment block for a new process, we simply build such a block, and hand a pointer to this block over to the CreateProcess function.
Within a Process, we can retrieve the complete block, fetch single values, or add values. The basic primitives are GetEnvironmentStrings, GetEnvironmentVariable and SetEnvironmentVariable.
3 - The Delphi unit Our Delphi unit simply offers the following INTERFACE:
function f_write_environment_key_value(p_key, p_value: String): Boolean;
function f_get_environment_value(p_key: pChar): String;
function f_get_all_environment_string: String;
type c_environment= Class
m_size: Integer;
m_oa_environment: array of char;
constructor create_environment(p_c_strings: tStrings);
end; // c_environment |
4 - The test project This is the simple test project, with: - a tButton to fetch and display all the environment strings
- a tButton to extract the value corresponding to the key written in
Edit1.Text
- a tButton using the c_environment CLASS in order to build in c_environment.m_oa_environment the null terminated block
The code of the tButton events is:
procedure TForm1.display_enviromnent_Click(Sender: TObject);
begin display(f_get_all_environment_string);
end; // display_enviromnent_Click
procedure TForm1.read_environment_key_Click(Sender: TObject);
begin
display(f_get_environment_value(pChar(edit1.Text)));
end; // read_environment_key_Click
procedure TForm1.build_environment_block_Click(Sender: TObject);
begin
with c_environment.create_environment(Memo2.Lines) do
begin
display(f_display_hex(@ m_oa_environment[0], m_size,
8, k_hex_default)); Free;
end; // with c_environment
end; // build_environment_block_Click |
Here is a snapshot of the project:
Note that - f_get_environment_value uses a pChar as parameter, since most of the time
we employ a literal string, which is automatically converted to a pChar by the Delphi compiler. Another version with a String parameter could be used as well
- the pointer handed over to the CreateProcess function is @
c_environment.m_oa_environment[0]. We used a Class since both the block and its size are required by the function call
5 - Download the Sources
Here are the source code files:
Those .ZIP files contain:
- the main program (.DPR, .DOF, .RES), the main form (.PAS, .DFM), and any other auxiliary form
- any .TXT for parameters
- all units (.PAS) for units
Those .ZIP
- are self-contained: you will not need any other product (unless expressly mentioned).
- can be used from any folder (the pathes are RELATIVE)
- will not modify your PC in any way beyond the path where you placed the .ZIP
(no registry changes, no path creation etc).
To use the .ZIP: - create or select any folder of your choice
- unzip the downloaded file
- using Delphi, compile and execute
To remove the .ZIP simply delete the folder.
As usual: - please tell us at fcolibri@felix-colibri.com if you found some errors, mistakes, bugs, broken
links or had some problem downloading the file. Resulting corrections will be helpful for other readers
- we welcome any comment, criticism, enhancement, other sources or reference
suggestion. Just send an e-mail to fcolibri@felix-colibri.com.
- or more simply, enter your (anonymous or with your e-mail if you want an
answer) comments below and clic the "send" button
- and if you liked this article, talk about this site to your fellow
developpers, add a link to your links page ou mention our articles in your blog or newsgroup posts when relevant. That's the way we operate: the more traffic and Google references we get, the more articles we will write.
6 - The Author Felix John COLIBRI works at the Pascal Institute. Starting with Pascal in 1979, he then became involved with Object
Oriented Programming, Delphi, Sql, Tcp/Ip, Html, UML. Currently, he is mainly active in the area of custom software
development (new projects, maintenance, audits, BDE migration, Delphi Xe_n migrations, refactoring), Delphi Consulting and Delph training. His web site features tutorials, technical papers about programming with full downloadable source
code, and the description and calendar of forthcoming Delphi, FireBird, Tcp/IP, Web Services, OOP / UML, Design Patterns, Unit Testing training sessions.
|