Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Calling class from form
Message
From
03/01/2013 03:20:10
 
 
To
02/01/2013 16:25:42
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Miscellaneous
Thread ID:
01561114
Message ID:
01561184
Views:
44
>.Net 2010
>
>Hi all
>
>Ok, stepping off into VB.Net for the first time since 2005 or so, and it's just as much fun as I remember.
>
>Created a class, build it and added it as a reference to the project:
>
>Public Class SoundPlayer
>    Declare Auto Function PlaySound Lib "winmm.dll" (ByVal name _
>  As String, ByVal hmod As Integer) As Integer
>
>'    Public Const SND_SYNC = &H0 ' play synchronously 
>'    Public Const SND_ASYNC = &H1 ' play asynchronously 
>'    Public Const SND_MEMORY = &H4  'Play wav in memory
>'    Public Const SND_ALIAS = &H10000 'Play system alias wav 
>'    Public Const SND_NODEFAULT = &H2
>'    Public Const SND_FILENAME = &H20000 ' name is file name 
>'    Public Const SND_RESOURCE = &H40004 ' name is resource name or atom 
>End Class
>
>
>And I'm trying to call it from a button on a form
>
>    Private Sub CmdSound_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdSound.Click
>        Dim WaveFile As String
>        Dim SndSync As Integer, SndNoDefault As Integer
>        SndSync = &H0
>        SndNoDefault = &H2
>
>        WaveFile = "C:\Downloads\Sounds\uslsfact.wav"
>        SoundPlayer.PlaySound(WaveFile, SndSync)
>    End Sub
>
>
>The form builds with no errors or warnings, but when I try to run it, I get
>
>PInvokeStackImbalance was detected
>Message: A call to PInvoke function 'WindowsApplication1!WindowsApplication1.SoundPlayer::PlaySound' 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.
>
>What am I not seeing? All I need to do is create a 'hook' to the Windows Sound Player and play a .wav file. I need it in a class because I'll be using this on various forms within the application.

I think, as per the exception message, that you have the wrong sig. See : http://www.pinvoke.net/default.aspx/winmm.PlaySound
But why use unmanaged code when .NET has a simple managed alternative in system.dll :

http://msdn.microsoft.com/en-us/library/system.media.soundplayer.aspx
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform