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