Share via


Stopping a Running Service

A version of this page is also available for

Windows Embedded CE 6.0 R3

4/8/2010

For Windows Mobile:

A service instance may be stopped by a call to the DeregisterService function. DeregisterService identifies and labels the service as invalid. This disallows any call attempts to CreateFile on a given service handle.

DeregisterService takes the same handle value that was returned to the call to RegisterService and not a file handle created using CreateFile. It is possible that an application may not have this handle. For example, if the service was started upon system initialization, the application may not have the handle. In this case, to receive this value, a call to GetServiceHandle must be made. The following code sample shows how to use GetServiceHandle to receive the necessary information.

HANDLE hService = GetServiceHandle(L"TEL0:", NULL, NULL);
if(0 != hService)
     DeregisterService(hService);

Once DeregisterService has been called for the last opened RegisterService,for a particular DLL, the DLL will be unloaded from memory. It is extremely important that no other threads are created by the DLL that is currently running, because unloading the DLL unloads any code pages and this causes all threads running in its context to fail.

See Also

Reference

RegisterService
DeregisterService
GetServiceHandle

Other Resources

Services.exe
CreateFile