弹出/关闭光驱
//打开光驱
mciSendString('Set cdaudio door open wait', nil, 0, handle);
//关闭光驱
mciSendString('Set cdaudio door closed wait', nil, 0, handle)
如果要打开指定的光驱,请使用下面的代码:
library ctlcdrom;
uses
Windows, MMSystem;
{$R *.res}
type
TCDRomOperate = (cdOpen, cdClose);
TCDStatus = (csOpen, csClosed);
function OperateCDROM(DriveLetter: Char; Operate: TCDRomOperate): Boolean;
var
OpenParm: TMCI_OPEN_Parms;
Flags: DWORD;
Drive: string;
DeviceID: Word;
Err: Cardinal;
begin
Drive := DriveLetter + ':';
Flags := mci_Open_Type or mci_Open_Element;
with OpenParm do
begin
dwCallBack := 0;
lpstrDeviceType := 'CDAudio';
lpstrElementName := PChar(Drive);
end;
Result := mciSendCommand(0, mci_Open, Flags, Longint(@OpenParm)) = 0;
if Result then
begin
DeviceID := OpenParm.wDeviceID;
try
case Operate of
cdOpen :
Result := mciSendCommand(DeviceID, MCI_SET, MCI_SET_DOOR_OPEN, 0) = 0;
cdClose:
Result := mciSendCommand(DeviceID, MCI_SET, MCI_SET_DOOR_CLOSED, 0) = 0;
end;
finally
mciSendCommand(DeviceID, mci_Close, Flags, Longint(@OpenParm));
end;
end;
end;
function OpenCDROM(Drive: Char): Boolean; stdcall; // 打开CDROM
begin
Result := OperateCDROM(Drive, cdOpen);
end;
function CloseCDROM(Drive: Char): Boolean; stdcall; // 关闭CDROM
begin
Result := OperateCDROM(Drive, cdClose);
end;
exports //dll文件导出的函数列表
OpenCDROM,
CloseCDROM;
begin
end.
---------------------------------------
{$WARN SYMBOL_PLATFORM OFF}
const
FILE_DEVICE_MASS_STORAGE = $2D;
METHOD_BUFFERED = 0;
FILE_ANY_ACCESS = 0;
FILE_READ_ACCESS = 1;
FILE_WRITE_ACCESS = 2;
IOCTL_STORAGE_EJECT_MEDIA = (FILE_DEVICE_MASS_STORAGE shl 16) OR
(FILE_READ_ACCESS shl 14) OR
($202 shl 2) OR
(METHOD_BUFFERED);
IOCTL_STORAGE_LOAD_MEDIA = (FILE_DEVICE_MASS_STORAGE shl 16) OR
(FILE_READ_ACCESS shl 14) OR
($203 shl 2) OR
(METHOD_BUFFERED);
function IsCDDrive( const PPhysicalDrive: String ): Boolean;
var
LDrive : String;
begin
Result := false;
LDrive := Trim( PPhysicalDrive );
if ( LDrive <> '' ) then
begin
if ( Pos( ':', LDrive ) <= 0 ) then
LDrive := LDrive + ':';
LDrive := IncludeTrailingBackSlash( LDrive );
Result := ( ( GetDriveType( @LDrive[1] ) and DRIVE_CDROM ) =
DRIVE_CDROM );
end; //if
end; //IsCDDrive
function Perform_CDAction( const PPhysicalDrive: String; const PControlCode:
Cardinal ): Boolean;
var
LByteCount : Cardinal;
LCD_Handle : THandle;
LDrive : String;
begin
Result := false;
LDrive := Trim( PPhysicalDrive );
if ( LDrive <> '' ) then
begin
if ( Pos( ':', LDrive ) <= 0 ) then
LDrive := LDrive + ':';
LDrive := ExcludeTrailingBackSlash( LDrive );
if IsCDDrive( LDrive ) then
begin
LCD_Handle := CreateFile( @Format( ' \\.\%s ', [LDrive] )[1],
GENERIC_READ, 0, nil, OPEN_EXISTING, 0, 0 );
if ( LCD_Handle <> INVALID_HANDLE_VALUE ) then
try
Result := DeviceIOControl( LCD_Handle, PControlCode, nil, 0, nil,
0, LByteCount, nil );
finally
CloseHandle( LCD_Handle );
end; //try-finally
end; //IsCDDrive
end; //if
end; //Perfrom_CDAction
function Eject_CD( const PPhysicalDrive: String ): Boolean;
begin
Result := Perform_CDAction( PPhysicalDrive, IOCTL_STORAGE_EJECT_MEDIA );
end; //Eject_CD
function Close_CD( const PPhysicalDrive: String ): Boolean;
begin
Result := Perform_CDAction( PPhysicalDrive, IOCTL_STORAGE_LOAD_MEDIA );
end; //Close_CD
example usage:
if not Eject_CD( 'D:' ) then
ShowMessage( Format( 'Eject Failed: %d', [GetLastError()] ) );
if not Close_CD( 'D:' ) then
ShowMessage( Format( 'Close CD Failed: %d', [GetLastError()] ) );