Skip to content

Latest commit

 

History

History
144 lines (97 loc) · 7.78 KB

nc-winsvc-lpservice_main_functionw.md

File metadata and controls

144 lines (97 loc) · 7.78 KB
UID title description helpviewer_keywords old-location tech.root ms.assetid ms.date ms.keywords req.header req.include-header req.target-type req.target-min-winverclnt req.target-min-winversvr req.kmdf-ver req.umdf-ver req.ddi-compliance req.unicode-ansi req.idl req.max-support req.namespace req.assembly req.type-library req.lib req.dll req.irql targetos req.typenames req.redist ms.custom f1_keywords dev_langs topic_type api_type api_location api_name
NC:winsvc.LPSERVICE_MAIN_FUNCTIONW
LPSERVICE_MAIN_FUNCTIONW (winsvc.h)
The entry point for a service. (Unicode)
LPSERVICE_MAIN_FUNCTION
LPSERVICE_MAIN_FUNCTION callback
LPSERVICE_MAIN_FUNCTION callback function
LPSERVICE_MAIN_FUNCTIONA
LPSERVICE_MAIN_FUNCTIONW
ServiceMain
_win32_servicemain
base.servicemain
winsvc/LPSERVICE_MAIN_FUNCTION
winsvc/LPSERVICE_MAIN_FUNCTIONA
winsvc/LPSERVICE_MAIN_FUNCTIONW
base\servicemain.htm
security
d7f3235e-91bd-4107-a30c-4a8f9a6c731e
12/05/2018
LPSERVICE_MAIN_FUNCTION, LPSERVICE_MAIN_FUNCTION callback, LPSERVICE_MAIN_FUNCTION callback function, LPSERVICE_MAIN_FUNCTIONA, LPSERVICE_MAIN_FUNCTIONW, ServiceMain, _win32_servicemain, base.servicemain, winsvc/LPSERVICE_MAIN_FUNCTION, winsvc/LPSERVICE_MAIN_FUNCTIONA, winsvc/LPSERVICE_MAIN_FUNCTIONW
winsvc.h
Windows.h
Windows
Windows XP [desktop apps only]
Windows Server 2003 [desktop apps only]
Windows
19H1
LPSERVICE_MAIN_FUNCTIONW
winsvc/LPSERVICE_MAIN_FUNCTIONW
c++
APIRef
kbSyntax
UserDefined
Winsvc.h
LPSERVICE_MAIN_FUNCTION
LPSERVICE_MAIN_FUNCTIONA
LPSERVICE_MAIN_FUNCTIONW

LPSERVICE_MAIN_FUNCTIONW callback function

-description

The entry point for a service.

The LPSERVICE_MAIN_FUNCTION type defines a pointer to this callback function. ServiceMain is a placeholder for an application-defined function name.

-parameters

-param dwNumServicesArgs [in]

The number of arguments in the lpServiceArgVectors array.

-param lpServiceArgVectors [in]

The null-terminated argument strings passed to the service by the call to the StartService function that started the service. If there are no arguments, this parameter can be NULL. Otherwise, the first argument (lpServiceArgVectors[0]) is the name of the service, followed by any additional arguments (lpServiceArgVectors[1] through lpServiceArgVectors[dwNumServicesArgs-1]).

If the user starts a manual service using the Services snap-in from the Control Panel, the strings for the lpServiceArgVectors parameter come from the properties dialog box for the service (from the Services snap-in, right-click the service entry, click Properties, and enter the parameters in Start parameters.)

-remarks

A service program can start one or more services. A service process has a SERVICE_TABLE_ENTRY structure for each service that it can start. The structure specifies the service name and a pointer to the ServiceMain function for that service.

When the service control manager receives a request to start a service, it starts the service process (if it is not already running). The main thread of the service process calls the StartServiceCtrlDispatcher function with a pointer to an array of SERVICE_TABLE_ENTRY structures. Then the service control manager sends a start request to the service control dispatcher for this service process. The service control dispatcher creates a new thread to execute the ServiceMain function of the service being started.

The ServiceMain function should immediately call the RegisterServiceCtrlHandlerEx function to specify a HandlerEx function to handle control requests. Next, it should call the SetServiceStatus function to send status information to the service control manager. After these calls, the function should complete the initialization of the service. Do not attempt to start another service in the ServiceMain function.

The Service Control Manager (SCM) waits until the service reports a status of SERVICE_RUNNING. It is recommended that the service reports this status as quickly as possible, as other components in the system that require interaction with SCM will be blocked during this time. Some functions may require interaction with the SCM either directly or indirectly.

The SCM locks the service control database during initialization, so if a service attempts to call StartService during initialization, the call will block. When the service reports to the SCM that it has successfully started, it can call StartService. If the service requires another service to be running, the service should set the required dependencies.

Furthermore, you should not call any system functions during service initialization. The service code should call system functions only after it reports a status of SERVICE_RUNNING.

The ServiceMain function should create a global event, call the RegisterWaitForSingleObject function on this event, and exit. This will terminate the thread that is running the ServiceMain function, but will not terminate the service. When the service is stopping, the service control handler should call SetServiceStatus with SERVICE_STOP_PENDING and signal this event. A thread from the thread pool will execute the wait callback function; this function should perform clean-up tasks, including closing the global event, and call SetServiceStatus with SERVICE_STOPPED. After the service has stopped, you should not execute any additional service code because you can introduce a race condition if the service receives a start control and ServiceMain is called again. Note that this problem is more likely to occur when multiple services share a process.

Examples

For an example, see Writing a ServiceMain Function.

Note

The winsvc.h header defines LPSERVICE_MAIN_FUNCTION as an alias which automatically selects the ANSI or Unicode version of this function based on the definition of the UNICODE preprocessor constant. Mixing usage of the encoding-neutral alias with code that not encoding-neutral can lead to mismatches that result in compilation or runtime errors. For more information, see Conventions for Function Prototypes.

-see-also

HandlerEx

RegisterServiceCtrlHandlerEx

RegisterWaitForSingleObject

SERVICE_TABLE_ENTRY

Service Functions

Service ServiceMain Function

SetServiceStatus

StartServiceCtrlDispatcher