home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 March / Chip_1998-03_cd.bin / zkuste / delphi / ruzkomp / APITHING.ZIP / tvAPIThing.pas < prev   
Pascal/Delphi Source File  |  1997-09-14  |  29KB  |  765 lines

  1. unit tvAPIThing;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Classes, Graphics, Controls, ShellAPI, Forms, Dialogs;
  7.  
  8. type
  9.   TInformationStrings = ( isCompanyName,  isFileDescription, isFileVersion,
  10.                           isInternalName, isLegalCopyright,  isOriginalFilename,
  11.                           isProductName,  isProductVersion,  isComments,
  12.                           isLegalTrademarks );
  13.  
  14.   TFileTimeComparision = ( ftError, ftFileOneIsOlder, ftFileTimesAreEqual, ftFileTwoIsOlder );
  15.  
  16.   TTimeOfWhat = ( ftCreationTime, ftLastAccessTime, ftLastWriteTime );
  17.  
  18.   TDriveType = (dtUnknown, dtNoDrive, dtFloppy, dtFixed, dtNetwork, dtCDROM, dtRAM);
  19.  
  20.   TVolumeInfo = record
  21.      Name               : String;
  22.      SerialNumber       : DWORD;
  23.      MaxComponentLength : DWORD;
  24.      FileSystemFlags    : DWORD;
  25.      FileSystemName     : String;
  26.   end; // TVolumeInfo
  27.  
  28.   type
  29.   PFixedFileInfo = ^TFixedFileInfo;
  30.   TFixedFileInfo = record
  31.      dwSignature       : DWORD;
  32.      dwStrucVersion    : DWORD;
  33.      wFileVersionMS    : WORD;  // Minor Version
  34.      wFileVersionLS    : WORD;  // Major Version
  35.      wProductVersionMS : WORD;  // Build Number
  36.      wProductVersionLS : WORD;  // Release Version
  37.      dwFileFlagsMask   : DWORD;
  38.      dwFileFlags       : DWORD;
  39.      dwFileOS          : DWORD;
  40.      dwFileType        : DWORD;
  41.      dwFileSubtype     : DWORD;
  42.      dwFileDateMS      : DWORD;
  43.      dwFileDateLS      : DWORD;
  44.   end; // TFixedFileInfo
  45.  
  46.   TtvAPIThing = class( TComponent )
  47.   private
  48.     FPageSize                         : DWORD;
  49.     FProcessorType                    : String;
  50.     FNumberOfProcessors               : DWORD;
  51.     // System Information
  52.     function myGetUserName            : String;
  53.     function myGetComputerName        : String;
  54.     function myGetWindowsDirectory    : String;
  55.     function myGetSystemDirectory     : String;
  56.     // Time Functions
  57.     function myGetSystemTime          : String;
  58.     function myGetLocalTime           : String;
  59.  
  60.     // File Functions
  61.     function myGetCurrentDirectory    : String;
  62.     function myGetTempPath            : String;
  63.     function myGetLogicalDrives       : String;
  64.  
  65.     function myGetFileTime( const FileName : String; ComparisonType : TTimeOfWhat ) : TFileTime;
  66.     function myGetVersion                                                           : String;
  67.     function myGlobalMemoryStatus( Index : Integer )                                : DWORD;
  68.  
  69.     procedure myGetSystemInfo;
  70.   protected
  71.     procedure Loaded; override;
  72.   public
  73.     function GetFileInformation( const FileName, Value : String ): String;
  74.     function CompareFileTime( const FileNameOne, FileNameTwo : String; ComparisonType : TTimeOfWhat ): TFileTimeComparision;
  75.     function GetFileTime( const FileName : String; ComparisonType : TTimeOfWhat ): TDateTime;
  76.     function FileInfo( const FileName : String ) : TFixedFileInfo;
  77.     function ExtractIcon( const FileName : String ): HIcon;
  78.     function ExtractAssociatedIcon( FileName : String ): HIcon;
  79.     function GetFreeDiskSpace( const Drive : Char ) : LongInt;
  80.     function FileSize( const FileName : String ) : LongInt;
  81.     function GetShortPathName( const Path : String ): String;
  82.     function GetFullPathName( const Path : String ): String;
  83.     function GetVolumeInformation( const Drive : Char ) : TVolumeInfo;
  84.     function FindExecutable( const FileName : String ): String;
  85.     function DriveType( const Drive : Char ) : TDriveType;
  86.  
  87.     procedure ShellAbout( const TitleBar, OtherText : String );
  88.     procedure FormatDrive( const Drive : Char );
  89.     procedure ShutDown;
  90.   published
  91.     // System Information
  92.     property UserName               : String read myGetUserName;
  93.     property ComputerName           : String read myGetComputerName;
  94.     property WindowsDirectory       : String read myGetWindowsDirectory;
  95.     property SystemDirectory        : String read myGetSystemDirectory;
  96.     // Time Functions
  97.     property SystemTime             : String read myGetSystemTime;
  98.     property LocalTime              : String read myGetLocalTime;
  99.     // File Functions
  100.     property CurrentDirectory       : String read myGetCurrentDirectory;
  101.     property TempPath               : String read myGetTempPath;
  102.     property LogicalDrives          : String read myGetLogicalDrives;
  103.     property PageSize               : DWORD  read FPageSize;
  104.     property ProcessorType          : String read FProcessorType;
  105.     property NumberOfProcessors     : DWORD read FNumberOfProcessors;
  106.     property OSVersion              : String read myGetVersion;
  107.     // From GlobalMemoryStatus
  108.     property dwMemoryLoad           : DWORD index 1 read myGlobalMemoryStatus;
  109.     property dwTotalPhys            : DWORD index 2 read myGlobalMemoryStatus;
  110.     property dwAvailPhys            : DWORD index 3 read myGlobalMemoryStatus;
  111.     property dwTotalPageFile        : DWORD index 4 read myGlobalMemoryStatus;
  112.     property dwAvailPageFile        : DWORD index 5 read myGlobalMemoryStatus;
  113.     property dwTotalVirtual         : DWORD index 6 read myGlobalMemoryStatus;
  114.     property dwAvailVirtual         : DWORD index 7 read myGlobalMemoryStatus;
  115.   end;
  116.  
  117.   procedure Register;
  118.  
  119. const
  120.    PROCESSOR_INTEL_386     = 386;
  121.    PROCESSOR_INTEL_486     = 486;
  122.    PROCESSOR_INTEL_PENTIUM = 586;
  123.    PROCESSOR_MIPS_R4000    = 4000;
  124.    PROCESSOR_ALPHA_21064   = 21064;
  125.  
  126. function SHFormatDrive(hWnd : HWND;Drive, fmtID, Options : Word) : longint; stdcall; external  'shell32.dll';
  127.  
  128. implementation
  129.  
  130. // Goes right after the VS_FIXEDFILEINFO structure
  131. function TtvAPIThing.FileInfo( const FileName :String ) : TFixedFileInfo;
  132. var
  133.   dwHandle, dwVersionSize : DWORD;
  134.   strSubBlock             : String;
  135.   pTemp                   : Pointer;
  136.   pData                   : Pointer;
  137. begin
  138.    strSubBlock := '\';
  139.  
  140.    // get version information values
  141.    dwVersionSize := GetFileVersionInfoSize( PChar( FileName ), // pointer to filename string
  142.                                             dwHandle );        // pointer to variable to receive zero
  143.  
  144.    // if GetFileVersionInfoSize is successful
  145.    if dwVersionSize <> 0 then
  146.    begin
  147.       GetMem( pTemp, dwVersionSize );
  148.       try
  149.          if GetFileVersionInfo( PChar( FileName ),             // pointer to filename string
  150.                                 dwHandle,                      // ignored
  151.                                 dwVersionSize,                 // size of buffer
  152.                                 pTemp ) then                   // pointer to buffer to receive file-version info.
  153.  
  154.             if VerQueryValue( pTemp,                           // pBlock     - address of buffer for version resource
  155.                               PChar( strSubBlock ),            // lpSubBlock - address of value to retrieve
  156.                               pData,                           // lplpBuffer - address of buffer for version pointer
  157.                               dwVersionSize ) then             // puLen      - address of version-value length buffer
  158.                Result := PFixedFileInfo( pData )^;
  159.       finally
  160.          FreeMem( pTemp );
  161.       end; // try
  162.    end; // if dwVersionSize
  163. end;
  164.  
  165. function TtvAPIThing.GetFileInformation( const FileName, Value : String ): String;
  166. var
  167.   dwHandle, dwVersionSize   : DWORD;
  168.   strLangCharSetInfoString  : String;
  169.   pcBuffer                  : PChar;
  170.   pTemp                     : Pointer;
  171. begin
  172.    //////////////////////////////////////////////////////////////////////////////////
  173.    // The Win32 API contains the following predefined version information strings: //
  174.    //////////////////////////////////////////////////////////////////////////////////
  175.    //    CompanyName               FileDescription          FileVersion            //
  176.    //    InternalName              LegalCopyright           OriginalFilename       //
  177.    //    ProductName               ProductVersion           Comments               //
  178.    //    LegalTrademarks                                                           //
  179.    //////////////////////////////////////////////////////////////////////////////////
  180.  
  181.    //////////////////////////////////////////////////////////////////////////////////
  182.    // Decription of lpSubBlock from the Win32 API (sLangCharSet)                   //
  183.    //////////////////////////////////////////////////////////////////////////////////
  184.    // Specifies a value in a language-specific structure. The lang-charset name is //
  185.    // a concatenation of a language and character-set identifier pair found in the //
  186.    // translation table for the resource. The lang-charset name must be specified  //
  187.    // as a hexadecimal string. The string-name name is one of the predefined       //
  188.    // strings described in the following Remarks section.                          //
  189.    //////////////////////////////////////////////////////////////////////////////////
  190.  
  191.    strLangCharSetInfoString := '\StringFileInfo\040904E4\' + Value;
  192.  
  193.    // get version information values
  194.    dwVersionSize := GetFileVersionInfoSize( PChar( FileName ),   // pointer to filename string
  195.                                             dwHandle );          // pointer to variable to receive zero
  196.  
  197.    // if GetFileVersionInfoSize is successful
  198.    if dwVersionSize <> 0 then
  199.    begin
  200.       GetMem( pcBuffer, dwVersionSize );
  201.       try
  202.          if GetFileVersionInfo( PChar( FileName ),               // pointer to filename string
  203.                                 dwHandle,                        // ignored
  204.                                 dwVersionSize,                   // size of buffer
  205.                                 pcBuffer ) then                  // pointer to buffer to receive file-version info.
  206.  
  207.             if VerQueryValue( pcBuffer,                          // pBlock     - address of buffer for version resource
  208.                               PChar( strLangCharSetInfoString ), // lpSubBlock - address of value to retrieve
  209.                               pTemp,                             // lplpBuffer - address of buffer for version pointer
  210.                               dwVersionSize ) then               // puLen      - address of version-value length buffer
  211.  
  212.                Result := PChar( pTemp );
  213.       finally
  214.          FreeMem( pcBuffer );
  215.       end; // try
  216.    end;// if dwVersionSize
  217. end; // GetFileInformation
  218.  
  219. function TtvAPIThing.myGetUserName : String;
  220. var
  221.    pcUser   : PChar;
  222.    dwUSize : DWORD;
  223. begin
  224.    dwUSize := 21; // user name can be up to 20 characters
  225.    GetMem( pcUser, dwUSize ); // allocate memory for the string
  226.    try
  227.       if Windows.GetUserName( pcUser, dwUSize ) then
  228.          Result := pcUser
  229.    finally
  230.       FreeMem( pcUser ); // now free the memory allocated for the string
  231.    end;
  232. end;
  233.  
  234. function TtvAPIThing.myGetComputerName : String;
  235. var
  236.    pcComputer : PChar;
  237.    dwCSize    : DWORD;
  238. begin
  239.    dwCSize := MAX_COMPUTERNAME_LENGTH + 1;
  240.    GetMem( pcComputer, dwCSize ); // allocate memory for the string
  241.    try
  242.       if Windows.GetComputerName( pcComputer, dwCSize ) then
  243.          Result := pcComputer;
  244.    finally
  245.       FreeMem( pcComputer ); // now free the memory allocated for the string
  246.    end;
  247. end;
  248.  
  249. function TtvAPIThing.myGetWindowsDirectory : String;
  250. var
  251.    pcWindowsDirectory : PChar;
  252.    dwWDSize           : DWORD;
  253. begin
  254.    dwWDSize := MAX_PATH + 1;
  255.    GetMem( pcWindowsDirectory, dwWDSize ); // allocate memory for the string
  256.    try
  257.       if Windows.GetWindowsDirectory( pcWindowsDirectory, dwWDSize ) <> 0 then
  258.          Result := pcWindowsDirectory;
  259.    finally
  260.       FreeMem( pcWindowsDirectory ); // now free the memory allocated for the string
  261.    end;
  262. end;
  263.  
  264. function TtvAPIThing.myGetSystemDirectory : String;
  265. var
  266.    pcSystemDirectory : PChar;
  267.    dwSDSize          : DWORD;
  268. begin
  269.    dwSDSize := MAX_PATH + 1;
  270.    GetMem( pcSystemDirectory, dwSDSize ); // allocate memory for the string
  271.    try
  272.       if Windows.GetSystemDirectory( pcSystemDirectory, dwSDSize ) <> 0 then
  273.          Result := pcSystemDirectory;
  274.    finally
  275.       FreeMem( pcSystemDirectory ); // now free the memory allocated for the string
  276.    end;
  277. end;
  278.  
  279. function TtvAPIThing.myGetSystemTime : String;
  280. var
  281.    stSystemTime : TSystemTime;
  282. begin
  283.    Windows.GetSystemTime( stSystemTime );
  284.    Result := DateTimeToStr( SystemTimeToDateTime( stSystemTime ) );
  285. end;
  286.  
  287. function TtvAPIThing.myGetLocalTime : String;
  288. var
  289.    stSystemTime : TSystemTime;
  290. begin
  291.    Windows.GetLocalTime( stSystemTime );
  292.    Result := DateTimeToStr( SystemTimeToDateTime( stSystemTime ) );
  293. end;
  294.  
  295. function TtvAPIThing.CompareFileTime( const FileNameOne, FileNameTwo : String; ComparisonType : TTimeOfWhat ): TFileTimeComparision;
  296. var
  297.    FileOneFileTime : TFileTime;
  298.    FileTwoFileTime : TFileTime;
  299. begin
  300.    Result := ftError;
  301.  
  302.    FileOneFileTime := myGetFileTime( FileNameOne, ComparisonType );
  303.    FileTwoFileTime := myGetFileTime( FileNameTwo, ComparisonType );
  304.  
  305.    case Windows.CompareFileTime( FileOneFileTime, FileTwoFileTime ) of
  306.       -1 : Result := ftFileOneIsOlder;
  307.        0 : Result := ftFileTimesAreEqual;
  308.        1 : Result := ftFileTwoIsOlder;
  309.    end;
  310.  
  311. end;
  312.  
  313. function TtvAPIThing.GetFileTime( const FileName : String; ComparisonType : TTimeOfWhat ): TDateTime;
  314. var
  315.    SystemTime : TSystemTime;
  316.    FileTime   : TFileTime;
  317. begin
  318.    Result := StrToDate( '12/31/9999' );
  319.  
  320.    FileTime := myGetFileTime( FileName, ComparisonType );
  321.    if FileTimeToSystemTime( FileTime, SystemTime ) then
  322.       // Convert to TDateTime and return
  323.       Result := SystemTimeToDateTime( SystemTime );
  324. end;
  325.  
  326. function TtvAPIThing.myGetFileTime( const FileName : String; ComparisonType : TTimeOfWhat ) : TFileTime;
  327. var
  328.    FileTime, LocalFileTime : TFileTime;
  329.    hFile                   : THandle;
  330. begin
  331.    // initialize TFileTime record in case of error
  332.    Result.dwLowDateTime := 0;
  333.    Result.dwHighDateTime := 0;
  334.    hFile := FileOpen( FileName, fmShareDenyNone );
  335.    try
  336.       if hFile <> 0 then
  337.       begin
  338.          case ComparisonType of
  339.             ftCreationTime   : Windows.GetFileTime( hFile, @FileTime, nil, nil );
  340.             ftLastAccessTime : Windows.GetFileTime( hFile, nil, @FileTime, nil );
  341.             ftLastWriteTime  : Windows.GetFileTime( hFile, nil, nil, @FileTime );
  342.          end; // case FileTimeOf
  343.  
  344.          // Change the file time to local time
  345.          FileTimeToLocalFileTime( FileTime, LocalFileTime );
  346.          Result := LocalFileTime;
  347.       end; // if hFile <> 0
  348.    finally
  349.       FileClose( hFile );
  350.    end; // try
  351. end;
  352.  
  353. procedure TtvAPIThing.ShellAbout( const TitleBar, OtherText : String );
  354. begin
  355.    ShellAPI.ShellAbout( Application.Handle,
  356.                         PChar( TitleBar ),
  357.                         PChar( OtherText ),
  358.                         Application.Icon.Handle );
  359. end;
  360.  
  361. function TtvAPIThing.ExtractIcon( const FileName : String ): HIcon;
  362. begin
  363.    Result := ShellAPI.ExtractIcon( Application.Handle,
  364.                                    PChar( FileName ),
  365.                                    0 );
  366. end;
  367.  
  368. function TtvAPIThing.ExtractAssociatedIcon( FileName : String ): HIcon;
  369. var
  370.    wIndex  : Word;
  371.    pcFileName : Pchar;
  372. begin
  373.    // with help from:
  374.    // William A. Portillo.
  375.    //wp@ois.com.au
  376.    GetMem( pcFileName, MAX_PATH + 1 ); // Allocate memory for our pointer
  377.    try
  378.       StrPCopy( pcFilename, FileName ); // Copy the Filename into the Pchar var
  379.       Result := ShellAPI.ExtractAssociatedIcon( Application.Handle,
  380.                                                 pcFileName,
  381.                                                 wIndex );
  382.    finally
  383.       // free allocated memory
  384.       FreeMem( pcFileName );
  385.    end; // try
  386. end;
  387.  
  388. function TtvAPIThing.GetFreeDiskSpace( const Drive : Char ) : LongInt;
  389. var
  390.    lpRootPathName          : PChar;    // address of root path
  391.    lpSectorsPerCluster     : DWORD;    // address of sectors per cluster
  392.    lpBytesPerSector        : DWORD;    // address of bytes per sector
  393.    lpNumberOfFreeClusters  : DWORD;    // address of number of free clusters
  394.    lpTotalNumberOfClusters : DWORD;    // address of total number of clusters
  395. begin
  396.       lpRootPathName := PChar( Drive + ':\' );
  397.       if Windows.GetDiskFreeSpace( lpRootPathName,
  398.                                    lpSectorsPerCluster,
  399.                                    lpBytesPerSector,
  400.                                    lpNumberOfFreeClusters,
  401.                                    lpTotalNumberOfClusters ) then
  402.          Result := lpNumberOfFreeClusters * lpBytesPerSector * lpSectorsPerCluster
  403.       else
  404.          Result := -1;
  405. end;
  406.  
  407. function TtvAPIThing.myGetCurrentDirectory: String;
  408. var
  409.    nBufferLength : DWORD; // size, in characters, of directory buffer
  410.    lpBuffer      : PChar; // address of buffer for current directory
  411. begin
  412.    GetMem( lpBuffer, MAX_PATH + 1 );
  413.    nBufferLength := 0;
  414.    try
  415.       if Windows.GetCurrentDirectory( nBufferLength, lpBuffer ) > 0 then
  416.          Result := lpBuffer;
  417.    finally
  418.       FreeMem( lpBuffer );
  419.    end; // try
  420. end;
  421.  
  422. function TtvAPIThing.FileSize( const FileName : String ) : LongInt;
  423. var
  424.    hFile          : THandle; // handle of file to get size of
  425.    lpFileSizeHigh : DWORD;   // address of high-order word for file size
  426. begin
  427.    Result := -1;
  428.    hFile := FileOpen( FileName, fmShareDenyNone );
  429.    try
  430.       if hFile <> 0 then
  431.          Result := Windows.GetFileSize( hFile, @lpFileSizeHigh );
  432.    finally
  433.       FileClose( hFile );
  434.    end; // try
  435. end;
  436.  
  437. function TtvAPIThing.GetShortPathName( const Path : String ): String;
  438. var
  439.    lpszShortPath : PChar; // points to a buffer to receive the null-terminated short form of the path
  440. begin
  441.    GetMem( lpszShortPath, MAX_PATH + 1 );
  442.    try
  443.       Windows.GetShortPathName( PChar( Path ), lpszShortPath, MAX_PATH + 1 );
  444.       Result := lpszShortPath;
  445.    finally
  446.       FreeMem( lpszShortPath );
  447.    end;
  448. end;
  449.  
  450. function TtvAPIThing.myGetTempPath: String;
  451. var
  452.     nBufferLength : DWORD; // size, in characters, of the buffer
  453.     lpBuffer      : PChar; // address of buffer for temp. path
  454. begin
  455.    nBufferLength := 0; // initialize 
  456.    GetMem( lpBuffer, MAX_PATH + 1 );
  457.    try
  458.       if GetTempPath( nBufferLength, lpBuffer ) <> 0 then
  459.          Result := lpBuffer
  460.       else
  461.          Result := '';
  462.    finally
  463.       FreeMem( lpBuffer );
  464.    end;
  465. end;
  466.  
  467. function TtvAPIThing.GetVolumeInformation( const Drive : Char ) : TVolumeInfo;
  468. var
  469.    lpRootPathName           : PChar; // address of root directory of the file system
  470.    lpVolumeNameBuffer       : PChar; // address of name of the volume
  471.    nVolumeNameSize          : DWORD; // length of lpVolumeNameBuffer
  472.    lpVolumeSerialNumber     : DWORD; // address of volume serial number
  473.    lpMaximumComponentLength : DWORD; // address of system's maximum filename length
  474.    lpFileSystemFlags        : DWORD; // address of file system flags
  475.    lpFileSystemNameBuffer   : PChar; // address of name of file system
  476.    nFileSystemNameSize      : DWORD; // length of lpFileSystemNameBuffer
  477. begin
  478.    GetMem( lpVolumeNameBuffer, MAX_PATH + 1 );
  479.    GetMem( lpFileSystemNameBuffer, MAX_PATH + 1 );
  480.    try
  481.       nVolumeNameSize := MAX_PATH + 1;
  482.       nFileSystemNameSize := MAX_PATH + 1;
  483.  
  484.       lpRootPathName := PChar( Drive + ':\' );
  485.       if Windows.GetVolumeInformation( lpRootPathName,
  486.                                        lpVolumeNameBuffer,
  487.                                        nVolumeNameSize,
  488.                                        @lpVolumeSerialNumber,
  489.                                        lpMaximumComponentLength,
  490.                                        lpFileSystemFlags,
  491.                                        lpFileSystemNameBuffer,
  492.                                        nFileSystemNameSize ) then
  493.       begin
  494.       (*
  495.          // to check disk flags do the following
  496.          if (lpFileSystemFlags and FS_CASE_IS_PRESERVED) <> 0 then
  497.             if Length( flags ) <> 0 then
  498.                flags := flags + #13#10'FS_CASE_IS_PRESERVED'
  499.             else
  500.                flags := 'FS_CASE_IS_PRESERVED';
  501.  
  502.          if (lpFileSystemFlags and FS_CASE_SENSITIVE) <> 0 then
  503.             if Length( flags ) <> 0 then
  504.                flags := flags + #13#10'FS_CASE_SENSITIVE'
  505.             else
  506.                flags := 'FS_CASE_SENSITIVE';
  507.  
  508.          if (lpFileSystemFlags and FS_UNICODE_STORED_ON_DISK) <> 0 then
  509.             if Length( flags ) <> 0 then
  510.                flags := flags + #13#10'FS_UNICODE_STORED_ON_DISK'
  511.             else
  512.                flags := 'FS_UNICODE_STORED_ON_DISK';
  513.  
  514.          if (lpFileSystemFlags and FS_PERSISTENT_ACLS) <> 0 then
  515.             if Length( flags ) <> 0 then
  516.                flags := flags + #13#10'FS_PERSISTENT_ACLS'
  517.             else
  518.                flags := 'FS_PERSISTENT_ACLS';
  519.  
  520.          if (lpFileSystemFlags and FS_FILE_COMPRESSION) <> 0 then
  521.             if Length( flags ) <> 0 then
  522.                flags := flags + #13#10'FS_FILE_COMPRESSION'
  523.             else
  524.                flags := 'FS_FILE_COMPRESSION';
  525.  
  526.          if (lpFileSystemFlags and FS_VOL_IS_COMPRESSED) <> 0 then
  527.             if Length( flags ) <> 0 then
  528.                flags := flags + #13#10'FS_VOL_IS_COMPRESSED'
  529.             else
  530.                flags := 'FS_VOL_IS_COMPRESSED';
  531.          *)
  532.  
  533.          with Result do
  534.          begin
  535.             Name               := lpVolumeNameBuffer;
  536.             SerialNumber       := lpVolumeSerialNumber;
  537.             MaxComponentLength := lpMaximumComponentLength;
  538.             FileSystemFlags    := lpFileSystemFlags;
  539.             FileSystemName     := lpFileSystemNameBuffer;
  540.          end; // with Result
  541.       end // if
  542.       else
  543.       begin
  544.          with Result do
  545.          begin
  546.             Name               := '';
  547.             SerialNumber       := -1;
  548.             MaxComponentLength := -1;
  549.             FileSystemFlags    := -1;
  550.             FileSystemName     := '';
  551.          end; // with Result
  552.       end; // else
  553.    finally
  554.       FreeMem( lpVolumeNameBuffer );
  555.       FreeMem( lpFileSystemNameBuffer );
  556.    end; // try
  557. end;
  558.  
  559. function TtvAPIThing.GetFullPathName( const Path : String ): String;
  560. var
  561.    nBufferLength : DWORD; // size, in characters, of path buffer
  562.    lpBuffer      : PChar; // address of path buffer
  563.    lpFilePart    : PChar; // address of filename in path
  564. begin
  565.    nBufferLength := MAX_PATH + 1;
  566.    GetMem( lpBuffer, MAX_PATH + 1 );
  567.    try
  568.       if Windows.GetFullPathName( PChar( Path ), nBufferLength, lpBuffer, lpFilePart ) <> 0 then
  569.          Result := lpBuffer
  570.       else
  571.          Result := '';
  572.    finally
  573.       FreeMem( lpBuffer );
  574.    end;
  575. end;
  576.  
  577. function TtvAPIThing.myGetLogicalDrives : String;
  578. var
  579.    drives  : set of 0..25;
  580.    drive   : integer;
  581. begin
  582.    Result := '';
  583.    DWORD( drives ) := Windows.GetLogicalDrives;
  584.    for drive := 0 to 25 do
  585.       if drive in drives then
  586.          Result := Result + Chr( drive + Ord( 'A' ));
  587. end;
  588.  
  589. function TtvAPIThing.FindExecutable( const FileName : String ): String;
  590. var
  591.    lpResult : PChar;  // address of buffer for string for executable file on return
  592. begin
  593.    GetMem( lpResult, MAX_PATH + 1 );
  594.    try
  595.       if ShellAPI.FindExecutable( PChar( FileName ),
  596.                                   PChar( CurrentDirectory ),
  597.                                   lpResult ) > 32 then
  598.          Result := lpResult
  599.       else
  600.          Result := 'ERROR_FILE_NOT_FOUND';
  601.    finally
  602.       FreeMem( lpResult );
  603.    end; // try
  604. end;
  605.  
  606. procedure TtvAPIThing.myGetSystemInfo;
  607. var
  608.    SysInfo : TSystemInfo;
  609. begin
  610.    Windows.GetSystemInfo(SysInfo);
  611.  
  612.    with SysInfo do
  613.    begin
  614.       FPageSize      := dwPageSize;
  615.  
  616.       case dwProcessorType of
  617.          PROCESSOR_INTEL_386      : FProcessorType := '386';
  618.          PROCESSOR_INTEL_486      : FProcessorType := '486';
  619.          PROCESSOR_INTEL_PENTIUM  : FProcessorType := 'Pentium';
  620.          PROCESSOR_MIPS_R4000     : FProcessorType := 'MIPS';
  621.          PROCESSOR_ALPHA_21064    : FProcessorType := 'ALPHA';
  622.       end; // case dwProcessorType
  623.  
  624.       FNumberOfProcessors := dwNumberOfProcessors; 
  625.    end;
  626. end;
  627.  
  628. function TtvAPIThing.myGetVersion: String;
  629. var
  630.    VersionInfo : TOSVersionInfo;
  631.    OSName      : String;
  632. begin
  633.    // set the size of the record
  634.    VersionInfo.dwOSVersionInfoSize := SizeOf( TOSVersionInfo );
  635.  
  636.    if Windows.GetVersionEx( VersionInfo ) then
  637.       begin
  638.          with VersionInfo do
  639.          begin
  640.             case dwPlatformId of
  641.                VER_PLATFORM_WIN32s      : OSName := 'Win32s';
  642.                VER_PLATFORM_WIN32_WINDOWS : OSName := 'Windows 95';
  643.                VER_PLATFORM_WIN32_NT      : OSName := 'Windows NT';
  644.             end; // case dwPlatformId
  645.             Result := OSName + ' Version ' + IntToStr( dwMajorVersion ) + '.' + IntToStr( dwMinorVersion ) +
  646.                       #13#10' (Build ' + IntToStr( dwBuildNumber ) + ': ' + szCSDVersion + ')';
  647.          end; // with VersionInfo
  648.       end // if GetVersionEx
  649.    else
  650.       Result := '';
  651. end;
  652.  
  653. procedure TtvAPIThing.Loaded;
  654. begin
  655.    inherited Loaded;
  656.    myGetSystemInfo;
  657.    // Uncomment out the line below to make the nagging message go away
  658.    ShowMessage( 'This application is using a'#13#10'TtvAPIThing component created by'#13#10'Tim Victor'#13#10'tvictor@erols.com' );
  659. end;
  660.  
  661. procedure TtvAPIThing.FormatDrive( const Drive : Char );
  662. var
  663.   wDrive       : Word;
  664.   dtDrive      : TDriveType;
  665.   strDriveType : String;
  666. begin
  667.    // determine what type of drive is being
  668.    dtDrive := DriveType( Drive );
  669.    // if it's not a HDD or a FDD then raise an exception
  670.    if  ( dtDrive <> dtFloppy ) and ( dtDrive <> dtFixed ) then
  671.       begin
  672.          strDriveType := 'Cannot format a ';
  673.          case dtDrive of
  674.             dtUnknown : strDriveType := 'Cannot determine drive type';
  675.             dtNoDrive : strDriveType := 'Specified drive does not exist';
  676.             dtNetwork : strDriveType := strDriveType + 'Network Drive';
  677.             dtCDROM   : strDriveType := strDriveType + 'CD-ROM Drive';
  678.             dtRAM     : strDriveType := strDriveType + 'RAM Drive';
  679.          end; // case dtDrive
  680.  
  681.          raise Exception.Create( strDriveType + '.' );
  682.       end // if DriveType
  683.    else // proceed with the format
  684.       begin
  685.          wDrive := Ord( Drive ) - Ord( 'A' );
  686.          // SHFormatDrive is an undocumented API function
  687.          SHFormatDrive( Application.Handle, wDrive, $ffff, 0);
  688.       end; // else
  689. end;
  690.  
  691. function TtvAPIThing.myGlobalMemoryStatus( Index : Integer ): DWORD;
  692. var
  693.    MemoryStatus : TMemoryStatus;
  694. begin
  695.    with MemoryStatus do
  696.    begin
  697.       dwLength := SizeOf( TMemoryStatus );
  698.       Windows.GlobalMemoryStatus( MemoryStatus );
  699.       case Index of
  700.          1 : Result := dwMemoryLoad;
  701.          2 : Result := dwTotalPhys;
  702.          3 : Result := dwAvailPhys;
  703.          4 : Result := dwTotalPageFile;
  704.          5 : Result := dwAvailPageFile;
  705.          6 : Result := dwTotalVirtual;
  706.          7 : Result := dwAvailVirtual;
  707.          else Result := 0;
  708.       end; // case
  709.    end; // with MemoryStatus
  710. end;
  711.  
  712. function TtvAPIThing.DriveType( const Drive : Char ) : TDriveType;
  713. begin
  714.    Result := TDriveType(GetDriveType(PChar(Drive + ':\')));
  715. end;
  716.  
  717. procedure TtvAPIThing.ShutDown;
  718. const
  719.   SE_SHUTDOWN_NAME = 'SeShutdownPrivilege';   // Borland forgot this declaration
  720. var
  721.   hToken       : THandle;
  722.   tkp          : TTokenPrivileges;
  723.   tkpo         : TTokenPrivileges;
  724.   zero         : DWORD;
  725. begin
  726.   if OSVersion = 'Windows NT' then // we've got to do a whole buch of things
  727.      begin
  728.         zero := 0;
  729.         if not OpenProcessToken( GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken) then
  730.            begin
  731.              MessageBox( 0, 'Exit Error', 'OpenProcessToken() Failed', MB_OK );
  732.              Exit;
  733.            end; // if not OpenProcessToken( GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken)
  734.  
  735.         // SE_SHUTDOWN_NAME
  736.         if not LookupPrivilegeValue( nil, 'SeShutdownPrivilege' , tkp.Privileges[0].Luid ) then
  737.            begin
  738.               MessageBox( 0, 'Exit Error', 'LookupPrivilegeValue() Failed', MB_OK );
  739.               Exit;
  740.            end; // if not LookupPrivilegeValue( nil, 'SeShutdownPrivilege' , tkp.Privileges[0].Luid )
  741.         tkp.PrivilegeCount := 1;
  742.         tkp.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
  743.  
  744.         AdjustTokenPrivileges( hToken, False, tkp, SizeOf( TTokenPrivileges ), tkpo, zero );
  745.         if Boolean( GetLastError() ) then
  746.            begin
  747.               MessageBox( 0, 'Exit Error', 'AdjustTokenPrivileges() Failed', MB_OK );
  748.               Exit;
  749.            end // if Boolean( GetLastError() )
  750.         else
  751.            ExitWindowsEx( EWX_FORCE or EWX_SHUTDOWN, 0 );
  752.       end // if OSVersion = 'Windows NT'
  753.    else
  754.       begin // just shut the machine down
  755.         Windows.ExitWindows( 0, 0 );
  756.       end; // else
  757. end;
  758.  
  759. procedure Register;
  760. begin
  761.    RegisterComponents( 'Samples', [TtvAPIThing] );
  762. end;
  763.  
  764. end.
  765.