ATL Service with _ATL_NO_COM_SUPPORT Defined will not Start

Si applica a: Visual Studio Professional 2010

Symptoms


After creating a default ATL Service project using the ATL Project Wizard, you register the service with the /Service command line parameter and then try to start the service with 'net start'. The service starts and then immediately exits with a message "The service could not be started":

c:\>net start atlservice
The ATLService service is starting........
The ATLService service could not be started.

The service did not report an error.

Cause


The ATL Service base class libraries do not call the Win32 method SetServiceStatus(SERVICE_RUNNING) if there are no ATL COM objects implemented in the service.

Resolution


The ATL Service is primarily designed as a service host for COM objects so the expectation is that the developer will be adding COM objects to the service. 

However, it is possible to use ATL without COM. For instance, there is an _ATL_NO_COM_SUPPORT macro which you can define which will turn off most of the support for registering and implementing COM objects in the ATL library.

Due to a bug in the service base classes, if _ATL_NO_COM_SUPPORT is defined, the SetServiceStatus(SERVICE_RUNNING) call is not made and the service will start and then immediately exit.

You can work around this bug by overriding the PreMessageLoop function in your CAtlServiceModule derived class and calling SetServiceStatus:

         HRESULT PreMessageLoop(int nShowCmd)
         {
                 SetServiceStatus(SERVICE_RUNNING);
                 return __super::PreMessageLoop(nShowCmd);
         }

Note that this PreMessageLoop override is only relevant for ATL Services that have _ATL_NO_COM_SUPPORT defined.

More Information


  1. Create a new ATL Project with the project wizard
  2. Name the project ATLService and in the Application Settings, select the Service option
  3. Open the stdafx.h file and before any header #include statements, define _ATL_NO_COM_SUPPORT:

    #define _ATL_NO_COM_SUPPORT
  4. Build the project
  5. From a console window, register the service:

    ATLService /service
  6. Start the service

    C:\>net start ATLService
    The ATLService service is starting.
    The ATLService service could not be started.

    he service did not report an error.
  7. Add the workaround code and rebuild the service. You may need to re-register the service as well.
  8. Start the service again:

    C:\>net start ATLService
    The ATLService service is starting.
    The ATLService service was started successfully.