Automatically Start Windows Service on Startup [modified]
-
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
-
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
-
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
After the Install method of
CAtlServiceModuleT
is called, you could useOpenService
to get a handle on the service and thenChangeServiceConfig
to alter the service's start type toSERVICE_AUTO_START
? Looking in the code, you could overrideRegisterAppId
to do that, asRegisterAppId
calls theInstall
method - you can't just overrideInstall
, because it's not virtual -RegisterAppId
is the closest method called using a correctly typed object pointer (look forpT->RegisterAppId
inAtlBase.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; }