Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

open cd-rom door

Status
Not open for further replies.

Sorwen

Technical User
Nov 30, 2002
1,641
US
I often have to pull data from files that are on a lot of cds so I created a program that will read the data with a button click. I want to be really lazy though and I want the cd-rom drive to open after it finishes reading the cd. Actually it would be helpful in that it also lets me know when it is finished reading the cd while I'm working on other things. So I found this.

Code:
Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" _
        (ByVal lpszCommand As String, ByVal lpszReturnString As String, _
        ByVal cchReturnLength As Long, ByVal hwndCallback As Long) As Long

To use as:
Code:
mcdSendString("set CDAudio door open", 0, 0, 0)
or
Code:
mcdSendString("set CDAudio door open", retValstring, 127, IntPtr.Zero)

But I always get the following error:
PInvokeStackImbalance was detected
Message: A call to PInvoke function 'CompileCDs!CompileCDs.main_frm::mciSendString' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.

How can I fix this or is there another way to do it?

-I hate Microsoft!
-Forever and always forward.
 
Cool. Thanks. Will do.

-I hate Microsoft!
-Forever and always forward.
 
Since I wasn't sure where to start on modifing this for what I wanted I created a test console program. The event never fires and it caused something to keep trying to access the A drive.

-I hate Microsoft!
-Forever and always forward.
 
Back to the original MCISendString api call.
I use it all the time, you just need to make some tweaks.

First you need to change all the long values in the MCISendString to integers.
( this API function was written pre .Net (integer maps to long)
Code:
    [COLOR=green]' api call used to open/close a cdrom door ******************************[/color]
    Private Declare Function MCISendString Lib "winmm.dll" Alias _
    "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString _
    As Integer, ByVal uReturnLength As Integer, ByVal hwndCallback As _
    Integer) As Integer [COLOR=green]' * end MCISendString API function ******************[/color]
Here is a shell of a routine that I use. I have exclude all of the code that checks the drive type.
Code:
    [COLOR=green]' constants used to open/close a cdrom door *****************************[/color]
    Private Enum EDoor
        closed  
        open    
    End Enum [COLOR=green]' * EDoor ******************************************************[/color]

    [COLOR=green]' open/close a CDRom door ***********************************************[/color]
    Private Function CDRom(ByVal e As EDoor, ByVal driveRootPath As String) _
    As Integer
        Dim i As Integer    [COLOR=green]' mcisendstring return value[/color]
        Dim action As String = String.Concat("set cddrive door ", e, " wait")
        Dim connection As String = String.Concat("open ", driveRootPath, _
            " type CDAudio alias cddrive")

        [COLOR=green]' open a connection to the drive[/color]
        i = MCISendString(connection, 0&, 0, 0)
        [COLOR=green]' open/close the drive door base on the enum constant passed[/color]
        i = MCISendString(action, 0&, 0, 0)
        [COLOR=green]' close the connection[/color]
        i = MCISendString("close cddrive", 0&, 0, 0)

        Return i
    End Function [COLOR=green]' * CDRom **************************************************[/color]
Now whenever you need to open/close the drive door, just make a call to the function.
Code:
Me.CDRom(e:=EDriveDoor.open, driveRootPath:="Z:\")

 
Cool. Thank you very much I'll try that. I was having no luck on the other. It seemed it would work well. I would have had to make changes so it would work for events instead, but I could see how that would work out. There is just something missing in my understanding of it though. It seems like it would be straight forward, but nope. The API will work out easier though. Thanks.

-I hate Microsoft!
-Forever and always forward.
 
Yeah... it dawned on me later that I got side tracked from what you really needed. I saw that bit of code and my stupid brain when "Hey, with this he could start his software back up on the CD Insert event!" ... got all excited and blew it! LOL! Sorry.

That bit you posted earlier is the only snipit I've ever seen to programatically eject the CD. So hopfully bigtimmin can help you with the needed tweeks.

Senior Software Developer
 
{QUOTE]...you need to change all the long values in the MCISendString to integer...[/QUOTE]

Yep. That did it. Thanks. It is fine. I just when with a thread that keeps looping. That was what I had originally planned.


Yeah... it dawned on me later that I got side tracked from what you really needed. I saw that bit of code and my stupid brain when "Hey, with this he could start his software back up on the CD Insert event!" ... got all excited and blew it! LOL! Sorry.

That bit you posted earlier is the only snipit I've ever seen to programatically eject the CD. So hopfully bigtimmin can help you with the needed tweeks.

NP. Thanks for helping. The events would likely work better in the long run, but I just couldn't get it worked out. It seems that a GUI program doesn't have the Event needed to attach the addhanler too. So I tried to just do a console program to get an idea on how it all would work, but it wasn't catching the event either. Likely something I'm failing to do. Ah well. I did get it worked out for the API at least so it is good for now.

Thank you both for the help.

-I hate Microsoft!
-Forever and always forward.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top