Using playsound to play background music

I am currently making a frogger game for an assignment at college. I decided to put some background music into the game and used the following code:

Public Class SoundClass
Declare Auto Function PlaySound Lib "winmm.dll" (ByVal Name _
As String, ByVal hmod As Integer, ByVal flags As Integer) As Integer
' name specifies the sound file when the SND_FILENAME flag is set.
' hmod specifies an executable file handle.
' hmod must be Nothing if the SND_RESOURCE flag is not set.
' flags specifies which flags are set.

' The PlaySound documentation lists all valid flags.
Public Const SND_SYNC = &H0 ' play synchronously
Public Const SND_ASYNC = &H1 ' play asynchronously
Public Const SND_FILENAME = &H20000 ' name is file name
Public Const SND_RESOURCE = &H40004 ' name is resource name or atom

Public Sub PlaySoundFile(ByVal filename As String)
' Plays a sound from filename.
PlaySound(filename, Nothing, SND_SYNC)
End Sub
End Class

in the form_load procedure

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim SoundInst As New SoundClass()
SoundInst.PlaySoundFile("\frogger.wav")
End Sub

The only problem is that when I go to run the code I get the following error:

An unhandled exception of type 'System.Security.SecurityException' occurred in Frogger.exe

Additional information: System.Security.Permissions.SecurityPermission

Hope I haven't been too confusing, I have tried looking on google but can't seem to find anything to help.

Thanks in advance for any help. Dan




Answer this question

Using playsound to play background music

  • NinJA999

    Running your program off a network share Search the forums for "caspol.exe".


  • arooni

    I have read up on the caspol.exe and I understand what it's for. The problem is I don't know what to do or what code to write. I am guessing the problem is that i'm on the college network and it ain't liking the code. Could you please give me some help with the code I need to put in my program.

    Thank you Dan



  • aliasx

    There's no code involved in what you need to do. You just need to configure the .NET 2.0 framework and tell it that you explicitly trust your assembly to do something as drastic as P/Invoking a Windows API function. Caspol.exe lets you do that. If you have the .NET 2.0 SDK installed, there's a GUI tool that lets you do this too, you'll find it in Control Panel + Administrative Tools.


  • SonAsylum

    Perhaps if you embed the wav file it MIGHT

    work better.

    Also, with VB Express you do not need to use APIs

    to do this.

    See my post at http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=953440&SiteID=1



  • Using playsound to play background music