views:

631

answers:

5

I need to play a wav file from a C# application running as a Windows Service. I have tried both System.Media.SoundPlayer and a P/Invoke call to WinMM.dll (which is probably what SoundPlayer is doing).

[DllImport("WinMM.dll")]
private static extern bool PlaySound(string fname, int Mod, int flag); 

If I run my code as a console application, the sounds play. When I run it from a service, no luck, and I guess I'm not surprised.

So is there a way to play a sound from a windows service? Would something like DirectSound help? Or am I going to be stuck writing a console application and having the windows service app communicate with it as an intermediary?

Thanks in advance

+3  A: 

You've chosen the wrong application type. A windows service is for longer running applications that execute non-interactively, whether or not someone has logged into the computer. For example SQL Server, IIS etc.

You are also prevented in Windows Vista and later, from displaying user interface windows from a windows service. For Windows XP,2000 Server and you can display a MessageBox, however this is not recommended for most services.

So in general, services are not permitted to be "interactive", this includes playing sounds, multimedia etc.

You either need to change the application type to a normal Console/Windows Forms application, or live without playing sounds from your service.

For more information see this page on interactive services and related pages at MSDN.

Ash
From the link provided, an accpeted method of interacting with the user is to spawn a GUI application. I will explore this option more fully.
A: 

I do not think that is a good idea as the service is time dependant in the order of the loading of the service, and anyway a service should not have to do anything like this, i.e. play a wav file, could you imagine an end user feeling frustrated every-time the computer boots up and hearing a wav file playing and the user not being able to shut it up rooting around looking for some kind of start up program...or worse, thinking that the computer is infected and end up reformatting the disk....

In general, a service should do at a minimum, provide a API layer to the high level application, such as communicating with a device driver, or setting up a TCP/IP network connection in such a way that the application or specifically, a network socket can interact with.

Your philosophy of what goes into a service violates that paradigm.

Hope this helps, Best regards, Tom.

tommieb75
A: 

If you service is logging on as "Local System Account", try this:

  1. Start Control Panel->Administrative Tools->Services
  2. Inspect the Properties of your service
  3. On the Log On tab, check "Allow service to interact with desktop"
egrunin
@egrunin, Don't do this! It has numerous security and usability problems. In Vista and later it has been specifically disabled anyway. Some reading: http://asprosys.blogspot.com/2009/03/allow-service-to-interact-with-desktop.html
Ash
Thanks for the link. I figured the other answers dealt with the security issues, and (wrongly) assumed the OP had good reasons for not starting spawning a normal app when he needed the user's attention.
egrunin
+1  A: 

Playing a wav file from a service is definitely possible, at least on Windows 7 (and most likely Vista), by using the Windows Core Audio APIs. I recently verified this by making a small test service using NAudio. I just downloaded the NAudio sources and copied the "Wsapi" parts from their NAudioDemo project. This was on Windows 7 Enterprise 64bit, but I don't think that matters. The service was using the LocalSystem account.
For the record, playing sounds from a service is a perfectly legitimate thing to do in an embedded setting.

Rickard Lind
Thanks!I just tried this out on 32-bit Vista and it worked fine. I also tried the Wave Playback (did not work) and Directsound playback (worked) included in the library. There are 2 limitations that I have seen, both of which I can live with for my project. First, with Wasapi, I could only get it to work (even with the demo app) in exclusive mode, which means no other apps could play a sound. For DirectSound, I couldn't get it to play a sound as my service stopped. But I can live with this.
A: 

@tommieb75:

Consider this scenario:

I want my server to provide our PABX with a continuous audio stream via a mini-jack cable so my callers can listen to some royalty free music while waiting endlessly for an operator.

As both server and PABX are in the same rack, it would be a very convinient solution. WinAmp etc. would not suffice as console apps are just not practical in a server environment because they get accidently shutdown all the time.