menu
  Home  ==>  papers  ==>  colibri_helpers  ==>  windows_environment   

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(lpApplicationNamePChar
  lpCommandLinePChar;
  lpProcessAttributeslpThreadAttributesPSecurityAttributes;
  bInheritHandlesBOOL
  dwCreationFlagsDWORD
  lpEnvironmentPointer;
  lpCurrentDirectoryPChar
  const lpStartupInfoTStartupInfo;
  var lpProcessInformationTProcessInformation): BOOLstdcall;

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_keyp_valueString): Boolean;
function f_get_environment_value(p_keypChar): String;
function f_get_all_environment_stringString;

type c_environmentClass
                      m_sizeInteger;
                      m_oa_environmentarray of char;
                      constructor create_environment(p_c_stringstStrings);
                    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(SenderTObject);
  begin
    display(f_get_all_environment_string);
  end// display_enviromnent_Click

procedure TForm1.read_environment_key_Click(SenderTObject);
  begin
    display(f_get_environment_value(pChar(edit1.Text)));
  end// read_environment_key_Click

procedure TForm1.build_environment_block_Click(SenderTObject);
  begin
    with c_environment.create_environment(Memo2.Linesdo
    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:

test_environment.dpr



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
    Name :
    E-mail :
    Comments * :
     

  • 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.
Created: nov-04. Last updated: dec-2019 - 103 articles, 239 .ZIP sources, 1292 figures
Contact : Felix COLIBRI - Phone: (33)1.42.83.69.36 / 06.87.88.23.91 - email:fcolibri@felix-colibri.com
Copyright © Felix J. Colibri   http://www.felix-colibri.com 2004 - 2019. All rigths reserved
Back:    Home  Papers  Training  Delphi developments  Links  Download
the Pascal Institute

Felix J COLIBRI

+ Home
  + articles_with_sources
    + database
    + web_internet_sockets
    + rest_services
    + oop_components
    + uml_design_patterns
    + debug_and_test
    + graphic
    + controls
    + colibri_utilities
    + colibri_helpers
      – windows_environment
      – stdin_stdout
    + delphi
    + IDE
    + firemonkey
    + compilers
    + vcl
  + delphi_training
  + delphi_developments
  + sweet_home
  – download_zip_sources
  + links
Contacts
Site Map
– search :

RSS feed  
Blog