Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Help needed with GetDriveType Function please

769 views
Skip to first unread message

ZahirX

unread,
Jul 6, 1999, 3:00:00 AM7/6/99
to
Can somoene please give me an example on how to use the GetDriveType
function?

The help says

UINT GetDriveType(

LPCTSTR lpRootPathName // address of root path
);

but I have no idea how to use LPCTSTR lpRootPathName as I am sort of new. I
am main interesting in finding the computers defaulted CD-ROM.

Thanks


Reid Roman

unread,
Jul 9, 1999, 3:00:00 AM7/9/99
to
The function returns an Integer which represents a Constant.

The function takes a PChar data type (null terminated string). SO
basically you convert your
pascal string to a PChar ...

PChar('C:');

Here is something I got from Lou's Delphi tip of the day site;
http://members.truepath.com/delphi/

Knowing the drive type can be useful if you need to test if a removable
or remote drive is ready. Tomorrow I will do a tip on testing if the
drive is ready, today we will look at what the drive types are. In this
tip we will be displaying the drive types in a TListBox according to
their type as returned from the WIN32 API call GetDriveType(). We will
test the return value within a case statement where you can manipulate
the code to your needs. We will be using the WIN32 API call
GetDriveType() and Delphi's Ord() and Chr()
functions.

First we will need a TListbox and a TButton and we will need to create a
TButton.OnClick event. You can easily create this event through the
Object Inspector Events tab.
Notice that in this routine we will be using a for..to..do loop from the
ordinal value of A to the ordinal value of Z. To get the ordinal values
we use Delphi's Ord() function. Then we use the same value to set the
drive letter string by using Delphi's Chr() function to get the
character representation of the local looping variable, x.
After we set the drive letter we use it in the WIN32 API call
GetDriveType() and then use the return value of this function call to
set our drive string to be added to our TListBox.
GetDriveType() function determines whether a disk drive is a removable,
fixed, CD-ROM, RAM
disk, or network drive. The only parameter is then drive letter to check
the type of in the format A:\.
The return values are as follows:
0 cannot be determined.
1 root directory does not exist.
DRIVE_REMOVABLE can be removed from the drive.
DRIVE_FIXED cannot be removed from the drive.
DRIVE_REMOTE a remote (network) drive.
DRIVE_CDROM a CD-ROM drive.
DRIVE_RAMDISK a RAM disk.
Ord() returns the ordinal value of an ordinal-type expression. The only
parameter is the ordical position to return for.

Chr() returns the character for a specified ASCII value. The only
parameter is the ordinal value (ASCII value) of the byte-type
expression.

Example 1


{...}

type
TForm1 = class(TForm)
Button1: TButton;
ListBox1: TListBox;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

{...}

procedure TForm1.Button1Click(Sender: TObject);
var
x,
DrvType : Integer;
DrvLetter,
DrvString : String;
begin
{ 25 possible drives ... a-z }
for x := Ord('A') to Ord('Z') do
begin

{ set drive letter to be used in GetDriveType }
DrvLetter := Chr(x)+':\';

{ get the drive type }
DrvType := GetDriveType(pChar(DrvLetter));

{ set our drive type string accordingly }
case DrvType of
0,1 : DrvString := '';
DRIVE_REMOVABLE : DrvString := 'Removable';
DRIVE_FIXED : DrvString := 'Fixed';
DRIVE_REMOTE : DrvString := 'Network';
DRIVE_CDROM : DrvString := 'CD-ROM';
DRIVE_RAMDISK : DrvString := 'RAM disk';
end;

{ add drivetype string if it is not blank
according to the above case statement }
if DrvString <> '' then
Listbox1.Items.Add(DrvLetter +
' = ' + DrvString);
end;
end;

{...}

</END>


HTH

Rkr

C8591

unread,
Jul 10, 1999, 3:00:00 AM7/10/99
to
>Can somoene please give me an example on how to use the GetDriveType
>function?

procedure GetDriveTypes(Items: TStrings; DrvType: longint);
var
i : char;
s : string;
x : longint;
begin
{ 0 The drive type cannot be determined.
1 The root directory does not exist.
DRIVE_REMOVABLE The drive can be removed from the drive.
DRIVE_FIXED The disk cannot be removed from the drive.
DRIVE_REMOTE The drive is a remote (network) drive.
DRIVE_CDROM The drive is a CD-ROM drive.
DRIVE_RAMDISK The drive is a RAM disk. }

Items.Clear;
for i := 'A' to 'Z' do begin
s := i + ':\'; //must be in form of 'x:\' not 'x:'
x := GetDriveType(PChar(s));
s := i + ': ';
case x of
0: s := s + ' The drive type cannot be determined.';
1: s := s + ' The root directory does not exist.';
DRIVE_REMOVABLE: s := s + ' The drive can be removed from the drive.';
DRIVE_FIXED: s := s + ' The disk cannot be removed from the drive.';
DRIVE_REMOTE: s := s + ' The drive is a remote (network) drive.';
DRIVE_CDROM: s := s + ' The drive is a CD-ROM drive.';
DRIVE_RAMDISK: s := s + ' The drive is a RAM disk.';
end;
if x <> 1 then begin
if DrvType < 0 then Items.AddObject(s, Pointer(longint(x)))
else if DrvType = x then Items.AddObject(s, Pointer(longint(x)));
end
end;
end;

//Example use

procedure TForm1.Button1Click(Sender: TObject);
begin
ListBox1.Clear;
GetDriveTypes(ListBox1.Items, DRIVE_CDROM); // -1 = All Drives
end;

procedure TForm1.ListBox1Click(Sender: TObject);
var
s : string;
begin
case longint(ListBox1.Items.Objects[ListBox1.ItemIndex]) of
0: s := 'DRIVE_UNKNOWN';
1: s := 'DRIVE_PATH_ERROR';
DRIVE_REMOVABLE: s := 'DRIVE_REMOVABLE';
DRIVE_FIXED: s := 'DRIVE_FIXED';
DRIVE_REMOTE: s := 'DRIVE_REMOTE';
DRIVE_CDROM: s := 'DRIVE_CDROM';
DRIVE_RAMDISK: s := 'DRIVE_RAMDISK';
else s := '';
end;
Label1.Caption := s;
end;

Chris


0 new messages