(************************************************)
(* Name: Enviroment                             *)
(* Last Revision: 22 Mar. 2001                  *)
(* Version: 2.3                                 *)
(*                                              *)
(* Now Detects NT correctly                     *)
(*                                              *)
(* Copyright © 2000 - 2001 by Christian Aaangel *)
(************************************************)

unit Enviroment;

interface

Uses
 Windows,
 Registry,
 SysUtils;

Function Enviroment_Version: String;
Function GetAppData : string;
Function GetCache : string;
Function GetCommonFilesDir : string;
Function GetConfigPath : string;
Function GetCookies : string;
Function GetDesktop : String;
Function GetDevicePath : string;
Function GetFavorites : string;
Function GetFonts : string;
Function GetHistory : string;
Function GetMediaPath : string;
Function GetNetHood : string;
Function GetOtherDevicePath : string;
Function GetPersonal : string;
Function GetPrintHood : string;
Function GetProductId : string;
Function GetProductKey : string;
Function GetProductName : string;
Function GetProgramFilesDir : string;
Function GetProgramFilesPath : string;
Function GetRecent : string;
Function GetRegisteredOrganization : string;
Function GetRegisteredOwner : string;
Function GetSendTo : string;
Function GetStartMenu : string;
Function GetStartMenuPrograms : string;
Function GetStartUp : string;
Function GetSystemRoot : string;
Function GetTemp: String;
Function GetTemplates : string;
Function GetWinVersion : string;
Function GetVersionNumber : string;
Function GetWallPaperDir : string;

// NT Extensions
Function GetCommonAdmTools: String;
Function GetCommonAppData: String;
Function GetCommonDesktop: String;
Function GetCommonDocuments: String;
Function GetCommonPrograms: String;
Function GetCommonStartMenu: String;
Function GetCommonStartup: String;
Function GetCommonTemplates: String;
Function GetLocalAppData: String;
Function GetLocalSettings: String;
Function GetMyPictures: String;

Var
 WinReg: TRegIniFile;

implementation

Function Enviroment_Version: String;
Begin
 Result := '3.0';
End;

Function PathToOk(APath: String): String;
Begin
 If APath[length(APath)] <> '\' Then APath := APath + '\';
 Result := APath;
End;

Procedure Init;
Begin
 WinReg := TReginifile.Create;
End;

Procedure Init2;
Begin
 WinReg.RootKey := HKEY_LOCAL_MACHINE;
End;

Function IsNT: Boolean;
Begin
 Result := Win32Platform = VER_PLATFORM_WIN32_NT;
End;

Function GetNTReg(X: Integer): String;
Var
 S1, S: String;
Begin
 Result := 'Query Out Of Range';
 S := 'Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders';
 Init; // Create WinReg
 Case X of
  1: Result := WinReg.ReadString(S, 'Desktop', ''); // Desktop
  2: Result := WinReg.ReadString(S, 'Templates', ''); // Templates
  3: Result := WinReg.ReadString(S, 'AppData', ''); // Application Data
  4: Result := WinReg.ReadString(S, 'Start Menu', ''); // Start menu
  5: Result := WinReg.ReadString(S, 'Programs', ''); // Start Menu ... Programs
  6: Result := WinReg.ReadString(S, 'Startup', ''); // Startmenu ... Startup
  7: Result := WinReg.ReadString(S, 'Fonts', ''); // Fonts
  8: Result := WinReg.ReadString(S, 'Sendto', ''); // Sendto folder
  9: Result := WinReg.ReadString(S, 'Recent', ''); // Recent
  10: Result := WinReg.ReadString(S, 'Favorites', ''); // Favorites
  11: Result := WinReg.ReadString(S, 'NetHood', ''); // Network hood
  12: Result := WinReg.ReadString(S, 'Personal', ''); // Documents
  13: Result := WinReg.ReadString(S, 'Cache', ''); // Temporary Internet Files
  14: Result := WinReg.ReadString(S, 'Cookies', ''); // Cookies
  15: Result := WinReg.ReadString(S, 'History', ''); // Internet History
  16: Result := WinReg.ReadString(S, 'PrintHood', ''); // Printers Network Hood
  41: Result := WinReg.ReadString(S, 'Local AppData', ''); // Local Application
  42: Result := WinReg.ReadString(S, 'Local Settings', ''); // Local Settings
  43: Result := WinReg.ReadString(S, 'My Pictures', ''); // My Picture folder
 End;
 Init2;
 S := 'Software\Microsoft\Windows\CurrentVersion';
 Case X of
  21: Result := WinReg.ReadString(S, 'ProgramFilesDir', ''); // Program Files
  22: Result := WinReg.ReadString(S, 'CommonFilesDir', ''); // Common files
  24: Result := WinReg.ReadString(S, 'MediaPath', ''); // Media Path
  31: Result := WinReg.ReadString(S, 'ProgramFilesDir', ''); // Program Files
 End;
 S := 'Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders';
 Case X of
  33: Result := WinReg.ReadString(S, 'Common Administrative Tools', ''); // Startmenu ... Administrative Tools
  34: Result := WinReg.ReadString(S, 'Common AppData', ''); // Application Data
  35: Result := WinReg.ReadString(S, 'Common Desktop', ''); // Desktop
  36: Result := WinReg.ReadString(S, 'Common Documents', ''); // Documents
  37: Result := WinReg.ReadString(S, 'Common Programs', ''); // Startmenu ... Programs
  38: Result := WinReg.ReadString(S, 'Common Start Menu', ''); // Startmenu
  39: Result := WinReg.ReadString(S, 'Common Startup', ''); // Startmenu ... Startup
  40: Result := WinReg.ReadString(S, 'Common Templates', ''); // Templates
 End;
 S := 'Software\Microsoft\Windows NT\CurrentVersion';
 Case X of
  17: Begin // Device path
       S1 := WinReg.ReadString(S, 'SystemRoot', '');
       If S1 > '' Then S1 := PathToOk(S1) + 'Inf';
       result := S1;
      End;
  18: Result := WinReg.ReadString(S, 'Registeredowner', ''); // Software Owner
  19: Result := WinReg.ReadString(S, 'RegisteredOrganization', ''); // Owner Organization
  20: Result := WinReg.ReadString(S, 'ProductID', ''); // WinNT ID
  23: Begin // Wallpaper dir
       S1 := WinReg.ReadString(S, 'SystemRoot', '');
       If S1 > '' Then S1 := PathToOk(S1) + 'Web\WallPaper';
       result := S1;
      End;
  26: Result := WinReg.ReadString(S, 'SystemRoot', ''); // System root
  27: Result := WinReg.ReadString(S, 'ProductName', ''); // Product name eg. Microsoft Windows 2000
  28: Result := WinReg.ReadString(S, 'CurrentVersion', ''); // NT Version
  29: Result := WinReg.ReadString(S, 'CurrentVersion', ''); // NT Version
  32: Begin // Device path
       S1 := WinReg.ReadString(S, 'SystemRoot', '');
       If S1 > '' Then S1 := PathToOk(S1) + 'Inf\Other';
       result := S1;
      End;
  44: Begin // Temp Dir
       S1 := WinReg.ReadString(S, 'SystemRoot', '');
       If S1 > '' Then S1 := PathToOk(S1) + 'Temp';
       result := S1;
      End;
 End;
 Case X of
  25: Result := 'Config Path Is Not Available In WinNT';
  30: Result := 'Product Key Is Not Available In WinNT';
 End;
End;

Function GetReg(Index: Integer): String;
Var
 S1: String;
Begin
 Result := '';
 // Detect NT OS
 (* No known Identification Code for this task *)
 Init;
 Init2;
 If IsNT Then Begin
//  showmessage('ISNT');
  Result := GetNTReg(Index);
 End
  Else
 Begin
  Init;
  case Index of
   1: Result := Winreg.Readstring('software\microsoft\windows\currentversion\Explorer\Shell Folders', 'Desktop', '');   {SKRIVEBORD}
   2: Result := Winreg.Readstring('software\microsoft\windows\currentversion\Explorer\Shell Folders', 'Templates', ''); {windows\shellnew}
   3: Result := Winreg.Readstring('software\microsoft\windows\currentversion\Explorer\Shell Folders', 'AppData', '');   {Windows\Application data}
   4: Result := Winreg.Readstring('software\microsoft\windows\currentversion\Explorer\Shell Folders', 'Start Menu', '');{MENUEN START}
   5: Result := Winreg.Readstring('software\microsoft\windows\currentversion\Explorer\Shell Folders', 'Programs', '');  {MENUEN START\Programmer}
   6: Result := Winreg.Readstring('software\microsoft\windows\currentversion\Explorer\Shell Folders', 'Startup', '');   {MENUEN START\Programmer\Start}
   7: Result := Winreg.Readstring('software\microsoft\windows\currentversion\Explorer\Shell Folders', 'Fonts', '');     {FONTS}
   8: Result := Winreg.Readstring('software\microsoft\windows\currentversion\Explorer\Shell Folders', 'Sendto', '');    {SENDTO}
   9: Result := Winreg.Readstring('software\microsoft\windows\currentversion\Explorer\Shell Folders', 'Recent', '');    {MENUEN START\Dokumenter}
  10: Result := Winreg.Readstring('software\microsoft\windows\currentversion\Explorer\Shell Folders', 'Favorites', ''); {FORTRUKNE}
  11: Result := Winreg.Readstring('software\microsoft\windows\currentversion\Explorer\Shell Folders', 'NetHood', '');   {ANDRE COMPUTERE}
  12: Result := Winreg.Readstring('software\microsoft\windows\currentversion\Explorer\Shell Folders', 'Personal', '');  {DOKUMENTER}
  13: Result := Winreg.Readstring('software\microsoft\windows\currentversion\Explorer\Shell Folders', 'Cache', '');     {TEMP INTERNET FILER}
  14: Result := Winreg.Readstring('software\microsoft\windows\currentversion\Explorer\Shell Folders', 'Cookies', '');   {Cookies}
  15: Result := Winreg.Readstring('software\microsoft\windows\currentversion\Explorer\Shell Folders', 'History', '');   {OVERSIGT}
  16: Result := Winreg.Readstring('software\microsoft\windows\currentversion\Explorer\Shell Folders', 'PrintHood', ''); {Windows\PrintHood}
  End;
  Init2;
  Case Index of
   17: Result := Winreg.Readstring('software\microsoft\windows\currentversion', 'DevicePath', '');             {INF FILES PATH}
   18: Result := WinReg.ReadString('software\microsoft\windows\currentversion', 'Registeredowner', '');        {OWNER NAME}
   19: Result := WinReg.ReadString('software\microsoft\windows\currentversion', 'RegisteredOrganization', ''); {ORG}
   20: Result := WinReg.ReadString('software\microsoft\windows\currentversion', 'ProductId', '');              {ProductID}
   21: Result := WinReg.ReadString('software\microsoft\windows\currentversion', 'ProgramFilesDir', '');        {\Programmer}
   22: Result := WinReg.ReadString('software\microsoft\windows\currentversion', 'CommonFilesDir', '');         {FÆLLES FILER}
   23: Result := WinReg.ReadString('software\microsoft\windows\currentversion', 'WallPaperDir', '');           {\Windows\Web\Wallpaper}
   24: Result := WinReg.ReadString('software\microsoft\windows\currentversion', 'MediaPath', '');              {\Windows\Media}
   25: Result := WinReg.ReadString('software\microsoft\windows\currentversion', 'ConfigPath', '');             {\Windows\Config}
   26: Result := WinReg.ReadString('software\microsoft\windows\currentversion', 'SystemRoot', '');             {\Windows}
   27: Result := WinReg.ReadString('software\microsoft\windows\currentversion', 'ProductName', '');
   28: Result := WinReg.ReadString('software\microsoft\windows\currentversion', 'Version', '');                {Product version}
   29: Result := WinReg.ReadString('software\microsoft\windows\currentversion', 'VersionNumber', '');          {Product version number}
   30: Result := WinReg.ReadString('software\microsoft\windows\currentversion', 'ProductKey', '');
   31: Result := WinReg.ReadString('software\microsoft\windows\currentversion', 'ProgramFilesPath', '');       {\Programmer}
   32: Result := WinReg.ReadString('software\microsoft\windows\currentversion', 'OtherDevicePath', '');        {\Windows\Inf\Other}
   44: Begin
        S1 := WinReg.ReadString('software\microsoft\windows\currentversion', 'SystemRoot', '');             {\Windows\Temp}
        If S1 <> '' Then S1 := PathToOk(S1) + 'Temp';
        Result := S1;
       End;
  End;
 End;
end;

Function GetDesktop : String;
begin
 Result := GetReg(1);
End;

Function GetTemplates : string;
begin
 result := GetReg(2);
End;

Function GetAppData : string;
begin
 result := GetReg(3);
End;

Function GetStartMenu : string;
begin
 result := GetReg(4);
End;

Function GetStartMenuPrograms : string;
begin
 result := GetReg(5);
End;

Function GetStartUp : string;
begin
 result := GetReg(6);
End;

Function GetFonts : string;
begin
 result := GetReg(7);
End;

Function GetSendTo : string;
begin
 result := GetReg(8);
End;

Function GetRecent : string;
begin
 result := GetReg(9);
End;

Function GetFavorites : string;
begin
 result := GetReg(10);
End;

Function GetNetHood : string;
begin
 result := GetReg(11);
End;

Function GetPersonal : string;
begin
 result := GetReg(12);
End;

Function GetCache : string;
begin
 result := GetReg(13);
End;

Function GetCookies : string;
begin
 result := GetReg(14);
End;

Function GetHistory : string;
begin
 result := GetReg(15);
End;

Function GetPrintHood : string;
begin
 result := GetReg(16);
End;

Function GetDevicePath : string;
begin
 result := GetReg(17);
End;

Function GetRegisteredowner : string;
begin
 result := GetReg(18);
End;

Function GetRegisteredOrganization : string;
begin
 result := GetReg(19);
End;

Function GetProductId : string;
begin
 result := GetReg(20);
End;

Function GetProgramFilesDir : string;
begin
 result := GetReg(21);
End;

Function GetCommonFilesDir : string;
begin
 result := GetReg(22);
End;

Function GetWallPaperDir : string;
begin
 result := GetReg(23);
End;

Function GetMediaPath : string;
begin
 result := GetReg(24);
End;

Function GetConfigPath : string;
begin
 result := GetReg(25);
End;

Function GetSystemRoot : string;
begin
 result := GetReg(26);
End;

Function GetProductName : string;
begin
 result := GetReg(27);
End;

Function GetWinVersion : string;
begin
 result := GetReg(28);
End;

Function GetVersionNumber : string;
begin
 result := GetReg(29);
End;

Function GetProductKey : string;
begin
 result := GetReg(30);
End;

Function GetProgramFilesPath : string;
begin
 result := GetReg(31);
End;

Function GetOtherDevicePath : string;
begin
 result := GetReg(32);
End;

// NT Extensions
Function GetCommonAdmTools: String;
Begin
 Result := GetReg(33);
End;

Function GetCommonAppData: String;
Begin
 Result := GetReg(34);
End;

Function GetCommonDesktop: String;
Begin
 Result := GetReg(35);
End;

Function GetCommonDocuments: String;
Begin
 Result := GetReg(36);
End;

Function GetCommonPrograms: String;
Begin
 Result := GetReg(37);
End;

Function GetCommonStartMenu: String;
Begin
 Result := GetReg(38);
End;

Function GetCommonStartup: String;
Begin
 Result := GetReg(39);
End;

Function GetCommonTemplates: String;
Begin
 Result := GetReg(40);
End;

Function GetLocalAppData: String;
Begin
 Result := GetReg(41);
End;

Function GetLocalSettings: String;
Begin
 Result := GetReg(42);
End;

Function GetMyPictures: String;
Begin
 Result := GetReg(43);
End;

Function GetTemp: String;
Begin
 Result := GetReg(44);
End;

end.
