Hi All,
I am writing my first windows service program.The below is my service program, I am unable to run my service after installing it. When run the service, the startservice api is returning the error "The service didnot respond to the start or control request in a timely fashion" and the servicecontroldispatcher API is returning the error "the service process couldnot connect to the service controller"(this error because unable to start the service).
Some times the start service is starting properly but the servicecontroldispatcher API is returning the error "the service process couldnot connect to the service controller".
I am unable to figure what is the exact problem, could u guys please help in resolving the issue.
// MyService.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "MyService.h"
#include <winsvc.h>
#include <windows.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
CWinApp theApp;
using namespace std;
#define SERVICE_NAME _T("MyServiceMain")
VOID WINAPI MyServiceMain(DWORD dwArgc, LPTSTR *lpszArgv);
VOID WINAPI ServiceHandler(DWORD fdwControl);
CRITICAL_SECTION CriticalSec;
SERVICE_TABLE_ENTRY lpServiceStartTable[] =
{
{SERVICE_NAME, MyServiceMain},
{NULL, NULL}
};
class CMyService
{
public:
CMyService(CString strServiceName)
{
m_strServiceName = strServiceName;
}
CString m_strServiceName;
CString m_strServicePath;
SERVICE_STATUS_HANDLE m_hServiceStatusHandle;
SERVICE_STATUS m_ServiceStatus;
bool CreateMyService();
bool DeleteMyService();
bool RunMyService();
bool StopMyService();
};
CMyService gOMyService(SERVICE_NAME);
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
HMODULE hModule = ::GetModuleHandle(NULL);
if (hModule != NULL)
{
if (!AfxWinInit(hModule, NULL, ::GetCommandLine(), 0))
{
_tprintf(_T("Fatal Error: MFC initialization failed\n"));
nRetCode = 1;
}
else
{
if(argc == 2)
{
CString strCmd = argv[1];
if(strCmd.CompareNoCase(_T("-i")) == 0)
gOMyService.CreateMyService();
if(strCmd.CompareNoCase(_T("-s")) == 0)
{
gOMyService.RunMyService();
if(!StartServiceCtrlDispatcher(lpServiceStartTable))
{
long nError = GetLastError();
CString strError;
LPVOID lpMsgBuf;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, nError, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&lpMsgBuf, 0, NULL);
strError = (LPTSTR)lpMsgBuf;
MessageBox(NULL,strError,_T("Error"),MB_OK);
}
}
if(strCmd.CompareNoCase(_T("-u")) == 0)
gOMyService.DeleteMyService();
if(strCmd.CompareNoCase(_T("-k")) == 0)
gOMyService.StopMyService();
}
}
}
else
{
_tprintf(_T("Fatal Error: GetModuleHandle failed\n"));
nRetCode = 1;
}
return nRetCode;
}
VOID WINAPI MyServiceMain(DWORD dwArgc, LPTSTR *lpszArgv)
{
MessageBox(NULL,_T("test"),_T("Error"),MB_OK);
DWORD status = 0;
DWORD specificError = 0xfffffff;
gOMyService.m_ServiceStatus.dwServiceType = SERVICE_WIN32;
gOMyService.m_ServiceStatus.dwCurrentState = SERVICE_RUNNING;
gOMyService.m_ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN | SERVICE_ACCEPT_PAUSE_CONTINUE;
gOMyService.m_ServiceStatus.dwWin32ExitCode = 0;
gOMyService.m_ServiceStatus.dwServiceSpecificExitCode = 0;
gOMyService.m_ServiceStatus.dwCheckPoint = 0;
gOMyService.m_ServiceStatus.dwWaitHint = 0;
gOMyService.m_hServiceStatusHandle = RegisterServiceCtrlHandler(gOMyService.m_strServiceName, ServiceHandler);
if (!gOMyService.m_hServiceStatusHandle)
{
long nError = GetLastError();
CString strError;
LPVOID lpMsgBuf;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, nError, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&lpMsgBuf, 0, NULL);
strError = (LPTSTR)lpMsgBuf;
MessageBox(NULL,strError,_T("Error"),MB_OK);
return;
}
gOMyService.m_ServiceStatus.dwCurrentState = SERVICE_RUNNING;
gOMyService.m_ServiceStatus.dwCheckPoint = 0;
gOMyService.m_ServiceStatus.dwWaitHint = 0;
if(!SetServiceStatus(gOMyService.m_hServiceStatusHandle, &gOMyService.m_ServiceStatus))
{
long nError = GetLastError();
char pTemp[121];
sprintf(pTemp, "SetServiceStatus failed, error code = %d\n", nError);
}
if(!StartServiceCtrlDispatcher(lpServiceStartTable))
{
long nError = GetLastError();
CString strError;
LPVOID lpMsgBuf;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, nError, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&lpMsgBuf, 0, NULL);
strError = (LPTSTR)lpMsgBuf;
MessageBox(NULL,strError,_T("Error"),MB_OK);
}
}
VOID WINAPI ServiceHandler(DWORD fdwControl)
{
switch(fdwControl)
{
case SERVICE_CONTROL_STOP:
case SERVICE_CONTROL_SHUTDOWN:
{
gOMyService.m_ServiceStatus.dwWin32ExitCode = 0;
gOMyService.m_ServiceStatus.dwCurrentState = SERVICE_STOPPED;
gOMyService.m_ServiceStatus.dwCheckPoint = 0;
gOMyService.m_ServiceStatus.dwWaitHint = 0;
::DeleteCriticalSection(&CriticalSec);
}
break;
case SERVICE_CONTROL_PAUSE:
gOMyService.m_ServiceStatus.dwCurrentState = SERVICE_PAUSED;
break;
case SERVICE_CONTROL_CONTINUE:
gOMyService.m_ServiceStatus.dwCurrentState = SERVICE_RUNNING;
break;
case SERVICE_CONTROL_INTERROGATE:
break;
default:
{
}
};
if (!SetServiceStatus(gOMyService.m_hServiceStatusHandle, &gOMyService.m_ServiceStatus))
{
long nError = GetLastError();
char pTemp[121];
sprintf(pTemp, "SetServiceStatus failed, error code = %d\n", nError);
}
}
bool CMyService::CreateMyService()
{
bool bRet = false;
SC_HANDLE hServiceMngr = NULL;
TCHAR szPath[MAX_PATH];
if( !GetModuleFileName( NULL, szPath, MAX_PATH ) )
{
return bRet;
}
hServiceMngr = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS);
if (hServiceMngr)
{
SC_HANDLE hService = ::CreateServiceW
(
hServiceMngr,
m_strServiceName,
m_strServiceName,
SERVICE_ALL_ACCESS,
SERVICE_WIN32_OWN_PROCESS ,
SERVICE_AUTO_START,
SERVICE_ERROR_NORMAL,
szPath,
NULL,
NULL,
NULL,
NULL,
NULL
);
char pTemp[121];
if (hService)
{
char pTemp[121];
sprintf(pTemp, "Service %s installed\n", m_strServiceName);
CloseServiceHandle(hService);
bRet = true;
}
else
{
long nError = GetLastError();
CString strError;
LPVOID lpMsgBuf;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, nError, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&lpMsgBuf, 0, NULL);
strError = (LPTSTR)lpMsgBuf;
MessageBox(NULL,strError,_T("Error"),MB_OK);
}
CloseServiceHandle(hServiceMngr);
}
else
{
long nError = GetLastError();
char pTemp[121];
sprintf(pTemp, "OpenSCManager failed, error code = %d\n", nError);
}
return bRet;
}
bool CMyService::RunMyService()
{
SC_HANDLE hSCMngr = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS);
if(hSCMngr)
{
SC_HANDLE hServiceHandle = OpenService( hSCMngr, m_strServiceName, SERVICE_ALL_ACCESS);
if(hServiceHandle)
{
if(StartService(hServiceHandle, 0, NULL))
{
CloseServiceHandle(hServiceHandle);
CloseServiceHandle(hSCMngr);
return TRUE;
}
else
{
long nError = GetLastError();
CString strError;
LPVOID lpMsgBuf;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, nError, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&lpMsgBuf, 0, NULL);
strError = (LPTSTR)lpMsgBuf;
MessageBox(NULL,strError,_T("Error"),MB_OK);
}
CloseServiceHandle(hServiceHandle);
}
else
{
long nError = GetLastError();
CString strError;
LPVOID lpMsgBuf;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, nError, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&lpMsgBuf, 0, NULL);
strError = (LPTSTR)lpMsgBuf;
MessageBox(NULL,strError,_T("Error"),MB_OK);
}
CloseServiceHandle(hSCMngr);
}
else
{
long nError = GetLastError();
CString strError;
LPVOID lpMsgBuf;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, nError, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&lpMsgBuf, 0, NULL);
strError = (LPTSTR)lpMsgBuf;
MessageBox(NULL,strError,_T("Error"),MB_OK);
}
return FALSE;
}
bool CMyService::StopMyService()
{
SC_HANDLE hSCMmngr = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS);
if(hSCMmngr)
{
SC_HANDLE hServiceHandle = OpenService( hSCMmngr, m_strServiceName, SERVICE_ALL_ACCESS);
if(hServiceHandle)
{
SERVICE_STATUS status;
if(ControlService(hServiceHandle,SERVICE_CONTROL_STOP,&status))
{
CloseServiceHandle(hServiceHandle);
CloseServiceHandle(hSCMmngr);
return TRUE;
}
else
{
long nError = GetLastError();
char pTemp[121];
sprintf(pTemp, "ControlService failed, error code = %d\n", nError);
}
CloseServiceHandle(hServiceHandle);
}
else
{
long nError = GetLastError();
char pTemp[121];
sprintf(pTemp, "OpenService failed, error code = %d\n", nError);
}
CloseServiceHandle(hSCMmngr);
}
else
{
long nError = GetLastError();
char pTemp[121];
sprintf(pTemp, "OpenSCManager failed, error code = %d\n", nError);
}
return FALSE;
}
bool CMyService::DeleteMyService()
{
bool bRet=false;
SC_HANDLE hServiceMngr = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS);
if(hServiceMngr)
{
SC_HANDLE hServiceHandle = OpenService( hServiceMngr, m_strServiceName, SERVICE_ALL_ACCESS);
if(hServiceHandle)
{
if(!DeleteService(hServiceHandle))
{
long nError = GetLastError();
CString strError;
LPVOID lpMsgBuf;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, nError, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&lpMsgBuf, 0, NULL);
strError = (LPTSTR)lpMsgBuf;
MessageBox(NULL,strError,_T("Error"),MB_OK);
}
CloseServiceHandle(hServiceHandle);
}
CloseServiceHandle(hServiceMngr);
}
else
{
long nError = GetLastError();
CString strError;
LPVOID lpMsgBuf;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, nError, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&lpMsgBuf, 0, NULL);
strError = (LPTSTR)lpMsgBuf;
MessageBox(NULL,strError,_T("Error"),MB_OK);
}
return bRet;
}</windows.h></winsvc.h>