whats new ¦  programming tips ¦  indy articles ¦  intraweb articles ¦  informations ¦  links ¦  interviews
 misc ¦  tutorials ¦  Add&Win Game

Tips (1541)

Database (90)
Files (137)
Forms (107)
Graphic (114)
IDE (21)
Indy (5)
Internet / LAN (130)
IntraWeb (0)
Math (76)
Misc (126)
Multimedia (45)
Objects/
ActiveX (51)

OpenTools API (3)
Printing (35)
Strings (83)
System (266)
VCL (242)

Top15

Tips sort by
component


Search Tip

Add new Tip

Add&Win Game

Advertising

30 Visitors Online


 
...retrieve Environmental Variables as strings by name?
Autor: Biopsy
[ Print tip ]  

Tip Rating (41):  
     


{ This code is very useful if you need to "translate" various
environmental variables. I use this a lot to get paths to TEMP
or windows folders on different systems.}

function GetEnvVarValue(const VarName: string): string;
var
  
BufSize: Integer;  // buffer size required for value
begin
  
// Get required buffer size (inc. terminal #0)
  
BufSize := GetEnvironmentVariable(PChar(VarName), nil, 0);
  if BufSize > 0 then
  begin
    
// Read env var value into result string
    
SetLength(Result, BufSize - 1);
    GetEnvironmentVariable(PChar(VarName),
      PChar(Result), BufSize);
  end
  else
    
// No such environment variable
    
Result := '';
end;

procedure TForm1.Button1(Sender: TObject);
begin
  
ShowMessage(GetEnvVarValue('SystemRoot'));
end;

{--- Here is the list of different variables you could use -----------
ALLUSERSPROFILE
APPDATA
CLIENTNAME
CommonProgramFiles
COMPUTERNAME
ComSpec
HOMEDRIVE
HOMEPATH
LOGONSERVER
NUMBER_OF_PROCESSORS
OS
Path
PATHEXT
PCToolsDir
PROCESSOR_ARCHITECTURE
PROCESSOR_IDENTIFIER
PROCESSOR_LEVEL
PROCESSOR_REVISION
ProgramFiles
SESSIONNAME
SystemDrive
SystemRoot
TEMP
TMP
USERDOMAIN
USERNAME
USERPROFILE
windir
---------------------------------------------------------------------}


 

Rate this tip:

poor
very good


Copyright © by SwissDelphiCenter.ch
All trademarks are the sole property of their respective owners