The Service API consists of a set of functions and a couple of callback functions that must be supported. Together these functions allow an application to run independent of any logged-on user . This is not common to the entire Windows family of operating systems. For instance, the MSDE running on a Windows 9x machine is not available to users if no one is logged on to the workstation. This limited ability of Windows 9x workstations to make applications available to other users is probably reasonable for applications that make minimal demands and are run for a few desktops physically near each other. This breaks down, however, when the application to be served is used by a large number of users distributed over a wide area who are unlikely to be located near the application server. In this case, the ability to run independent of the currently logged-on user is essential.
At its heart, a service application supports at least three functions:
VOIDWINAPIServiceMain(DWORDdwArgc,//Numberofarguments LPTSTR*lpszArgv//Arrayofargumentstringpointers); |
One interesting feature of ServiceMain is that it takes the same arguments as a traditional C/C++ main function.
DWORDWINAPIHandlerEx(DWORDdwControl,//Requestedcontrolcode DWORDdwEventType,//Eventtype LPVOIDlpEventData,//Eventdata LPVOIDlpContext//User-definedcontextdata); |
Note that this function prototype has superseded the previous Handler prototype that expected only a single dwControl parameter. The old-style control handler function can still be specified for compatibility and is the only option for Windows NT 4 and earlier, but the new-style function is preferred for Windows 2000 service applications. Listing 3-1 will use the Handler function. Later examples in this book will use the new form of the control handler function. The HandlerEx function is primarily useful for services that need to manage multiple services from a single control handler or for a service that needs to react to device notifications.
Before we look at an example, we need to answer one question: if a service is running when no one is logged on, under whose authority does the service run?