2007. április 4., szerda

On which storage is you application?


Problem/Question/Abstract:

Sometimes, when you develop a software you need to disable the execution of the code from certain types of media, for example, if your application uses a database file, you can't write on it if it's located on a CR-ROM.
How to manage this in a easy way? There's the solution.

Answer:

Just write down these short routines:

function IsOnHDD: boolean;
begin
  result := GetDriveType(pChar(uppercase(copy(ParamStr(0), 1, 3)))) = DRIVE_FIXED;
end;

function IsOnCD: boolean;
begin
  result := GetDriveType(pChar(uppercase(copy(ParamStr(0), 1, 3)))) = DRIVE_CDROM;
end;

function IsOnRemoveable: boolean;
begin
  result := GetDriveType(pChar(uppercase(copy(ParamStr(0), 1, 3)))) = DRIVE_REMOVABLE;
end;

The use them in your project file (.DPR) like this:

program Project1;

uses
  Windows, // Added manually
  SysUtils, // Added manually
  Dialogs, // Added manually
  Forms,
  Unit1 in 'Unit1.pas' {Form1};

{$R *.RES}

function IsOnCD: boolean;
begin
  result := GetDriveType(pChar(uppercase(copy(ParamStr(0), 1, 3)))) = DRIVE_CDROM;
end;

begin
  Application.Initialize;
  if IsOnCD then
  begin
    ShowMessage('This program cannot be executed from a CD-ROM drive.');
    Application.Terminate;
  end
  else
  begin
    Application.CreateForm(TForm1, Form1);
    Application.Run;
  end;
end.

This program will not start if located on a CD-ROM. And no other code than the necessary one will be executed.

Christian Cristofori

Nincsenek megjegyzések:

Megjegyzés küldése