i am new to vc++. i just created the service and register it with service snapin. i then started service by using startservice() function. it shows the error code as 1053:"The service did not respond to the start or control request in a timely fashion"
herewith i have attached the code
Expand|Select|Wrap|Line Numbers
- // sample.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include "Windows.h"
- #include "Winsvc.h"
- //#include "time.h"
- #include "stdio.h"
- char SvcName[10]="GOM";
- SERVICE_STATUS m_ServiceStatus;
- SERVICE_STATUS_HANDLE m_ServiceStatusHandle;
- BOOL bRunning=true;
- void WINAPI ServiceMain(DWORD argc, LPTSTR *argv);
- void WINAPI ServiceCtrlHandler(DWORD Opcode);
- BOOL InstallService();
- BOOL DeleteService();
- BOOL StartSvc();
- int main(int argc, char* argv[])
- {
- bool str=true;
- //printf("want to install enter -i and delete -d");
- if(str)
- {
- if(InstallService())
- printf("\n\nService Installed Sucessfully\n");
- else
- printf("\n\nError Installing Service\n");
- }
- if(str)
- {
- if(StartSvc())
- printf("service Started successfully");
- else
- printf("\n\nError in Starting Service\n");
- }
- if(!str)
- {
- if(DeleteService())
- printf("\n\nService UnInstalled Sucessfully\n");
- else
- printf("\n\nError UnInstalling Service\n");
- }
- else
- {
- SERVICE_TABLE_ENTRY DispatchTable[]=
- {{SvcName,ServiceMain},{NULL,NULL}};
- StartServiceCtrlDispatcher(DispatchTable);
- }
- return 0;
- }
- void WINAPI ServiceMain(DWORD argc, LPTSTR *argv)
- {
- DWORD status;
- DWORD specificError;
- m_ServiceStatus.dwServiceType = SERVICE_WIN32;
- m_ServiceStatus.dwCurrentState = SERVICE_START_PENDING;
- m_ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
- m_ServiceStatus.dwWin32ExitCode = 0;
- m_ServiceStatus.dwServiceSpecificExitCode = 0;
- m_ServiceStatus.dwCheckPoint = 0;
- m_ServiceStatus.dwWaitHint = 0;
- m_ServiceStatusHandle = RegisterServiceCtrlHandler(SvcName, ServiceCtrlHandler);
- if (m_ServiceStatusHandle == (SERVICE_STATUS_HANDLE)0)
- {
- return;
- }
- m_ServiceStatus.dwCurrentState = SERVICE_RUNNING;
- m_ServiceStatus.dwCheckPoint = 0;
- m_ServiceStatus.dwWaitHint = 0;
- if (!SetServiceStatus (m_ServiceStatusHandle, &m_ServiceStatus))
- {
- }
- bRunning=true;
- while(bRunning)
- {
- Sleep(3000);
- //Place Your Code for processing here....
- }
- return;
- }
- void WINAPI ServiceCtrlHandler(DWORD Opcode)
- {
- switch(Opcode)
- {
- case SERVICE_CONTROL_PAUSE:
- m_ServiceStatus.dwCurrentState = SERVICE_PAUSED;
- break;
- case SERVICE_CONTROL_CONTINUE:
- m_ServiceStatus.dwCurrentState = SERVICE_RUNNING;
- break;
- case SERVICE_CONTROL_STOP:
- m_ServiceStatus.dwWin32ExitCode = 0;
- m_ServiceStatus.dwCurrentState = SERVICE_STOPPED;
- m_ServiceStatus.dwCheckPoint = 0;
- m_ServiceStatus.dwWaitHint = 0;
- SetServiceStatus (m_ServiceStatusHandle,&m_ServiceStatus);
- bRunning=false;
- break;
- case SERVICE_CONTROL_INTERROGATE:
- break;
- }
- return;
- }
- BOOL InstallService()
- {
- HANDLE schSCManager,schService;
- schSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
- if (schSCManager == NULL)
- return false;
- char strDir[1024];
- // GetCurrentDirectory(1024,strDir);
- //strcat(strDir,"\\Debug\\sample.exe");
- LPCTSTR lpszBinaryPathName= "C:\\Program Files\\Microsoft Visual Studio\\MyProjects\\helloworld\\Debug\\helloworld.exe";
- schService = CreateService(schSCManager,SvcName,
- SvcName, // service name to display
- SERVICE_ALL_ACCESS, // desired access
- SERVICE_WIN32_OWN_PROCESS, // service type
- SERVICE_AUTO_START, // start type
- SERVICE_ERROR_NORMAL, // error control type
- lpszBinaryPathName, // service's binary
- NULL, // no load ordering group
- NULL, // no tag identifier
- NULL, // no dependencies
- NULL, // LocalSystem account
- NULL); // no password
- if (schService == NULL)
- {
- DWORD dw=GetLastError();
- printf("%ld",dw);
- return false;
- }
- /*if(StartService(schSCManager,0,NULL))
- printf("Service started successfully");
- else
- {
- DWORD dw=GetLastError();
- printf("error in start service %ld",dw);
- printf("error in service starting");
- }*/
- CloseServiceHandle(schService);
- return true;
- }
- BOOL DeleteService()
- {
- HANDLE schSCManager;
- SC_HANDLE hService;
- schSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
- if (schSCManager == NULL)
- return false;
- hService=OpenService(schSCManager,SvcName,SERVICE_ALL_ACCESS);
- if (hService == NULL)
- return false;
- if(DeleteService(hService)==0)
- return false;
- if(CloseServiceHandle(hService)==0)
- return false;
- return true;
- }
- BOOL StartSvc()
- {
- HANDLE hSvcMgr;
- SC_HANDLE shSvc;
- hSvcMgr=OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
- if(hSvcMgr==NULL)
- return false;
- shSvc=OpenService(hSvcMgr,SvcName,SERVICE_ALL_ACCESS);
- if(shSvc==NULL)
- return false;
- if(StartService(shSvc,0,NULL)==NULL)
- {
- ControlService(shSvc,SERVICE_CONTROL_CONTINUE,&m_ServiceStatus);
- DWORD dw=GetLastError();
- printf("error in start service %ld",dw);
- return false;
- }if(ControlService(shSvc,SERVICE_CONTROL_CONTINUE,&m_ServiceStatus)==NULL)
- {
- DWORD dw=GetLastError();
- printf("error in control service %ld",dw);
- return false;
- }
- if(CloseServiceHandle(shSvc)==0)
- return false;
- return true;
- }
can anybody help me to point out my mistakes. thanks in advance.