Mega Code Archive

 
Categories / Delphi / System
 

How to get DOS Environment Variables

Title: How to get DOS Environment Variables Question: How to get environment variables like PATH and PROMPT. Answer: This is a simple function that uses the GetEnvironmentVariable API function. GetEnvironmentVariable return values: - If the function succeeds, the return value is the number of characters stored into the buffer pointer, not including the terminating null character. - If the specified environment variable name was not found in the environment block for the current process, the return value is zero. - If the buffer pointer to is not large enough, the return value is the buffer size required to hold the value string and its terminating null character. function GetDOSEnvVar(const VarName: string): string; var i: integer; begin Result := ''; try i := GetEnvironmentVariable(PChar(VarName), nil, 0); if i 0 then begin SetLength(Result, i); GetEnvironmentVariable(Pchar(VarName), PChar(Result), i); end; except Result := ''; end; end;