Johannes Fourie wrote:
function TGenFuncUtils.GetEnvironment(const EnvVar,default : string) :
string;
var
  EnvList, Value, Name : PChar;
  EnvName : string;
  SignPos : integer;
  found   : boolean;

begin
  EnvList := GetEnvironmentStrings;

This code neglects to call FreeEnvironmentStrings when it's finished with the list.


  found := false;

  while EnvList^ <> #0 do
  begin
    EnvName := StrPas(EnvList);

The StrPas function is never needed. You can assign EnvList directly to EnvName and get the same result.


    SignPos := pos('=',EnvName);
    EnvName := copy(Envname,1,SignPos-1);  // truncate Envname to remove
value
    if EnvName = EnvVar then
    begin
      Name := StrAlloc(255);   //allocate memory for name variable
      Value := StrAlloc(255);  // allocate memory for value variable

      StrPCopy(Name,EnvName);  // copy string value 'EnvName' to PChar
Name
      GetEnvironmentVariable(Name, Value, 255);
      found := true;
      Result := StrPas(Value);  //convert value to string and assign to
Result

      StrDispose(Name);
      StrDispose(Value);
      break;
    end;
    Inc(EnvList, StrLen(EnvList)+1);
  end;

  if not(found) then
  begin
    MessageDlg('Cannot Find Environment
Variable'+#10#13+#10#13+'Continuing with default
values',mtinformation,[mbOk],0);
    Result := default;
  end;
end;

The entire loop is pointless. The code can be written much more simply. The code below takes advantage of the return value of GetEnvironmentVariable, which indicates whether the requested variable exists. It also doesn't assume that the environment variable's value will fit in the rather arbitrary 255-byte limit of your code. Values may be as large as 32767 characters. (The code also fixes the order of the #13 and #10 characters in the error message; carriage return comes _before_ line feed.) The function will raise an EWin32Error exception in two circumstances:


1. If the API function fails while determining the required buffer size for any reason other than the variable name not existing

2. If the second call to the API function returns something other than what the first call said it should return (if the variable's value changed mid-call, for instance, which could happen in a multi-threaded program)

function GetEnvironment(const EnvVar, Default: string): string;
var
  Len: DWord;
begin
  Len := GetEnvironmentVariable(PChar(EnvVar), nil, Len);
  if Len = 0 then begin
    if GetLastError <> Error_EnvVar_Not_Found then RaiseLastOSError;
    MessageDlg('Cannot Find Environment Variable'#13#10#13#10
      + 'Continuing with default values', mtInformation, [mbOK], 0);
    Result := Default;
    exit;
  end;
  SetLength(Result, Pred(Len));
  Len := GetEnvironmentVariable(PChar(EnvVar), PChar(Result), Len);
  Win32Check(Len = Length(Result));
end;

You might even just want to call the GetEnvironmentVariable function from the SysUtils unit. If the requested variable doesn't exist, then the SysUtils function returns the empty string.

--
Rob

_______________________________________________
Delphi mailing list -> [email protected]
http://www.elists.org/mailman/listinfo/delphi

Reply via email to