Mega Code Archive

 
Categories / Delphi / Strings
 

Get all Environment Strings

Title: Get all Environment Strings Question: 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: 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;