DelphiFAQ Home Search:

Read environment variables

 

commentsThis article has not been rated yet. After reading, feel free to leave comments and rate it.

Windows provides two function to access the operating system's environment strings.
The procedure below reads the environment into a TStringList which you need to pass as a parameter.
An example for a call populating a TMemo field:

GetEnvStringsList(TStringList(Memo1.Lines));

The two relevant API calls are GetEnvironmentStrings and FreeEnvironmentStrings.

procedure GetEnvironmentStringsList(EnvironmentStrings: TStringList);
var
  PEnv,
  PCopyEnv: PChar;
begin { GetEnvironmentStringsList }
  EnvironmentStrings.Clear;
  PEnv := GetEnvironmentStrings;
  PCopyEnv := PEnv;
  if PCopyEnv<>nil then
    repeat
      EnvironmentStrings.Add(StrPas(PCopyEnv));
      Inc(PCopyEnv, StrLen(PCopyEnv) + 1);
    until PCopyEnv^=#0;
  FreeEnvironmentStrings(PEnv);
  PCopyEnv := nil
end; { GetEnvironmentStringsList }
You don't like the formatting? Check out SourceCoder then!

Comments:

 

 

NEW: Optional: Register   Login
Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option, or post under a registered account.
 

Show city and country
Show country only
Hide my location
You can mark text as 'quoted' by putting [quote] .. [/quote] around it.
Please type in the code:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity.
The owner of this web site reserves the right to delete such material.

photo Add a picture: