Stopping a Running Service

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

Services.exe | CreateFile | RegisterService | DeregisterService | GetServiceHandle

 Last updated on Saturday, April 10, 2004

© 1992-2003 Microsoft Corporation. All rights reserved.