Results 1 to 12 of 12

Thread: Opening The CD Rom Drive

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2007
    Location
    Las Vegas, NV
    Posts
    301

    Opening The CD Rom Drive

    does anyone know how to make the software automatically open a specific cd rom drive?

    if you happen to know a string of code that does this, it would be appreciated...

  2. #2
    Addicted Member Genom's Avatar
    Join Date
    May 2006
    Posts
    186

    Re: Opening The CD Rom Drive

    in vb 6.0 you would use API try it...
    VB Code:
    1. 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
    2.  
    3. Private Sub cmdOpenCD_Click()
    4. Dim lRet As Long
    5.     lRet = mciSendString("set CDAudio door open", returnstring, 127, 0)
    6. End Sub
    7. Private Sub cmdCloseCD_Click()
    8. Dim lRet As Long
    9.     lRet = mciSendString("set CDAudio door closed", returnstring, 127, 0)
    10. End Sub
    Dim Me As Coder

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2007
    Location
    Las Vegas, NV
    Posts
    301

    Re: Opening The CD Rom Drive

    that one didn't work, but thanks... if anyone else has one i can try i'd appreciate it...

  4. #4
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Sector 001
    Posts
    1,577

    Re: Opening The CD Rom Drive

    It is working but you have to change every Long there to Integer and put the Returnstring in "":
    VB Code:
    1. Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Integer, ByVal hwndCallback As Integer) As Integer
    2.  
    3. 'then to open:
    4.     Dim lRet As Long
    5.      lRet = mciSendString("set CDAudio door open", "returnstring", 127, 0)
    6.  
    7. 'and to close:
    8.    Dim lRet2 As Long
    9.    lRet2 = mciSendString("set CDAudio door closed", "returnstring", 127, 0)
    Shoutouts go to Genom of course.
    VB 2005, Win Xp Pro sp2

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2007
    Location
    Las Vegas, NV
    Posts
    301

    Re: Opening The CD Rom Drive

    that didn't work for me either... do i install that on a button or will it do it automatically?

    i'm a noob... so i need some help... i never would have figured out any of that so far...

  6. #6
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Sector 001
    Posts
    1,577

    Re: Opening The CD Rom Drive

    Yes, the 'Private Declare Function etc...' is outside any procedure, then put
    VB Code:
    1. Dim lRet As Long
    2. lRet = mciSendString("set CDAudio door open", "returnstring", 127, 0)
    in a button click.
    VB 2005, Win Xp Pro sp2

  7. #7
    Addicted Member Genom's Avatar
    Join Date
    May 2006
    Posts
    186

    Re: Opening The CD Rom Drive

    only for info
    VB Code:
    1. Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Integer, ByVal hwndCallback As Integer) As Integer

    is a declaration of a function which shows to software that there is a function in that dll file and make it ready to use. we put them always outside an event or a function.
    Dim Me As Coder

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2007
    Location
    Las Vegas, NV
    Posts
    301

    Re: Opening The CD Rom Drive

    ok, i got the string in with no errors, but when i click the button... nothing happens...

  9. #9
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    530

    Re: Opening The CD Rom Drive

    hi Conflix

    I Need The Same Thing As You
    Have You Sorted ?

    Plz Post The Working One

    Thanks

  10. #10
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Opening The CD Rom Drive

    Here is a complete example:

    Opens the CD tray and then after 2 seconds it closes too.

    Code:
    Option Explicit On
    Option Strict On
    
    Imports System.Runtime.InteropServices
    
    Public Class Form1
    
    	<DllImport("winmm.dll", CharSet:=CharSet.Auto, EntryPoint:="mciSendString")> _
    	Private Shared Function mciSendString( _
    		ByVal lpstrCommand As String, _
    		ByVal lpstrReturnString As String, _
    		ByVal uReturnLength As Integer, _
    		ByVal hwndCallback As Integer) As Integer
    	End Function
    
    	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    		Dim lRet As Long
    		lRet = mciSendString("set CDAudio door open", "returnstring", 127, 0)
    		System.Threading.Thread.Sleep(2000)
    		lRet = mciSendString("set CDAudio door closed", "returnstring", 127, 0)
    	End Sub
    End Class
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  11. #11
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Opening The CD Rom Drive

    Here is a sub to open a specific CDRom tray using Window Media Player. You need to add a reference to Windows Media Player COM object to your project
    Code:
    Imports WMPLib
    
    Private Sub EjectCDRom(ByVal driveName As String)
            Dim player As WMPLib.WindowsMediaPlayer = Nothing
            Try
                player = New WMPLib.WindowsMediaPlayer
                Dim count As Integer = player.cdromCollection.count
                If count > 0 Then
                    For n As Integer = 0 To count - 1
                        If (player.cdromCollection.Item(n).driveSpecifier).StartsWith(driveName.ToUpper) Then
                            player.cdromCollection.Item(n).eject()
                            Exit For
                        End If
                    Next
                End If
            Catch ex As Exception
                Throw ex
            Finally
                player = Nothing
            End Try
        End Sub
    For example, if you want to open the tray of the E: CDRom drive, you just call
    Code:
    EjectCDRom("E:")

  12. #12
    Fanatic Member
    Join Date
    Jul 2007
    Posts
    530

    Re: Opening The CD Rom Drive

    Thank You Guy

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