PlaySound Windows 7


Can anyone help with a problem I've got with PlaySound running on Windows 7. I'm porting some software from XP to Win 7 and the application is meant to play a WAV file when an alarm is sounded.

The process is spawned via CreateProcess as follows:

BOOL bSuccess = CreateProcess( NULL,
&SCInfo );

PlaySound is called as follows:

PlaySound( szWavFileName, NULL, SND_FILENAME | SND_ASYNC | SND_LOOP );

Now I've created a small MFC dialog with Play/Stop buttons with the call as shown above and it works fine on both XP and Windows 7. But for some reason because its running as a process it doesn't produce the sound.

I've tried loading the WAV file as a resource and using that but it just caused the process to continuing fall over.

Anyone got any ideas?

Help appreciated.
What file type is the audio file you are trying to play? What is the return value of PlaySound()?
Its a standard WAV file, plays OK through windows media player playsound returns true like its successful
And you are saying it doesn't sound. Strange. Not even by launching the executable from Windows Explorer?
The exe is stated as a background task using create process it can't be run in that manner, yes no sound is generated
I understand it is meant to be a background process. But why can't it be executed from Windows Explorer? What would it take to make it launchable this way?

Also, have you checked the return value of CreateProcess()? Is the process being created OK?
Yes its started OK and running as a process its started by a service it would take a lot to change it to run as a exe. I did create a mfc app to just play/stop the wave file and it worked fine, I used the same play sound call with no problems
Well, an EXE is an EXE. I really can't imagine what you need to change to make the PlaySound() function call. But anyway, are you trying to produce sound from a Windows service? It might be problematic. I don't remember right now if that is allowed or not nowadays. The Session 0 thingy may be in the way.

Have you searched the web to see if there are reports on PlaySound() not working for services or for executables started from services?
Been looking all day but the core audio API is meant to work under session 0
Checking the documentation of PlaySound() ( ) I see the SND_SYSTEM flag. Have you tried it? It might be related to playing sounds from services.
Yes tried that :-(
I saw a post in MSDN stating async sound was unstable. Have you tried using synchronous playback?
Its a real time application can't make it sync, I need it to sound the alarm and continue processing leaving the alarm in a loop until the alarm is acknowledged by the user
Spawn a new thread and play it synchronously.

EDIT: Wait, I thought the call to PlaySound() was in a different executable. If it is in a different executable, the service can continue to work with the other executable's main thread stuck. No need for a new thread.
Last edited on
Ok, it just hit me: Are you letting the process' thread die? Since it is a helper process with the only purpose of playing the sound and since you are playing it asynchronously, doesn't that mean that your executable dies shortly after the PlaySound() call? I rarely work with sound so I cannot be sure, but right now I am thinking that letting the executable die after PlaySound() also kills the sound.

This may not be happening in your MFC test app because it probably had a message loop? In any case I still support the synchronous playback test.
If you launch the exe from a service could be a problem with windows 7 session 0 isolation, you cannot launch any GUI exe from a windows service.
Topic archived. No new replies allowed.