Page 1 of 1

Prevent DVD ROM tray opening?

Posted: Sun Jul 22, 2007 6:28 am
by PB
Is there a way to programatically do this, so that if the Eject button on the
CD ROM drive is pressed, the door stays closed and doesn't respond/open?

Posted: Sun Jul 22, 2007 10:13 am
by LuCiFeR[SD]
this may give you a few idea's PB...

http://support.microsoft.com/default.aspx/kb/165721

I know this is doing something slightly different to what you want, but read between the lines and you should see that everything you require is there :)

Posted: Sun Jul 22, 2007 10:18 am
by PB
Thanks, I'll take a look and see if can do what I want.

Also, I found this app which does exactly what I want:

http://www.cdrom-lock.de/index_en.html

And it's GPL so it should have the source somewhere. (It's not included
so I'll have to ask the author for it).

Posted: Sun Jul 22, 2007 10:21 am
by LuCiFeR[SD]
Yeah, I found that too... good luck mate :) To be fair though, doesn't look mega difficult hehe... should be a fun small project for ya :)

[edit]the source is available via the sourceforge link in the download section[/edit]

Posted: Sun Jul 22, 2007 12:00 pm
by milan1612
LuCiFeR[SD] wrote:[edit]the source is available via the sourceforge link in the download section[/edit]
Download

Posted: Sun Jul 22, 2007 1:58 pm
by PB
The CDRom-Lock app is old and doesn't work with XP -- I could still open my
drive even when it was set to "Lock". So that's no good to me at all. I'll have
to look deeper into the link that Lucifer gave me. Thanks to all who replied.

Posted: Sun Jul 22, 2007 2:26 pm
by PB
Okay, I can't get it to work. Here's the code I've tried, and there's some VB
code at the following URL which relates to Lucifer's link. Can anyone help?

http://forums.microsoft.com/MSDN/ShowPo ... 3&SiteID=1

Code: Select all

#FSCTL_LOCK_VOLUME=$90018
#FSCTL_DISMOUNT_VOLUME=$90020

letter$="F" ; My DVD ROM drive letter.

hVolume=CreateFile_("\\.\"+letter$+":",#GENERIC_READ,#FILE_SHARE_READ|#FILE_SHARE_WRITE,0,#OPEN_EXISTING,0,0)

Repeat
  locked=DeviceIoControl_(hVolume,#FSCTL_LOCK_VOLUME,0,0,0,0,@dwBytesReturned,0)
  Sleep_(250)
Until locked

ret=DeviceIoControl_(hVolume,#FSCTL_DISMOUNT_VOLUME,0,0,0,0,@dwBytesReturned,0)

Debug ret

Repeat : Sleep_(1) : ForEver

Posted: Sun Jul 22, 2007 6:45 pm
by LuCiFeR[SD]
Hey PB, that drive lock program does work... just make sure you actually have media in the drive.

It is a known limitation of that program that if a disk is not in the drive it will not lock... well, with certain CD/DVD hardware anyway. Just check the compatability link on the site.

now, your code isn't working because it depends on the PREVENT_MEDIA_REMOVAL structure. to stop the drive ejecting.

I have never tried VB or C++ conversions to PB, but I'll have a crack at it :P...

Posted: Sun Jul 22, 2007 7:03 pm
by BI2
Hello PB,

have a Look at this

Code: Select all

; German forum: http://robsite.de/php/pureboard/viewtopic.php?t=2789&highlight=
; Author: Andreas (updated for PB3.93 by ts-soft)
; Date: 10. November 2003
; OS: Windows
; Demo: No

;############################# 
;Lock & Unlock CDRom-Trays 
;Open & Close CDRom-Trays 
;############################# 
;Lock & Unlock only 
;NT4, XP and Win2000 
;############################# 
;Author : Andreas 
;November 2003 
;############################# 

#IOCTL_STORAGE_MEDIA_REMOVAL = $2D4804 
#IOCTL_STORAGE_EJECT_MEDIA  = $2D4808 
#IOCTL_STORAGE_LOAD_MEDIA   = $2D480C 

#ListViewGadget = 100 
#Lock = 101 
#UnLock = 102 
#Eject = 104 
#Load = 105 
#Quit = 106 

Global WaitCur.l,ArrowCur.l 
WaitCur = LoadCursor_(0,#IDC_WAIT) 
ArrowCur = LoadCursor_(0,#IDC_ARROW) 

Structure PREVENT_MEDIA_REMOVAL 
  P1.b 
EndStructure 

Structure LWInfo 
  Drive.s 
  IsLock.b 
EndStructure  

Global NewList LW.LWInfo() 

Procedure GetCDLW() 
  Protected a.l,Buffer.s 
  ListViewGadget(#ListViewGadget,10,10,200,200) 
  *Buffer = AllocateMemory(255) 
  GetLogicalDriveStrings_(255,*Buffer) 
  Repeat 
    If PeekS(*Buffer + b) = "" 
      Break 
    EndIf  
    Buffer = PeekS(*Buffer + b) 
    SHGetFileInfo_(Buffer,0,SHF.SHFILEINFO,270,769) 
    If GetDriveType_(Buffer) = #DRIVE_CDROM 
      AddGadgetItem(#ListViewGadget,-1, PeekS(SHF+12)) 
      AddElement(LW()) 
      LW()\Drive = Mid(PeekS(SHF+12),FindString(PeekS(SHF+12),":",0)-1,2) 
      LW()\IsLock = 0 
    EndIf 
    b = b + 4 
  ForEver 
  FreeMemory(*Buffer) 
EndProcedure 

Procedure EjectCD(LW.s) 
  SetCursor_(WaitCur) 
  Protected hLwStatus.l 
  hLwStatus = CreateFile_("\\.\"+LW,#GENERIC_READ|#GENERIC_WRITE, 0, 0, #OPEN_EXISTING, 0, 0) 
  If hLwStatus 
    DeviceIoControl_(hLwStatus,#IOCTL_STORAGE_EJECT_MEDIA,0,0,0,0,@Ret,0) 
    CloseHandle_(hLwStatus) 
  EndIf 
  SetCursor_(ArrowCur) 
EndProcedure  

Procedure LoadCD(LW.s) 
  SetCursor_(WaitCur) 
  Protected hLwStatus.l 
  hLwStatus = CreateFile_("\\.\"+LW,#GENERIC_READ|#GENERIC_WRITE, 0, 0, #OPEN_EXISTING, 0, 0) 
  If hLwStatus 
    DeviceIoControl_(hLwStatus,#IOCTL_STORAGE_LOAD_MEDIA,0,0,0,0,@Ret,0) 
    CloseHandle_(hLwStatus) 
  EndIf 
  SetCursor_(ArrowCur) 
EndProcedure  

Procedure.l LockCD(Lock.b,LW.s) 
  Protected LW$,RetBuffer.l,hLwStatus.l 
  Dim p.PREVENT_MEDIA_REMOVAL(0) 
  p(0)\P1 = Lock 
  hLwStatus = CreateFile_("\\.\"+LW,#GENERIC_READ|#GENERIC_WRITE, 0, 0, #OPEN_EXISTING, 0, 0) 
  If hLwStatus 
    ;LOCK or UNLOCK CD-TRAY 
    Retval.l = DeviceIoControl_(hLwStatus, #IOCTL_STORAGE_MEDIA_REMOVAL,p(0),SizeOf(BYTE),@p(0),SizeOf(BYTE),@RetBuffer,0) 
    CloseHandle_(hLwStatus) 
  EndIf 
  ProcedureReturn Retval 
EndProcedure  

Procedure Repair() 
  ResetList(LW()) 
  While NextElement(LW()) 
  ; alle Locks rückgängig machen 
      LockCD(0,LW()\Drive) 
  Wend 
  DeleteObject_(WaitCur) 
  DeleteObject_(ArrowCur) 
  End 
EndProcedure  

Procedure ErrorMessage() 
  MessageRequester("Info","Kein Laufwerk gewählt",#MB_OK|#MB_ICONINFORMATION) 
EndProcedure 

If OpenWindow(0,0,0,320,230,"Lock-CDTray",#PB_Window_ScreenCentered|#PB_Window_SystemMenu) And CreateGadgetList(WindowID(0)) 
  GetCDLW() 
  ButtonGadget(#Lock, 220, 10, 80,20,"Lock") 
  ButtonGadget(#UnLock, 220, 40, 80,20,"UnLock") 
  ButtonGadget(#Eject, 220, 70, 80,20,"Eject") 
  ButtonGadget(#Load, 220, 100, 80,20,"Load") 
  ButtonGadget(#Quit, 220, 190, 80,20,"Quit") 
  
  Repeat 
    Select WaitWindowEvent() 
      Case #PB_Event_Gadget 
        Select EventGadget() 
          Case #Quit 
            Repair() 
          Case #Eject 
            If GetGadgetState(#ListViewGadget) > -1 
              SelectElement(LW(),GetGadgetState(#ListViewGadget)) 
              EjectCD(LW()\Drive) 
            Else 
              ErrorMessage()    
            EndIf  
          Case #Load 
            If GetGadgetState(#ListViewGadget) > -1 
              SelectElement(LW(),GetGadgetState(#ListViewGadget)) 
              LoadCD(LW()\Drive)  
            Else 
              ErrorMessage()    
            EndIf  
          Case #Lock 
            If GetGadgetState(#ListViewGadget) > -1 
               SelectElement(LW(),GetGadgetState(#ListViewGadget)) 
               If LW()\IsLock = 0 
               If LockCD(1,LW()\Drive) 
                  LW()\IsLock = 1 
               EndIf 
               EndIf 
            Else 
              ErrorMessage()    
            EndIf  
          Case #UnLock 
            If GetGadgetState(#ListViewGadget) > -1 
               SelectElement(LW(),GetGadgetState(#ListViewGadget)) 
              LockCD(0,LW()\Drive) 
            Else 
              ErrorMessage()    
            EndIf  
        EndSelect            
      Case #PB_Event_CloseWindow 
        Repair() 
    EndSelect 
  ForEver 
EndIf 
  
; ExecutableFormat=Windows
; FirstLine=1
; EnableXP
; EOF

Greets,

BI2

Posted: Sun Jul 22, 2007 8:34 pm
by LuCiFeR[SD]
well, that saves me a job :) Nice find BI2!

Posted: Mon Jul 23, 2007 1:57 am
by PB
> just make sure you actually have media in the drive

I'll take your word for it (I can't be bothered putting a disc in to see). :)

@BI2: Thanks for that -- just what I needed.