Author: Jonas Bilinkevicius
Sometimes you want to show the user the current settings on his/her machine. One of
the vital information are the Environment Strings. The Windows API gives us an
efficient set of funcitons to access these
Answer:
Solve 1:
Actually, it is really easy to access the Windows Environment Strings. The Windows
API defines a function called "GetEnvironmentStrings" to return a double-null
terminated buffer filled with null terminated strings seperating all environment
variables.
The following procedure will takes a string list as parameter and fill it with all
environment variables returned. It will parse the buffer string by string, setting
a pointer behind every string returned in order to retrieve the next one.
I hope this will help you.
1 procedure LoadEnvironmentStrings(Strings: TStrings);
2 var3 AllStrings, CurrentString: PChar;
4 begin5 AllStrings := GetEnvironmentStrings;
6 try7 if AllStrings <> nilthen8 begin9 CurrentString := AllStrings;
10 while True do11 begin12 Strings.Add(StrPas(CurrentString));
13 Inc(CurrentString, Succ(StrLen(CurrentString)));
14 if CurrentString[0] = #0 then15 Break;
16 end;
17 end;
18 finally19 FreeEnvironmentStrings(AllStrings);
20 end;
21 end;
Solve 2:
22 GetEnvStringsList(TStringList(Memo1.Lines));
23 24 procedure GetEnvStringsList(EnvStr: TStringList);
25 var26 PEnv, PCopyEnv: pchar;
27 begin28 EnvStr.Clear;
29 PEnv := GetEnvironmentStrings;
30 PCopyEnv := PEnv;
31 if PCopyEnv <> nilthen32 repeat33 EnvStr.Add(StrPas(PCopyEnv));
34 inc(PCopyEnv, StrLen(PCopyEnv) + 1);
35 until PCopyEnv^ = #0;
36 FreeEnvironmentStrings(PEnv);
37 PCopyEnv := nil;
38 end;