can anyone teach me how to correct my code....when I debug this the 12th line or the sndPlaySound("shot.wav", SND_ASYNC) line shows an error "A call to PInvoke function 'StartForm!StartForm.StartForm::sndPlaySound' has unbalanced the stack. This is likely because the managed PInvoke ".Please help me with this one this is a requirement for us to be able to pass the subject...tnx guyz

Public Class StartForm

Declare Function sndPlaySound Lib "winmm.dll " Alias "sndPlaySoundA" (ByVal Name As String, ByVal Flags As Long) As Long
Private Const SND_ASYNC As Int32 = &H1
Private BlnSounds As Boolean = True

Public Sub PictureBox1 _Click(blah blah blah)
On Error GoTo fix
If Start = True Then
            If MenuItem4.Checked = True Then
                sndPlaySound("shot.wav", SND_ASYNC)
            Else
            End If
            Bullets = Bullets - 1
            Points = Points + 1
            Label2.Text = "Bullets : " & Bullets
            Label3.Text = "Score : " & Points
            If Bullets = 0 Then
                Start = False
                MenuItem3.Enabled = False
                MsgBox("Bullets over!" & Chr(13) & "Game over!", vbCritical, "Out of bullets!")
            End If
            Me.PictureBox3.BackColor = Color.Red
            Me.PictureBox3.Cursor = Cursors.SizeAll
        Else
            If Points = 20 Then
                Start = False
                MenuItem3.Enabled = False
                MsgBox("You exterminated all Gays!!!You won the game!!!")
            End If

        End If
End Sub
End Class

Dani AI

Generated

That runtime message almost always means the native function signature in your Declare/DllImport does not match the Win32 API. In VB.NET a Long is 64-bit while the Win32 UINT/DWORD is 32-bit; using the wrong managed type will "unbalance the stack." Also check for small mistakes (for example a stray space inside the DLL name) and remove the On Error GoTo so exceptions are visible while debugging.

The simplest, safest fix is to stop P/Invoking and use the managed APIs that ship with .NET. Example approaches:

' Play a file in the application folder:
Dim p As New System.Media.SoundPlayer(IO.Path.Combine(Application.StartupPath, "shot.wav"))
p.Play()

' Or VB helper:
My.Computer.Audio.Play(IO.Path.Combine(Application.StartupPath, "shot.wav"), AudioPlayMode.Background)

Make sure shot.wav is included in the project as Content and set to "Copy if newer" (or use a full path). If you add the WAV to Resources you can play it from My.Resources via SoundPlayer as a stream.

If you must P/Invoke, correct the signature so marshaling matches the native types (use 32-bit integers, proper charset). For example use DllImport with a 32-bit integer parameter rather than a VB.NET Long. Also prefer SetLastError:=True and the correct CharSet. After changing the signature, rebuild and rerun to see if the error clears.

Quick checklist:

  • Replace VB6-style Long with VB.NET Integer in any Declare/DllImport.
  • Remove stray characters from the DLL name.
  • Remove On Error GoTo while debugging so errors are visible.
  • Ensure shot.wav is copied to the output folder or reference a full path.
  • Try the managed SoundPlayer or My.Computer.Audio.Play first; they avoid P/Invoke issues.
  • Compare with 's sample to see which approach it uses.

Microsoft docs for the managed class: System.Media.SoundPlayer.

See sound program in this attachment and compare with your own prog

WindowsApplication1.zip

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.