unit MyInfo;

// by Randy Beck
//   http://www.randybeck.com
//   email:  rb@randybeck.com
//
// note: this unit sets {$WRITEABLECONST ON }

interface

uses
    WinTypes, SysUtils, Registry;

function  CDPrepDirectory : string;
  // Returns the folder used by Windows to store files pending transfer to
  // the CD.  In other words, copying files to this location will cause
  // Windows to tell the user there are files waiting to be burned to the CD.
  // AFAIK, this won't work on Windows versions prior to XP Service Pack 2
  // in which case it will return an empty string.

function  WindowsRegisteredOrganizationName : string;
function  WindowsRegisteredOwnerName : string;
  // These two functions return the name or organization that the
  // owner used when he or she registered their system.  It will
  // often be their full name, but it may also be something like
  // "Registered Owner".


function  WindowsVersionName : string;
  // This function returns the name of the Windows version as a string.


function  WindowsDirectory : string;
function  TempDirectory : string;
function  DesktopDirectory : string;
function  ProgramFilesDirectory : string;
function  MyDocumentsDirectory : string;
  // These functions return the locations of the Windows, Desktop,
  // Temp, Program Files and My Documents folders.  They will always
  // have a trailing backslash ("\") even if the registry entry
  // didn't supply one.  This will allow you to append a filename
  // without worrying about a backslash, like this:
  //     CreateThisFile(DesktopDirectory + 'MyFile.txt');


implementation

function  GetRegistryKeyValue(ARoot: HKEY; AKey,AName: string) : string;
var  Reg: TRegistry;
begin
  Result := '';
  Reg := TRegistry.Create;
  try
    Reg.RootKey := ARoot;
    If Reg.OpenKey(AKey, FALSE) then
      Result := Reg.ReadString(AName);
  finally
    Reg.Free;
  end;
end;

{$WRITEABLECONST ON }

function  CDPrepDirectory : string;
const  S: string = '?';
begin
  If S = '?' then
  begin
    S := GetRegistryKeyValue(HKEY_CURRENT_USER,
                             'Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders',
                             'CD Burning');
    If (length(S) > 0) and (S[length(S)] <> '\') then
      S := S + '\';
  end;
  Result := S;
end;


function  WindowsRegisteredOwnerName : string;
const  S: string = '?';
begin
  If S = '?' then
  begin
    S := GetRegistryKeyValue(HKEY_LOCAL_MACHINE,
                             'Software\Microsoft\Windows\CurrentVersion',
                             'RegisteredOwner');
    If S = '' then
      S := GetRegistryKeyValue(HKEY_LOCAL_MACHINE,
                               'Software\Microsoft\Windows NT\CurrentVersion',
                               'RegisteredOwner');
  end;
  Result := S;
end;


function  WindowsRegisteredOrganizationName : string;
const  S: string = '?';
begin
  If S = '?' then
  begin
    S := GetRegistryKeyValue(HKEY_LOCAL_MACHINE,
                             'Software\Microsoft\Windows\CurrentVersion',
                             'RegisteredOrganization');
    If S = '' then
      S := GetRegistryKeyValue(HKEY_LOCAL_MACHINE,
                               'Software\Microsoft\Windows NT\CurrentVersion',
                               'RegisteredOrganization');
  end;
  Result := S;
end;


function  WindowsVersionName : string;
const  WinVerStr: string = '?';
begin
  If WinVerStr = '?' then
  begin
    WinVerStr := GetRegistryKeyValue(HKEY_LOCAL_MACHINE,
                                     'Software\Microsoft\Windows\CurrentVersion\',
                                     'Version');
    If WinVerStr = '' then
      WinVerStr := GetRegistryKeyValue(HKEY_LOCAL_MACHINE,
                                       'SOFTWARE\Microsoft\Windows NT\CurrentVersion',
                                       'ProductName');
  end;
  Result := WinVerStr;
end;


function  WindowsDirectory : string;
const WinDir: string[255] = '?';
var  i: integer;
begin
  If WinDir = '?' then
  begin
    i := GetWindowsDirectory(@WinDir[1], 255);
    If i > 245 then
      WinDir := ''
    else
    begin
      SetLength(WinDir, i);
      If (length(WinDir) > 0) and (WinDir[length(WinDir)] = '\') then
        SetLength(WinDir, pred(length(WinDir)));
    end;
  end;
  Result := WinDir;
end;


function  TempDirectory : string;
const TempDir: ShortString = '?';
var   TempFileName: array[0..255] of char;
begin
  If TempDir = '?' then
  begin
    GetTempPath(255, @TempFileName);
    TempDir := StrPas(@TempFileName);
    While (TempDir <> '') and (TempDir[length(TempDir)] <> '\') do Dec(TempDir[0]);
    If TempDir <> '' then
    begin
      If TempDir[length(TempDir)] <> '\' then
        TempDir := TempDir + '\';
    end;
  end;
  Result := TempDir;
end;


function  DesktopDirectory : string;
const  S: string = '?';
begin
  If S = '?' then
  begin
    S := GetRegistryKeyValue(HKEY_CURRENT_USER,
                             'Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders',
                             'Desktop');
    If (length(S) > 0) and (S[length(S)] <> '\') then S := S + '\';
  end;
  Result := S;
end;


function  ProgramFilesDirectory : string;
const  S: string = '?';
begin
  If S = '?' then
  begin
    S := GetRegistryKeyValue(HKEY_LOCAL_MACHINE,
                             'Software\Microsoft\Windows\CurrentVersion',
                             'ProgramFilesDir');
    If (length(S) > 0) and (S[length(S)] <> '\') then S := S + '\';
  end;
  Result := S;
end;


function  MyDocumentsDirectory : string;
const  MyDocs: string = '?';
begin
  If MyDocs = '?' then
  begin
    MyDocs := GetRegistryKeyValue(HKEY_CURRENT_USER,
                                  'Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders',
                                  'Personal');
    If (length(MyDocs) > 0) and (MyDocs[length(MyDocs)] <> '\') then
      MyDocs := MyDocs + '\';
  end;
  Result := MyDocs;
end;


end.


