Wednesday, January 10, 2007


SytemInfo

Mostra algunes propietats de sistema operatiu.

unit SystemInfo;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;

type
  TSystemInfo = class(TComponent)
  private
    { Private declarations }
    FShortDate: string;
    FLongDate: string;
    FTime: string;
    FComputerName: string;
    FUserName: string;
    FCDRomDrive: string;
    function GetFirstCdRomDrive: string;
  protected
    { Protected declarations }
  public
    { Public declarations }
    constructor Create(AOwner:TComponent); override;
  published
    { Published declarations }
    property ShortDate: string read FShortDate write FShortDate;
    property LongDate: string read FLongDate write FLongDate;
    property Time: string read FTime write FTime;
    property ComputerName: string read FComputerName write FComputerName;
    property UserName: string read FUserName write FUserName;
    property CDRomDrive: string read FCDRomDrive write FCDRomDrive;
  end;

procedure Register;

implementation

function TSystemInfo.GetFirstCdRomDrive: string;
var
  r: LongWord;
  Unidades: array[0..128] of char;
  pUnidad: pchar;
begin
  Result := '';
  r := GetLogicalDriveStrings(sizeof(Unidades), Unidades);
  if r = 0 then exit;
  if r > sizeof(Unidades) then
    raise Exception.Create(SysErrorMessage(ERROR_OUTOFMEMORY));
  pUnidad := Unidades; // Apunta a la primera unidad
  while pUnidad^ <> #0 do begin
    if GetDriveType(pUnidad) = DRIVE_CDROM then begin
      Result := pUnidad;
      exit;
    end;
    inc(pUnidad, 4); // Apunta a la siguiente unidad
  end;
end;

constructor TSystemInfo.Create(AOwner:TComponent);
var
  Buf: array[0..99] of char;
  size: dword;
begin
  inherited;
  GetLocaleInfo(LOCALE_USER_DEFAULT,LOCALE_SSHORTDATE,@Buf,100);
  FShortDate := Buf;
  GetLocaleInfo(LOCALE_USER_DEFAULT,LOCALE_SLONGDATE,@Buf,100);
  FLongDate := Buf;
  GetLocaleInfo(LOCALE_USER_DEFAULT,LOCALE_STIMEFORMAT,@Buf,100);
  FTime := Buf;
  size := 100;
  GetComputerName(Buf, size);
  FComputerName := Buf;
  GetUserName(Buf, size);
  FUserName := Buf;
  FCDRomDrive := GetFirstCdRomDrive;
end;

procedure Register;
begin
  RegisterComponents('Components Delphi', [TSystemInfo]);
end;

end.
 

La propietat CDRomDrive ens indica la unitat del CD. ComputerName ens dona el nom de la màquina. LongData i ShortDate els formats de data del sistam. Time el format de temps del sistema. Finalment, User, l'usuari actual del sistema operatiu. Lògicament es poden anar afegint tantes propietats com es vulgui per obtenir dades del sistema.

Comments: Post a Comment



<< Home

This page is powered by Blogger. Isn't yours?