DelphiDabbler Code Snippets Database

Snippet Selection

The following snippets from the Code Snippets Database have been requested.

DeleteEnvVar

Deletes environment variable VarName. Returns 0 on success or the relevant Windows error code on failure.

function DeleteEnvVar(const VarName: string): Integer;
begin
  if Windows.SetEnvironmentVariable(PChar(VarName), nil) then
    Result := 0
  else
    Result := Windows.GetLastError;
end;

GetAllEnvVars

Stores all the environment variables in the current process' environment block in the given string list in Name=Value format.

procedure GetAllEnvVars(const Vars: Classes.TStrings);
var
  PEnvVars: PChar;    // pointer to start of environment block
begin
  Assert(Assigned(Vars));
  Vars.Clear;
  // Get reference to environment block for this process
  PEnvVars := Windows.GetEnvironmentStrings;
  if PEnvVars = nil then
    Exit;
  try
    MultiSzToStrings(PEnvVars, Vars);
  finally
    Windows.FreeEnvironmentStrings(PEnvVars);
  end;
end;

GetEnvironmentBlockSize

Returns the size of the current process' environment block, in characters.

function GetEnvironmentBlockSize: Cardinal;
var
  PEnvVars: PChar;    // pointer to start of environment block
  PEnvEntry: PChar;   // pointer to an environment string in block
begin
  // Get reference to environment block for this process
  PEnvVars := Windows.GetEnvironmentStrings;
  if PEnvVars <> nil then
  begin
    PEnvEntry := PEnvVars;
    try
      while PEnvEntry^ <> #0 do
        Inc(PEnvEntry, SysUtils.StrLen(PEnvEntry) + 1);
      Result := (PEnvEntry - PEnvVars) + 1;
    finally
      Windows.FreeEnvironmentStrings(PEnvVars);
    end;
  end
  else
    Result := 0;
end;

GetEnvVar

Returns the value of the given environment variable. If the environment variable does not exist then the empty string is returned.

function GetEnvVar(const VarName: string): string;
var
  BufSize: Integer;  // buffer size required for value
begin
  // Get required buffer size
  BufSize := Windows.GetEnvironmentVariable(PChar(VarName), nil, 0);
  if BufSize > 0 then
  begin
    // Environment variable found: read it
    SetLength(Result, BufSize - 1);
    Windows.GetEnvironmentVariable(PChar(VarName), PChar(Result), BufSize);
  end
  else
    // No such environment variable
    Result := '';
end;

RefreshEnvironment

Notifies all windows that the system environment variables have been changed.

procedure RefreshEnvironment(const Timeout: Cardinal = 5000);
var
  {$IFDEF CONDITIONALEXPRESSIONS}
  {$IF CompilerVersion >= 23.0} // Delphi XE2
  MessageRes: Windows.DWORD_PTR;
  {$ELSE}
  MessageRes: Windows.DWORD;
  {$IFEND}
  {$ELSE}
  MessageRes: Windows.DWORD;
  {$ENDIF}
begin
  Windows.SendMessageTimeout(
    Windows.HWND_BROADCAST,
    Messages.WM_SETTINGCHANGE,
    0,
    LPARAM(PChar('Environment')),
    Windows.SMTO_ABORTIFHUNG, Timeout,
    {$IFDEF CONDITIONALEXPRESSIONS}
    {$IF CompilerVersion >= 23.0} // Delphi XE2
    @MessageRes
    {$ELSE}
    MessageRes
    {$IFEND}
    {$ELSE}
    MessageRes
    {$ENDIF}
  );
end;

SetEnvVar

Sets the value of environment variable VarName to the value VarValue. Returns 0 on success or the relevant Windows error code on failure.

function SetEnvVar(const VarName, VarValue: string): Integer;
begin
  if Windows.SetEnvironmentVariable(PChar(VarName), PChar(VarValue)) then
    Result := 0
  else
    Result := GetLastError;
end;

SetEnvVarValue

Sets the value of environment variable VarName to the value VarValue. Returns the empty string on success or the relevant Windows error message on failure.

function SetEnvVarValue(const VarName, VarValue: string): string;
begin
  if Windows.SetEnvironmentVariable(PChar(VarName), PChar(VarValue)) then
    Result := ''
  else
    Result := GetErrorMessageWithId(GetLastError);
end;

View the whole database.

Go to the DelphiDabbler website.