Get all Environment Strings (Views: 29)
Problem/Question/Abstract: 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. procedure LoadEnvironmentStrings(Strings: TStrings); var AllStrings, CurrentString: PChar; begin AllStrings := GetEnvironmentStrings; try if AllStrings <> nil then begin CurrentString := AllStrings; while True do begin Strings.Add(StrPas(CurrentString)); Inc(CurrentString, Succ(StrLen(CurrentString))); if CurrentString[0] = #0 then Break; end; end; finally FreeEnvironmentStrings(AllStrings); end; end; Solve 2: GetEnvStringsList(TStringList(Memo1.Lines)); procedure GetEnvStringsList(EnvStr: TStringList); var PEnv, PCopyEnv: pchar; begin EnvStr.Clear; PEnv := GetEnvironmentStrings; PCopyEnv := PEnv; if PCopyEnv <> nil then repeat EnvStr.Add(StrPas(PCopyEnv)); inc(PCopyEnv, StrLen(PCopyEnv) + 1); until PCopyEnv^ = #0; FreeEnvironmentStrings(PEnv); PCopyEnv := nil; end; |