We help IT Professionals succeed at work.

PlaySound fails on Windows 7

LogicMan2006 asked
I've got a fairly large VS2008/MFC application that has been around for quite some time.    The program has functionality to have sounds played (using PlaySound) when certain events occur.   It's seldom used and frankly I rarely test anything to do with it.  So,  I didn't notice that at some point the PlaySound quit working with Windows 7.      I can take a current version of the program and it works fine on Windows XP, but nothing happens on Windows 7.   I can get small sample apps to work ok in W7 using the same WAV files so I know this not related to the WAV file itself.

The code is basically this:
  PlaySound(_T("C:\\Windows\Media\\Tada.wav"), NULL,  SND_FILENAME | SND_ASYNC );

What I've done so far is:
1)  Tried different combinations of the SND_* constants.
2)  Tried creating another thread and executing it there using SND_SYNC
3)  Tried removing as many  dependencies on outside libraries as possible.
4)  Tried playing WAV files that come with Windows, Aliases.   Nothing works in this application, but does in test applications.

As I said,  I can create test apps that work ok.    If I play the sound using SND_SYNC, it fails but you can't use GetLastError() to find out why.   Using SND_ASYNC will cause PlaySound to always return
true, but no sound comes out.   I'm really at a loss with this.   Clearly there's something I'm doing to cause the problem but at this point I don't know how to troubleshoot this any further.   Does anyone have any idea what I could do to cause this to occur?

Watch Question

Put this test line in the beginning of your program to test that it is really work. If it works, you may have a memory problem - the program cannot load the wav. You can try with a very small wav.

You can see how this problem was solved:

This guy proposes to allocate the memory and only if this operation succeeds, load the wav to that memory block and play sound with SND_MEMORY|SND_ASYNC.
Thank you for your help.     We figured out what the issue was and I'll share it here as it was an a very subtle issue that ended up affecting several other areas of our program.    We use a 3rd party library called LeadTools to do some image processing.    We have not upgraded it since 2001.   It turns out that it was not behaving correctly when new threads were being called.   When one of their DLLs received the DLL_THREAD_ATTACH it would execute some code that was blowing up in some cases.    We were able to remove that part of LeadTools and now things are working fine again.

One sysadmin once said me: "sound always disappears first." :)