如何检测出光驱驱动器号?(20分)

  • 主题发起人 主题发起人 strang
  • 开始时间 开始时间
检测是否是CDROM:

Function IsCDROM(Drv : Char):BOOLEAN;
Var
CDR : string;
cnt : byte;
Count,First : word;
begin
Result := false;
CDR := '';
asm
mov ax, 1500h
xor bx, bx
int $2f //CDROM中断
les di, COUNT
mov es:[di], bx
les di, FIRST
mov es:[di], cx
end;
if Count > 0 then
for cnt := 0 to (Count-1) do
CDR := CDR + char(First + Byte('A') + cnt);
Result := (Pos(upcase(Drv),CDR) > 0);
end;
 
Win95下可以用代码如下:

TDriveType = (dtUnknown, dtNoDrive, dtFloppy, dtFixed, dtNetwork, dtCDROM,
dtRAM);

function isCDROM(DriveNum:Integer):Boolean;
var
DriveChar: Char;
DriveType: TDriveType;
DriveBits: set of 0..25;
begin
result := False;
Integer(DriveBits) := GetLogicalDrives;
if (DriveNum in DriveBits) then
begin
DriveChar := Char(DriveNum + Ord('A'));
DriveType := TDriveType(GetDriveType(PChar(DriveChar + ':/')));
if DriveType = dtCDROM then
result:=True;
end;
end;

function GetCDROMNumber:integer;
var
DriveNum: Integer;
DriveChar: Char;
DriveType: TDriveType;
DriveBits: set of 0..25;
begin
result := 0;
Integer(DriveBits) := GetLogicalDrives;
for DriveNum := 2 to 25 do
begin
if not (DriveNum in DriveBits) then Continue;
DriveChar := Char(DriveNum + Ord('A'));
DriveType := TDriveType(GetDriveType(PChar(DriveChar + ':/')));
if DriveType = dtCDROM then
Inc(result);
end;
end;
 
很简单,用GetDriveType函数就可以。
下面是一个例子:将所有光驱盘符放进ListBox中。

unit Unit1;

interface

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

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

var
Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
var
i:integer;
s:string;
begin
for i:=0 to 23 do begin
s := Chr(67+i)+':/';
if GetDriveType(PChar(s))= DRIVE_CDROM then
ListBox1.Items.Add(s);
end;
end;

end.
 
后退
顶部