Click to See Complete Forum and Search --> : CD ROM drive, and shutting down
samdv
Jun 13th, 2000, 06:17 AM
Hi people.
I'm currently throwing together a little personal desktop pal to keep me company during the long nights, and two of the things I want to be able to do are to open and close the CD drive and to shut down the computer (separately, of course). There must be API calls out there somewhere to help me in my plight, so perhaps somebody knows of either of them?
Any help would be massively appreciated.
SamDV :D
kedaman
Jun 13th, 2000, 06:27 AM
Here's fore the Cd-rom ejecting
Property Get CdromOpen(Optional valchange) As Boolean
Static old As Boolean
If Not IsMissing(valchange) Then old = valchange
CdromOpen = old
End Property
Property Let CdromOpen(Optional valchange, newvalue As Boolean)
Dim lRet&, SRet&
valchange = CdromOpen(newvalue)
If newvalue Then
lRet = mciSendString("set CDAudio door open", SRet, 127, 0)
Else
lRet = mciSendString("set CDAudio door closed", SRet, 127, 0)
End If
End Property
And here's for the Windows shutdown:
'In declarations
Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long, ByVal dwReserved As Long) As Long
Enum exitmode
Shutdown = 0
Restart = 1
Suspend = 2
End Enum
'In code
Sub ExitWindows(how As exitmode)
Const EWX_LOGOFF = 0
Const EWX_SHUTDOWN = 1
Const EWX_REBOOT = 2
Const EWX_FORCE = 4
Select Case how
Case 0
t& = ExitWindowsEx(EWX_SHUTDOWN, 0)
Case 1
t& = ExitWindowsEx(EWX_REBOOT Or EXW_FORCE, 0)
Case 2
SetSystemPowerState 1, 1
End Select
End Sub
samdv
Jun 13th, 2000, 10:35 PM
kedaman,
thanks for replying. The code for shutting down made sense, and that works fine. However, and I know this will probably really annoy you, I don't understand what is happening in the open CD drive code. I'm very skeptical about cutting and pasting code without knowing what's going on, so if you could give me a brief runthrough of how the code works, and how I might use it to open the cd drive at the click of a button then I'd much appreciate it.
Thanks again,
SamDV
smiffe
Jun 18th, 2000, 10:45 PM
this is how to open or close a cd drive door.
Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" _
(ByVal lpstrCommand As String, ByVal lpstrReturnString As String, _
ByVal uReturnLength As Long, ByVal hwndCallback As Long) As Long
Private Declare Function GetDriveType Lib "kernel32" Alias "GetDriveTypeA" _
(ByVal nDrive As String) As Long
Private Const DRIVE_CDROM = 5
Public Sub cdDriveDoor(OpenDoor As Boolean, Optional DriveLetter As String = "")
Dim mssg As String * 255 'makes a string with 255 blank spaces
Dim DriveType As Long
If DriveLetter <> "" Then
'make sure the drive entered is a cd drive
DriveType = GetDriveType(DriveLetter)
If DriveType <> DRIVE_CDROM Then
MsgBox DriveLetter & " is not a cd-rom drive", vbOKOnly + vbCritical, "Error"
Exit Sub
End If
If OpenDoor = True Then 'open the drive door
'open the drive as a cdaudio device
mciSendString "Open " & DriveLetter & " Type cdaudio Alias cd", mssg, 255, 0
'open the door
mciSendString "set cd door open", 0&, 0, 0
'close the drive as a cdaudio device
mciSendString "close cd", 0&, 0, 0
Else 'close the drive door
'open the drive as a cdaudio device
mciSendString "Open " & DriveLetter & " Type cdaudio alias cd", mssg, 255, 0
'close the door
mciSendString "set cd door closed", 0&, 0, 0
'close the drive as a cdaudio device
mciSendString "close cd", 0&, 0, 0
End If
Else 'no driveletter entered
If OpenDoor = True Then
'open the door of the default cdaudio device (first cd drive)
mciSendString "set cdaudio door open", 0&, 0, 0
Else
'close the door of default device
mciSendString "set cdaudio door closed", 0&, 0, 0
End If
End If
End Sub
this code will work for computers like mine that have multiple cd drives. If you just want to open a drive door and
don't care to know which one then leave off the optional driveletter parameter. by default windows sets the
first cd drive on a system as the active cdaudio device so to use any other drive you have to first tell windows to make it
the default drive. Then when done with whatever you
are doing you should close it so windows will go back to its
normal default.
kedaman
Jun 19th, 2000, 06:31 AM
It's a property, which have the cdrom status stored in a static variable, you can use it like this:
Cdromopen=true 'opens cdrom
Cdromopen=false 'closes cdrom
cdromopen=not cdromopen 'closes if open, open if closed
I'm sure smiffes code is better if you have several cdrom drives, but i haven't tested it ;)
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.