PlaySound question

H

Harry J. Smith

I added some sounds to my application, but the example in the msdn Library did not work.
It had:

[DllImport("coredll")]
public static extern bool PlaySound( string szSound, IntPtr hMod, PlaySoundFlags flags );

The file coredll was not found. I changed it to [DllImport("WINMM")] and it now works.
What is the best way to handle this?

-Harry
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi Harry,

code.dll ( Please note that you forgot the . in your post ) is a dll from
the PocketPC platform. so the code you took maybe intended to run on a
PocketPC, if not you have to change it to the correct dll from the desktop.

Cheers,
 
H

Harry J. Smith

Thanks Mike, your C# Tutorial is great.

The short answer to me question is use

[DllImport("winmm.dll")]
public static extern bool PlaySound( string szSound, IntPtr hMod, PlaySoundFlags flags );

I also put in an exception handler to keep the application from crashing:

public class Sound
{
public static void Play( string strFileName )
{
try
{
Helpers.PlaySound( strFileName, IntPtr.Zero, Helpers.PlaySoundFlags.SND_FILENAME |
Helpers.PlaySoundFlags.SND_ASYNC );
}
catch
{
// DllImport("winmm.dll") file not found or no PlaySound entry point in the file
// so generate no sound
}
}
}

-Harry

Mike Kitchen said:
This has been addressed several times here.

I have compiled the common answers in my snippets page found at

http://www.publicjoe.f9.co.uk/csharp/snip/snip001.html

Plus I now have about sound at
http://www.publicjoe.f9.co.uk/csharp/csharp17.html
and
http://www.publicjoe.f9.co.uk/csharp/csharp18.html

Hope this helps

Publicjoe
C# Tutorial at http://www.publicjoe.f9.co.uk/csharp/tut.html
C# Snippets at http://www.publicjoe.f9.co.uk/csharp/snip/snippets.html
C# Ebook at http://www.publicjoe.f9.co.uk/csharp/samples/ebook.html
VB Ebook at http://www.publicjoe.f9.co.uk/vbnet/samples/ebook.html

Harry J. Smith said:
I added some sounds to my application, but the example in the msdn Library did not work.
It had:

[DllImport("coredll")]
public static extern bool PlaySound( string szSound, IntPtr hMod, PlaySoundFlags flags );

The file coredll was not found. I changed it to [DllImport("WINMM")] and it now works.
What is the best way to handle this?

-Harry
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top