home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 October / Chip_2001-10_cd1.bin / zkuste / delphi / kolekce / d56 / TDSOFT.ZIP / TDEnvironmentStrings.pas < prev    next >
Pascal/Delphi Source File  |  2001-08-09  |  2KB  |  89 lines

  1. unit TDEnvironmentStrings;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;
  7.  
  8. type
  9.   TTDEnvironmentStrings = class(TComponent)
  10.   private
  11.     { Private declarations }
  12.     FEnvList: TStringList;
  13.   protected
  14.     { Protected declarations }
  15.   public
  16.     { Public declarations }
  17.     function RefreshVariables: Boolean;
  18.     constructor Create(AOwner: TComponent); override;
  19.     destructor Destroy; override;
  20.     function GetVariable(const VarName: string): string;
  21.     procedure GetVariablesList(VarList: TStrings);
  22.   published
  23.     { Published declarations }
  24.   end;
  25.  
  26. procedure Register;
  27.  
  28. implementation
  29.  
  30. procedure Register;
  31. begin
  32.   RegisterComponents('TDSoft', [TTDEnvironmentStrings]);
  33. end;
  34.  
  35. { TTDEnvironmentStrings }
  36.  
  37. constructor TTDEnvironmentStrings.Create(AOwner: TComponent);
  38. begin
  39.   inherited;
  40.     FEnvList:=TStringList.Create;
  41.   if not RefreshVariables then
  42.       raise Exception.Create('Cannot retrieve Environment Strings!');
  43. end;
  44.  
  45. destructor TTDEnvironmentStrings.Destroy;
  46. begin
  47.   inherited;
  48.   {$IFDEF VER130}
  49.       FreeAndNil(FEnvList);
  50.   {$ELSE}
  51.         FEnvList.Free;
  52.     FEnvList:=nil;
  53.   {$ENDIF}
  54. end;
  55.  
  56. function TTDEnvironmentStrings.GetVariable(const VarName: string): string;
  57. begin
  58.      Result:=FEnvList.Values[VarName];
  59. end;
  60.  
  61. procedure TTDEnvironmentStrings.GetVariablesList(VarList: TStrings);
  62. begin
  63.     VarList.Assign(FEnvList);
  64. end;
  65.  
  66. function TTDEnvironmentStrings.RefreshVariables: Boolean;
  67. var
  68.     X,P:PChar;
  69. begin
  70.     Result:=True;
  71.     try
  72.       X:=GetEnvironmentStrings;
  73.          try      
  74.       P:=X;
  75.       while P^<>#0 do
  76.       begin
  77.         FEnvList.Add(StrPas(P));
  78.         P:=P + Length(StrPas(P))+1;
  79.       end;
  80.     finally
  81.         FreeEnvironmentStrings(X);
  82.     end;
  83.   except
  84.         Result:=False;
  85.   end;
  86. end;
  87.  
  88. end.
  89.