我有一个这样的例子,如果需要可以e-mail给斑竹。
NT Service
The NT Service for Delphi 3 download contains a component which encapsulates NT
services, and a demo service. The component produces neat multithreaded, self
installing and uninstalling services. The source code is heavily commented.
(*========================================================================*
| NT Service unit |
| |
| Encapsulates an NT service that supports the following interface. |
| |
| Copyright (c) Colin Wilson 1997. All rights reserved. |
| |
| Command line parameters: |
| -i Install the service |
| -d De-install the service |
| -l Turn on logging (must be before '-n') |
| -n Run in non-service mode (for debugging) |
| |
| Service Startup Parameters: |
| -l Turn on logging |
| |
| The following protected functions can be overridden. |
| |
| function PreRegisterInitialize : boolean; |
| |
| Override this to perform pre-registration processing. This must not |
| take more than a couple of milliseconds. Return false to prevent |
| the service from running. |
| |
| function PostRegisterInitialize : boolean; |
| |
| Override this to perform processing that should happen before the |
| main thread starts. |
| |
| function Execute : Integer; |
| |
| Override this to perform processing for the service. |
| This main processing must check the Terminated flag, or the return |
| value of ExecuteDelay, often, and finish when asked. The function |
| can return an Exit Code for the service. |
| |
| procedure Installed; |
| |
| Override this to perform post installation processing. |
| |
| procedure Deleted; |
| |
| Override this to perform post deletion processing. |
| |
| The following functions and properties can be used from within the |
| above overridden functions: |
| |
| property RegistryRoot : string read GetRegistryRoot; |
| |
| Returns the root key for the service entry in the registry. |
| |
| function ExecuteDelay (nMilliseconds : Integer) : boolean; |
| |
| Delay for the specified time in milliseconds, or until the service |
| is asked to shut down. The function returns False if the service |
| should terminate. |
| |
| property TerminateEvent : THandle read fTerminatEvent; |
| |
| This event gets signalled when the service should terminate. It can |
| be used in more advanced services (With a WaitForMultipleObjects |
| call, for instance). |
*==============================================================