views:

470

answers:

5

Does anyone have a good guide to capabilities of Windows Services under XP? In particular, I am trying to find out what happens when a program being run as a service tries to open windows, but hasn't been given permission to interact with the desktop.

Basically, I have a program that is/was a GUI application, that should be able to run as a service for long term background processing. Rewriting the program to not display the GUI elements when doing background processing is a major effort, so I'd like to see if there is just a way to ignore the UI elements. It is sort of working now, as long as too many windows aren't opened. I'm trying to figure out what limits I might be running into. Ideally, there would be an MSDN page that discusses this, but I've had no luck finding one yet.

+3  A: 

A service in Microsoft Windows is a program that runs whenever the computer is running the operating system. It does not require a user to be logged on. Services are needed to perform user-independent tasks such as directory replication, process monitoring, or services to other machines on a network, such as support for the Internet HTTP protocol

Usually it is implemented as a console application that runs in the background and performs tasks that don't require user interaction.

The installed services can be configured through the Services applet, available from Control Panel --> Administrative Tools in Windows 2000/XP.

Services can be configured to start automatically when operating system starts, so you dont have to start each of them manually after a system reboot.

  1. Creating a Simple Service - MSDN Article
  2. Writing Windows Services Made easy - Code Project Article
  3. Five Steps to Writing Windows Services in C - DevX Article
Prakash
+8  A: 

Generally, services should be designed to not have any visible UI. The entire point of a service is to run in the background, without UI, unattended. (Think SQL Server, IIS, etc.)

In most scenarios, a separate application controls the service's operation, should a GUI be needed. (Continuing the samples I just mentioned, SQL Server Management Studio, IIS Manager, etc.) These separate applications configure and manipulate the service (and occasionally, if needed, bounce said service).

If your service requires occasional UI, and said UI can't be isolated to a control app, then you probably should reconsider the fact that you're using a service to begin with. Perhaps a UI application which resides in the system notification area is the right pattern to use? (E.G., Windows Live Communicator.)

John Rudy
The UI isn't needed while running as a service, but reworking the program to not pop up info windows and such requires a major rewrite. I'm trying to see if I can just ignore the GUI when doing the background work.
Jeff Olhoeft
And I take it the app can't be converted back out of being a service? If that's the case, you'll probably be best off doing what you mention above -- but I'd comment that pretty thoroughly so you or the maintenance programmer understands the scenario!
John Rudy
Not easily. Basically the app's gui is for configuring the background processing. You configure it and it then runs forever. Splitting it into a service and a control program is ideal, but it is this huge thing with the processing intertwined with the UI code...
Jeff Olhoeft
Understood ... You're in a tough position, there. As I said above, I think the path you mentioned is the only one available -- comments will be your friend.
John Rudy
A: 

If you should be thinking of eventually migrating to a newer OS such as Vista or Server 2008, you will find that you cannot give a service permission to interact with the desktop at all. Therefore, from the point of view of forwards compatibility, you should design your service to not require it.

1800 INFORMATION
A: 

Usually the service won't have permission to write to the window station and desktop, so it will fail; even running applications that load user32.dll can fail simply because user32 has initialization code that wants to talk to the window station and can't get access to it unless the service is running as an administrator.

Slothman
This would be my expectation, but it works for a while, until a certain number of windows have been opened (or some other limit has been reached or buffer overrun).
Jeff Olhoeft
A: 

A service in Windows XP can interact with the Desktop if its "Allow Service to Interact with Desktop" property (MMC -> service properties -> Log On tab) is checked. It is also possible to do so by doing the following:

hWinstation = OpenWindowStation("winsta0", FALSE, MAXIMUM_ALLOWED);
SetProcessWindoStation(hWinstation);
hDesktop = OpenDesktop("default", 0, FALSE, MAXIMUM_ALLOWED);
SetThreadDesktop(hDesk);

But be aware that presenting UI from a service process in Windows XP will almost always lead to a security problem (see Shatter attack). You should try to break out the UI part of your application from the service.

Max Caceres
Actually, I don't want to display the UI elements. It's fine, and preferable, if they don't show up. Removing them form the existing program is a major rewrite, though.
Jeff Olhoeft