如何判斷光碟機是CD還是DVD

來源:互聯網
上載者:User

// 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
不知道什麼問題,有時間再研究下

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.