怎样用软件操作光驱 ?(50分)

  • 主题发起人 主题发起人 wangrui
  • 开始时间 开始时间
W

wangrui

Unregistered / Unconfirmed
GUEST, unregistred user!
怎样用软件 打开/关闭 光驱 ?
 
在Use 中添加 MMsystem


//Open
begin
mcisendstring('set cdaudio door open wait',nil,o,handle);
end
//close
begin
mcisendstring('set cdaudio door closed wait',nil,o,handle);
end
 
怎样得知光驱的当前状态呢 ?
 
检测CD-ROM或是其他磁盘是否有过变化。

  最简单的检查CD-ROM或是磁盘是否有过变化的方法
是检查其volume号码。你可以简单地运用下面的函数来返
回磁盘的volume系列号码GetDiskVolSerialID(′E′),
函数代码如下:
  function GetDiskVolSerialID(cDriveName:char)

  var
  dwTemp1,dwTemp2:DWord;
Result:DWord;
  begin
  GetVolumeInformation(PChar(cDriveName+′:
/′),
  Nil,
 0,
  Result,
  dwTemp2,
  dwTemp2,
  Nil,
  0);
  end;
 
还有没有别的方法 ?
 
Close: mciSendString('Set cdaudio door open', nil, 0, handle);

Open: mciSendString('Set cdaudio door closed', nil, 0, handle);


当光盘放进(出)后,有个WM_DEVICECHANGE消息,可以通过这个消息来检测光驱的状态。
 
一下子都说完了:-(Frequently Asked Questions

How can I eject a CD-ROM in code?


Question:


How can I eject a CD-ROM in code?

Answer:


You can use the Windows API function GetDriveType() to test

if the drive is a CD-ROM drive then use the TMediaPlayer

(even if the CD is not an Audio CD) to eject the CD.



Example:



function IsDriveCD(Drive : char) : longbool;

var

DrivePath : string;

begin

DrivePath := Drive + ':/';

result := LongBool(GetDriveType(PChar(DrivePath)) and DRIVE_CDROM);

end;



function EjectCD(Drive : char) : bool;

var

mp : TMediaPlayer;

begin

result := false;

Application.ProcessMessages;

if not IsDriveCD(Drive) then exit;

mp := TMediaPlayer.Create(nil);

mp.Visible := false;

mp.Parent := Application.MainForm;

mp.Shareable := true;

mp.DeviceType := dtCDAudio;

mp.FileName := Drive + ':';

mp.Open;

Application.ProcessMessages;

mp.Eject;

Application.ProcessMessages;

mp.Close;

Application.ProcessMessages;

mp.free;

result := true;

end;



procedure TForm1.Button1Click(Sender: TObject);

begin

if not EjectCD('D') then

ShowMessage('Not A CD Drive');

end;

Hope this helps
 
to wangrui:
是不是该结束问题了?
 
{ 贴 一 贴}
const
DBT_DEVICEARRIVAL = $8000; // system detected a new device
DBT_DEVICEREMOVECOMPLETE = $8004; // device is gone
DBT_DEVTYP_VOLUME = $00000002; // logical volume
DBTF_MEDIA = $0001; // media comings and goings
type
PDEV_BROADCAST_HDR = ^TDEV_BROADCAST_HDR;
TDEV_BROADCAST_HDR = packed record
dbch_size : DWORD;
dbch_devicetype : DWORD;
dbch_reserved : DWORD;
end;

PDEV_BROADCAST_VOLUME = ^TDEV_BROADCAST_VOLUME;
TDEV_BROADCAST_VOLUME = packed record
dbcv_size : DWORD;
dbcv_devicetype : DWORD;
dbcv_reserved : DWORD;
dbcv_unitmask : DWORD;
dbcv_flags : WORD;
end;

{设置一钩子可知光驱的当前状态。}

procedure TCDEvents.WMDeviceChange(var Msg : TWMDeviceChange);
var lpdb : PDEV_BROADCAST_HDR;
lpdbv : PDEV_BROADCAST_VOLUME;
begin
(* received a wm_devicechange message *)
lpdb := PDEV_BROADCAST_HDR(Msg.dwData);
(* look at the event send together with the wm_devicechange message *)
case Msg.Event of
DBT_DEVICEARRIVAL : begin
if lpdb^.dbch_devicetype = DBT_DEVTYP_VOLUME then begin
lpdbv := PDEV_BROADCAST_VOLUME(Msg.dwData);
if (lpdbv^.dbcv_flags and DBTF_MEDIA) = 1 then
Application.MessageBox (' CDROM closed.',
'CDROM Information.',
MB_OK);
end;
end;
DBT_DEVICEREMOVECOMPLETE : begin
if lpdb^.dbch_devicetype = DBT_DEVTYP_VOLUME then begin
lpdbv := PDEV_BROADCAST_VOLUME(Msg.dwData);
if (lpdbv^.dbcv_flags and DBTF_MEDIA) = 1 then
if Assigned(fAfterRemove) then
Application.MessageBox (' CDROM Open.',
'CDROM Information.',
MB_OK);
end;
end;
end;
end;

{ get Handle to a CD-player}

function TCDEvents.GetDevice : word;
var OpenParms : TMCI_Open_Parms;
FID : Word;
begin
if not (csDesigning in ComponentState) then begin
if fID=0 then begin
FFlags := 0;
FFlags := mci_notify or mci_open_type or mci_open_shareable;
OpenParms.lpstrDeviceType := 'CDAudio';
OpenParms.dwCallback := 0;
fErrCode := mciSendCommand(0, mci_open, FFlags, Longint(@OpenParms));
if FErrCode = 0 then {device successfully opened}
begin
fID := OpenParms.wDeviceID;
end;
end;
Result := fID;
end;
end;
{ Eject CDROM}
procedure TCDEvents.OpenDoor;
var
SetParms: TMCI_Set_Parms;
begin
FFlags := 0;
FFlags := mci_notify or mci_set_door_open;
SetParms.dwCallback := 0;
fErrCode := mciSendCommand(GetDevice, mci_Set, FFlags, Longint(@SetParms));
end;
{Close CDROM}
procedure TCDEvents.CloseDoor;
var
SetParm: TMCI_Set_Parms;
begin
FFlags := 0;
FFlags := mci_notify or mci_set_door_closed;
SetParm.dwCallback := 0;
fErrCode := mciSendCommand( GetDevice, mci_Set, FFlags, Longint(@SetParm) );
end;
 
; 十分抱歉,前几天一直考试,没时间来看看,谢谢大家了 !
 
后退
顶部