Mega Code Archive

 
Categories / Delphi / Multimedia
 

How to detect an installed cd recorder

Title: How to detect an installed cd recorder? Question: If you've ever tried to detect an installed CD recorder/burner on the system where you app is running, you may stumbled over the WinAPI method "GetDriveType" and wondered, why this doesn't work. Read this short article to get a simple solution, how to solve this 8-) Answer: NOTE: This solutions ONLY WORKS on Windows XP or Windows VISTA! WindowsXP & VISTA supported a new API called IMAPI which included functionality to retrieve specific informations about installed CD/DVD recorders. This API also supports direct simulating & burning, erasing and creating multiseasion CD's. This article uses a SMALL SUBSET of it's functionality to detect an installed CD/DVD recorder, retrieve it's drive letter and call the CD assistant with only a few lines of code. What you need =============== Be sure to include the unit "COMObj" in your uses clause - that's it! Wrapping CDBurn interface =========================== To get access of the methods implemented by this interface, you need to wrap it's class id to a published interface: type ICDBURN = interface( IUnknown ) ['{3d73a659-e5d0-4d42-afc0-5121ba425c8d}'] function GetRecorderDriveLetter(var StrDrive: WideChar; CharCount: uInt): HResult; stdcall; function Burn(hWindow: HWND): HResult; stdcall; function HasRecordableDrive(out pfHasRecorder: Bool): HResult; stdcall; end; This interface must be mapped to a COMObject with a specific class id: const CLSID_XPDICBURN: TGUID = '{fbeb8a05-beee-4442-804e-409d6c4515e9}'; The whole code for this mapping could looks like this: var IICDBurn: ICDBURN; begin try IICDBurn := CreateCOMObject(CLSID_XPDICBURN) as ICDBURN; except { catch all errors } end; // try/except end; Accessing the COMObject ========================= As you've may noticed, the ICDBURN interface supports three methods to detect a recorder, retrieve it's drive letter and force the assistant. Note: NEVER change the order of these methods! It's a mapper to an already existing system COMObject! Here's a small method to check if a recorder is installed and to retrieve it's driveletter: function xpCDRAvailable(out DriveLetter: string): Boolean; var HasRecorder: Bool; IICDBurn: ICDBURN; PDriveLetter: PWideChar; DriveLetters: array[0..3] of WideChar; begin Result := False; DriveLetter := ''; PDriveLetter := @DriveLetters; try IICDBurn := CreateCOMObject(CLSID_XPDICBURN) as ICDBURN; IICDBurn.HasRecordableDrive(HasRecorder); if (HasRecorder) and (IICDBurn.GetRecorderDriveLetter(PDriveLetter^, SizeOf(DriveLetters)) = S_OK) then begin Result := True; DriveLetter := string(PDriveLetter); end; except { catch all errors } end; // try/except end; ...and here to force the CD assistant: function xpCDRBurn: Boolean; var DriveLetter: string; IICDBurn: ICDBURN; begin Result := xpCDRAvailable(DriveLetter); if not(Result) then Exit; try IICDBurn := CreateCOMObject(CLSID_XPDICBURN) as ICDBURN; IICDBurn.Burn(0); except { catch all errors } end; // try/except end; That's all! If you need more informations about how to prepare files to be burned by Windows, please let me know and I'll write an additional article 8-) Regards,...