Author: Christian Cristofori
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:
1 function IsOnHDD: boolean;
2 begin
3 result := GetDriveType(pChar(uppercase(copy(ParamStr(0), 1, 3)))) = DRIVE_FIXED;
4 end;
5
6 function IsOnCD: boolean;
7 begin
8 result := GetDriveType(pChar(uppercase(copy(ParamStr(0), 1, 3)))) = DRIVE_CDROM;
9 end;
10
11 function IsOnRemoveable: boolean;
12 begin
13 result := GetDriveType(pChar(uppercase(copy(ParamStr(0), 1, 3)))) =
14 DRIVE_REMOVABLE;
15 end;
The use them in your project file (.DPR) like this:
16 program Project1;
17
18 uses
19 Windows, // Added manually
20 SysUtils, // Added manually
21 Dialogs, // Added manually
22 Forms,
23 Unit1 in 'Unit1.pas' {Form1};
24
25 {$R *.RES}
26
27 function IsOnCD: boolean;
28 begin
29 result := GetDriveType(pChar(uppercase(copy(ParamStr(0), 1, 3)))) = DRIVE_CDROM;
30 end;
31
32 begin
33 Application.Initialize;
34 if IsOnCD then
35 begin
36 ShowMessage('This program cannot be executed from a CD-ROM drive.');
37 Application.Terminate;
38 end
39 else
40 begin
41 Application.CreateForm(TForm1, Form1);
42 Application.Run;
43 end;
44 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
|