Subject: Ejecting CD-ROM 86. ***** Q: How do I eject a CD-ROM using a Turbo Pascal program? A: The code for the ejection is given below. Note that it needs the MSCDEXFN function from the previous FAQ item. uses Dos; {} procedure EJECT (charDev : string; var ok : boolean; var errCode : word); var regs : registers; cdrom : file; cdCtrlBlock : byte; { CD-ROM Control Block } handle : ^word; { Handle referencing CD-ROM driver } begin Assign (cdrom, charDev); { Character device for CD-ROM driver } {$I-} Reset (cdrom); {$I+} { Tackle errors yourself } if IOresult <> 0 then begin { Exit if file not found } ok := false; errCode := $FFFF; { Your own arbitrary error code } exit; end; FillChar (regs, SizeOf(regs), 0); { Just to make sure } regs.ax := $4403; { Function $44, subfunction $03 } handle := @cdrom; { Establish the file handle } regs.bx := handle^; FillChar(CdCtrlBlock, SizeOf(CdCtrlBlock), 0); CdCtrlBlock := $00; { $00 eject disk; $05 close tray } regs.ds := Seg(CdCtrlBlock); { ds:dx CD-ROM control block } regs.dx := Ofs(CdCtrlBlock); MsDos (regs); { Call interrupt $21 } {$I-} Close (cdrom); {$I+} ok := regs.flags and FCarry = 0; { Success or not? } errCode := regs.ax; { $01 = invalid function } end; { $05 = access denied } {} { $06 = invalid handle } procedure TEST; { $0D = invalid data } var ok : boolean; code : word; begin EJECT ('K', ok, code); if ok then writeln ('Success') else writeln ('Error ', code); end; {} begin TEST; end. My thanks are due to Miro Wikgren (wikgren@cc.helsinki.fi) who pointed out that the "handle referencing character device for CD-ROM driver" must be the name given when the CD-ROM driver is loaded in CONFIG.SYS and AUTOEXEC.BAT. I could not solve this problem without that help in comp.lang.pascal.borland. In fact the previous FAQ item was tackled only after the current FAQ item had been solved first. A slightly different approach to the file handle by Miro var cdrom : text; { CD-ROM is a character device } handle : word; { Handle: word, not a pointer } : handle := TextRec(cdrom).handle; { Use TP help for more on this } regs.bx := handle; : Another solution can be found in 3427 Mar 15 18:35 ftp://garbo.uwasa.fi/pc/turbopas/cdtips01.zip cdtips01.zip Eject/Close/Lock/Unlock CD-ROM in TP for Win95, C.Rankin --------------------------------------------------------------------