Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. ATL / WTL / STL
  4. Automatically Start Windows Service on Startup [modified]

Automatically Start Windows Service on Startup [modified]

Scheduled Pinned Locked Moved ATL / WTL / STL
csharpc++question
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    narayanagvs
    wrote on last edited by
    #1

    Hi, I would like to create a windows service which automatically starts on startup and calls my function.I dont want to use .NET support for this. Using ATL I have taken a windows service project.I build it and registered the service using "/Service" which is then shown in Services with "Manual Startup type". I would want this to be automatic. How should I go about it? For this while creating the service we need to specify "SERVICE_AUTO_START" flag. But this service creation is automatically done by the base class CAtlServiceModuleT.

    SC_HANDLE hService = ::CreateService(
    hSCM, m_szServiceName, m_szServiceName,
    SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
    SERVICE_DEMAND_START, SERVICE_ERROR_NORMAL,
    szFilePath, NULL, NULL, _T("RPCSS\0"), NULL, NULL);

    Now How should I go about overriding the base class functions. I know I need to make use of _tWinMain() and override but Iam not sure of the approach. Any suggestions or relevant links would be helpful Thanks Satya

    Today is a gift, that's why it is called the present.

    modified on Tuesday, October 7, 2008 4:17 AM

    K S 2 Replies Last reply
    0
    • N narayanagvs

      Hi, I would like to create a windows service which automatically starts on startup and calls my function.I dont want to use .NET support for this. Using ATL I have taken a windows service project.I build it and registered the service using "/Service" which is then shown in Services with "Manual Startup type". I would want this to be automatic. How should I go about it? For this while creating the service we need to specify "SERVICE_AUTO_START" flag. But this service creation is automatically done by the base class CAtlServiceModuleT.

      SC_HANDLE hService = ::CreateService(
      hSCM, m_szServiceName, m_szServiceName,
      SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
      SERVICE_DEMAND_START, SERVICE_ERROR_NORMAL,
      szFilePath, NULL, NULL, _T("RPCSS\0"), NULL, NULL);

      Now How should I go about overriding the base class functions. I know I need to make use of _tWinMain() and override but Iam not sure of the approach. Any suggestions or relevant links would be helpful Thanks Satya

      Today is a gift, that's why it is called the present.

      modified on Tuesday, October 7, 2008 4:17 AM

      K Offline
      K Offline
      KarstenK
      wrote on last edited by
      #2

      Override the base implementation via copying the code in your project and change it. Or you can try to change the startup type of the service yourself (somehow).:confused:

      Greetings from Germany

      1 Reply Last reply
      0
      • N narayanagvs

        Hi, I would like to create a windows service which automatically starts on startup and calls my function.I dont want to use .NET support for this. Using ATL I have taken a windows service project.I build it and registered the service using "/Service" which is then shown in Services with "Manual Startup type". I would want this to be automatic. How should I go about it? For this while creating the service we need to specify "SERVICE_AUTO_START" flag. But this service creation is automatically done by the base class CAtlServiceModuleT.

        SC_HANDLE hService = ::CreateService(
        hSCM, m_szServiceName, m_szServiceName,
        SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
        SERVICE_DEMAND_START, SERVICE_ERROR_NORMAL,
        szFilePath, NULL, NULL, _T("RPCSS\0"), NULL, NULL);

        Now How should I go about overriding the base class functions. I know I need to make use of _tWinMain() and override but Iam not sure of the approach. Any suggestions or relevant links would be helpful Thanks Satya

        Today is a gift, that's why it is called the present.

        modified on Tuesday, October 7, 2008 4:17 AM

        S Offline
        S Offline
        Stuart Dootson
        wrote on last edited by
        #3

        After the Install method of CAtlServiceModuleT is called, you could use OpenService to get a handle on the service and then ChangeServiceConfig to alter the service's start type to SERVICE_AUTO_START? Looking in the code, you could override RegisterAppId to do that, as RegisterAppId calls the Install method - you can't just override Install, because it's not virtual - RegisterAppId is the closest method called using a correctly typed object pointer (look for pT->RegisterAppId in AtlBase.h). Something like the code below should work? HRESULT RegisterAppId(bool bService = false) throw() { HRESULT hBaseRes = CAtlServiceModuleT< CbModule, IDS_SERVICENAME >::RegisterAppId(bService); if (SUCCEEDED(hBaseRes) && bService) { HRESULT hRes = E_FAIL; SC_HANDLE hSCM = ::OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); if (hSCM != NULL) { SC_HANDLE hService = ::OpenService(hSCM, m_szServiceName, SERVICE_CHANGE_CONFIG); if (hService != NULL) { if (::ChangeServiceConfig(hService, SERVICE_NO_CHANGE, SERVICE_AUTO_START, SERVICE_NO_CHANGE, NULL, NULL, NULL, NULL, NULL, NULL, NULL)) { hRes = hBaseRes; } ::CloseServiceHandle(hService); } ::CloseServiceHandle(hSCM); } hBaseRes = hRes; } return hBaseRes; }

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups