Results 1 to 5 of 5

Thread: CD ROM drive, and shutting down

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2000
    Posts
    81

    Cool

    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

  2. #2
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Here's fore the Cd-rom ejecting
    Code:
    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:
    Code:
    '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
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Mar 2000
    Posts
    81

    Unhappy ???

    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

  4. #4
    New Member
    Join Date
    Jun 2000
    Posts
    4

    Wink

    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.

  5. #5
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    It's a property, which have the cdrom status stored in a static variable, you can use it like this:
    Code:
    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
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width