Mega Code Archive

 
Categories / Delphi / Examples
 

Read environment variables

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 }