Newsgroups : Borland : borland.public.delphi.nativeapi.win32 : 2007 Mar : Preventing CD/DVD Eject

www.cryer.info
Managed Newsgroup Archive

Preventing CD/DVD Eject

Subject:Preventing CD/DVD Eject
Posted by:"Mike Warren" (miwa-not-this-b..@or-this-csas.net.au)
Date:19 Mar 2007 18:48:11

I have used the following code for the last couple of years to lock the
CD/DVD drawer. I have a machine at the moment where this function
fails. No error is returned but it is still possible to eject the CD.

I've tried calling the function several times but it still fails to
lock the drive.

If I fit a different model DVD drive it works fine.

If I exit and restart my program the drive becomes locked.

Am I going nuts?


function PreventEjection(Drive: Char; Prevent: Boolean): Boolean;
const
  IOCTL_STORAGE_MEDIA_REMOVAL = $002d4804;
  IsPrevented: Boolean = False;
var
  zDeviceName: array [0..15] of Char;
  Device: THandle;
  BufIn: TPREVENT_MEDIA_REMOVAL;
  BytesReturned: DWORD;
begin
  Result := False;
  if Prevent then
    AddToDebugLog('Disabling eject on drive ' + Drive + ':')
  else
    AddToDebugLog('Enabling eject on drive ' + Drive + ':');
  FillChar(zDeviceName, Length(zDeviceName), #0);
  StrPCopy(zDeviceName, '\.\' + Drive + ':');
  Device := CreateFile(zDeviceName, GENERIC_READ,
    FILE_SHARE_READ or FILE_SHARE_WRITE, nil, OPEN_EXISTING,
    FILE_ATTRIBUTE_NORMAL, 0);

  if Device = INVALID_HANDLE_VALUE then
    AddToDebugLog('[ERROR] Invalid drive handle')
  else begin
    BufIn.PreventMediaRemoval := Prevent;
    Result := DeviceIOControl(Device,
      IOCTL_STORAGE_MEDIA_REMOVAL, @BufIn,
      SizeOf(BufIn), nil, 0, BytesReturned, nil);
    CloseHandle(Device);
  end;

  if not Result then
   AddToDebugLog('[ERROR] OS Error ' + SysErrorMessage(GetLastError));
end;

--
-Mike

Replies:

www.cryer.info
Managed Newsgroup Archive