共用方式為


Why specify SND_NODEFAULT when calling PlaySound?

Because the alternative is often much worse.

 

Several months ago, I got a bug report that if you launched mmsys.cpl then set the “Select” sound to a value, then cleared the sound, the reporters application would ding whenever you moved around their tree control.

I dug into the problem a bit and discovered that the problem was actually in the Windows common controls.  Under some circumstances the common controls would call PlaySound specifying a sound alias and the SND_ASYNC and SND_ALIAS flags only. 

The problem with this is that if you specify SND_ALIAS without also specifying SND_NODEFAULT, the PlaySound API decides that you really want the sound to be played and it will play the default “ding” sound instead. 

From the PlaySound API’s point of view this makes sense.  After all, you asked the API to play a sound, it doesn’t know that you meant “only play this sound when the sound file represented by the alias exists”. 

 

In fact, that’s the entire reason for the SND_NODEFAULT flag – it lets the PlaySound API that you only want to play a sound when the sound has been defined.

Comments

  • Anonymous
    September 25, 2008
    PingBack from http://www.easycoded.com/why-specify-snd_nodefault-when-calling-playsound/

  • Anonymous
    September 25, 2008
    Maybe someone from the Outlook team could read this, and add SND_NODEFAULT to the PlaySound they call when a new mail doesn't match any rule. Even setting the default sound to (none) doesn't help - it would just beep the PC speaker. To solve it, I've resorted to creating a 0-byte .wav and setting the default sound to it.

  • Anonymous
    September 26, 2008
    Is this also the reason that an older app without a commctrl manifest, when run on Vista, will sound a beep every time you change the selection in a list view control?  The MSDN viewer in VC 6 is where I run into this annoyance the most.

  • Anonymous
    September 26, 2008
    Mike: exactly.  Backstory:  This was a bug in Win2000 that was fixed in XP.  Unfortunately it was present in Server 2K3 and when the XP changes were merged into the Server 2K3 changes at the time of the Longhorn Reset, this change was missed. FWIW it's highly likely to be fixed in a future release of Windows :).

  • Anonymous
    September 28, 2008
    Does 2008 have the same problem too?  I don't think I'm going to get sound working in 2008 in order to check.  I did get sound working in 2003 and wonder why I never noticed that bug.  Seems out of character that it's not being fixed in Vista though, being that it's a cosmetic bug in addition to being noisy. Enough of that digression though.  Here's the real reason for specifying SND_NODEFAULT:  Real defaults come from unsound lending practices.