4

this question has been asked before but there is no conclusive answer.

I've written a Windows service in Delphi, which needs to generate a beep under certain condition. This works fine on XP, however fails in Windows 7 or 2008.

Note:

  1. Beep can work if i create a console program instead of a service - using PC speakers.
  2. Beep cannot work in a service even if i enable "allow service to interact with desktop" or even assign administrator rights to the service.

My question: Is there a way I can call beep API such that it works in a service? Thanks.

RRUZ
  • 134,889
  • 20
  • 356
  • 483
Joshua Lim
  • 51
  • 1
  • 4

2 Answers2

1

You can't do this in Vista and up. Services run in a different session and so don't have access to the speaker.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • So couldn't they start a separate EXE which does the beep? – Hossein May 12 '11 at 17:13
  • 1
    @Hossein, a separate EXE will ordinarily run under the same session as the process that started it. The service would need to find a session for some interactive user, impersonate the credentials of that user, and then run the noise-generating process in that other session. – Rob Kennedy May 12 '11 at 21:51
  • David/Rob - Why not just have a trayicon.exe mini app added to the registry to autorun for each logged in user? VMWare, and Interbase, and every other service has a tray icon app these days, and just make it beep. (I put that as an answer actually) – Warren P May 14 '11 at 04:54
  • @warren clearly that's how to do it. But question seemed adamant about wanting to do it in a service. – David Heffernan May 14 '11 at 06:22
  • @David - the service in question is actually a monitoring software, so the alerting is the main functionality. Currently, it sends alerts using email or SMS using a GSM phone - but sometimes, there is no internet or the GSM phone is down - that's where I need to sound a beeper alarm. – Joshua Lim May 16 '11 at 12:04
  • @Joshua Well, whatever it is you are doing, you can't beep from a service. Instead you'll need an app running in the current interactive session. – David Heffernan May 16 '11 at 12:36
  • There is a way. Call it an evil hack but... http://stackoverflow.com/questions/1286194/how-can-i-make-a-windows-service-beep – Warren P Oct 04 '11 at 18:30
1

Update: Someone found a way here. it involves IOCTL, and is available to drivers and services.

Original answer:

The only way I know of to interact with the user would be to have your Service communicate with a small user-agent process which would be added to HKEY_LOCAL_MACHINE\CurrentUser\Run to autorun.

This is the usual pattern in vista and win7 where no user interaction is possible directly from the service:

  1. MyLittleService.exe has no access to the user. But it can communicate via a named pipe with a tray icon utility.

  2. MyLittleTrayIcon.exe communicates to the service, and can also be told to signal the user with message boxes, beep via whatever method (windows sound effects probably would be better than trying to access the PC speaker which is not guaranteed to exist on every PC anymore), etc, and maybe even can be used to control the service (restart it, reload the configuration etc).

Community
  • 1
  • 1
Warren P
  • 65,725
  • 40
  • 181
  • 316
  • P, thanks for proposing this. I do have a tray icon to display balloon alerts, so I could make the tray icon beep, but one problem is it wouldn't work unless the user logon. "small user-agent process" - can this agent run even without logon? – Joshua Lim May 16 '11 at 12:01
  • As far as I know, no, it wouldn't run if the user has not been logged in. But Microsoft has actively blocked you from doing anything else. – Warren P May 16 '11 at 12:27
  • P - I guess the system tray method is the best, unless i can find a special USB device with a buzzer attached and sound it from the service. ;) – Joshua Lim Jun 28 '11 at 04:29
  • How about you SMS out from the background service to your cell phone? :-) Then you can notify people who can't hear the beep. – Warren P Jun 28 '11 at 13:03
  • The beep is actually to notify people when the SMS device is not working. The tray method is not complete because the user has to login. ;) – Joshua Lim Jun 30 '11 at 07:09