Eject

The declaration:
Visual Basic:
Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As System.Text.StringBuilder, ByVal uReturnLength As Long, ByVal hwndCallback As Long) As Long
Along with the following code, will work fine.
Visual Basic:
'Eject: 
mciSendString("set CDAudio door open", Nothing, 127, 0)
'Inject: 
mciSendString("set CDAudio door closed", Nothing, 127, 0)
 
I try that code..and VB spits out this error in the IDE

A call to PInvoke function 'WindowsApplication1!WindowsApplication1.Form1::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.
 
Thats because half the declaration seems to be in VB6 and half seems to be in VB.Net. I'm also kind of confused as to why a return parameter is being passed as a StringBuilder instead of a string, unless StringBuilders are simply more flexible in their marshalling.

Try:
Visual Basic:
Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (_ 
    ByVal lpstrCommand As String, _
    ByVal lpstrReturnString As String, _
    ByVal uReturnLength As Int32, _
    ByVal hwndCallback As Int32) As Int32
or
Visual Basic:
Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (_ 
    ByVal lpstrCommand As String, _
    ByVal lpstrReturnString As System.Text.StringBuilder, _
    ByVal uReturnLength As Int32, _
    ByVal hwndCallback As Int32) As Int32

The unbalanced stack happens because the declaration you are using has two longs (VB6 Int32 versus VB.NET Int64), resulting in an extra four bytes of data on the stack.
 
Back
Top