如何判断光驱是CD还是DVD
// 0: Unknown
// FILE_DEVICE_CD_ROM: CD-ROM
// FILE_DEVICE_DVD : DVD-ROM
function GetCDType(Drive: Char): DEVICE_TYPE;
const Size = 2048;
var
hDrive: THandle;
dwBytesReturned : DWORD;
bMediaTypes : array[0..Size-1] of Byte;
rMediaTypes : TGetMediaTypes absolute bMediaTypes;
begin
Result := 0;
hDrive := CreateFile(PChar('\\.\' + Drive + ':'), GENERIC_READ,
FILE_SHARE_READ, nil, OPEN_EXISTING, 0, 0);
if hDrive = INVALID_HANDLE_VALUE then Exit;
if DeviceIoControl( hDrive, IOCTL_STORAGE_GET_MEDIA_TYPES_EX, nil, 0,
@bMediaTypes, Size, dwBytesReturned, nil ) then
with rMediaTypes do
if MediaInfoCount > 0 then Result := DeviceType;
CloseHandle(hDrive);
end;
其中DEVICE_TYPE/TGetMediaTypes都定义在 IoCtl.pas 里
其实在 IoCtl.pas 里有个方法 GetMediaType,返回的是 STORAGE_MEDIA_TYPE,值有N多,比如
RemovableMedia = 11; // Removable media other than floppy
FixedMedia = 12; // Fixed hard disk media
...
CD_ROM = 51; // Opt_Disk - CD
CD_R = 52; // Opt_Disk - CD-Recordable (Write Once)
CD_RW = 53; // Opt_Disk - CD-Rewriteable
DVD_ROM = 54; // Opt_Disk - DVD-ROM
DVD_R = 55; // Opt_Disk - DVD-Recordable (Write Once)
DVD_RW = 56; // Opt_Disk - DVD-Rewriteable
应该可以更详细的确定驱动器类型,但是我测试下来发现我机器的DVD-RW被认成CD_ROM
不知道什么问题,有时间再研究下