Click here to Skip to main content
15,879,095 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
'The problem described below has now been solved when I noticed I had stupidly put ByVal lphMidiOut instead of ByRef !!!! Apologies to all.

'I am baffled as to why this program fails
VB
Imports System.Runtime.InteropServices
Public Class Form1
    Declare Function midiOutOpen Lib "winmm.dll" Alias "midiOutOpen" _
        (ByVal lphMidiOut As Int32, ByVal uDeviceID As Int32, _
         ByVal dwCallback As Int32, ByVal dwInstance As Int32, _
         ByVal dwFlags As Int32) As Int32

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) _
                            Handles Me.Load
        Dim lpMO As Int32
        Dim rv As Int32 = midiOutOpen(lpMO, -1, 0, 0, 0)
        Debug.Print(CStr(rv))

        '''''  the above code returns error 11 (MMSYSERR_INVALPARAM) ?? '''''
        '''''           -1 is the uDeviceID for the MIDI_MAPPER         '''''
        '''''  It also fails for uDeviceID = 0 (my on-board sound card) '''''
    End Sub
End Class


'Anyone got an idea as to why?
Posted
Updated 6-Nov-12 1:14am
v4

1 solution

At least one, first parameter is wrong. It's a pointer to a handle, so you need an out parameter passing for this one instead:
http://msdn.microsoft.com/en-us/library/windows/desktop/dd798476%28v=vs.85%29.aspx[^].

This should work:
http://www.pinvoke.net/search.aspx?search=midiOutOpen&namespace=[All][^].

For VB.NET, out parameters passing method is not supported (shame on it), so ByRef is used instead. (The difference is purely syntactic: ref parameter should be initialized before passing, out does not require that, but the called code is supposed to initialize it, which is also required — in C#.)

—SA
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900