Rasheed
2011-06-22 14:54:58 UTC
Hi ,
i have created a simple Service by reading the MSDN. i have installed
the service successfully, but when i try to start the service its
throwing the exception as below:
Windows could not start the MYService on Local Computer.
Error 1053: The service did not respond to the start or control
request in a timely fashion.
i have pasted my code in below, kindly can anybody help me where i
did
wrong.
i am working on Windows 7 OS and VS 2010.
TCHAR szCommand[10];
TCHAR szSvcName[80];
#define SERVICENAME _T("MYService")
SC_HANDLE schSCManager;
SC_HANDLE schService;
VOID SvcInstall();
VOID DisplayUsage(void);
VOID StartSvc(void);
VOID uninstallSvc();
VOID Run();
VOID Run_Main(DWORD dwArgCount, LPTSTR args[]);
SERVICE_TABLE_ENTRY s_DispatchTable[] =
{
{ (LPWSTR)SERVICENAME, (LPSERVICE_MAIN_FUNCTION)Run_Main },
{ NULL, NULL } };
void _tmain(int argc, TCHAR *argv[])
{
StringCchCopy(szCommand, 10, argv[1]);
if (lstrcmpi( szCommand, TEXT("install")) == 0 )
SvcInstall();
else if (lstrcmpi( szCommand, TEXT("start")) == 0 )
StartSvc();
else if (lstrcmpi( szCommand, TEXT("uninstall")) == 0 )
uninstallSvc();
else
{
Run();
_tprintf(TEXT("Unknown command (%s)\n\n"),
szCommand);
DisplayUsage();
}
}
VOID Run()
{
BOOL started
= ::StartServiceCtrlDispatcher( s_DispatchTable );
if( !started)
{
}
}
void Run_Main( DWORD dwArgCount, LPTSTR args[] )
{
;
}
VOID DisplayUsage()
{
printf("Description:\n");
printf("\tCommand-line tool that controls a service.\n\n");
printf("Usage:\n");
printf("\tsvccontrol [command] [service_name]\n\n");
printf("\t[command]\n");
printf("\t start\n");
printf("\t dacl\n");
printf("\t stop\n");
}
VOID SvcInstall()
{
SC_HANDLE schSCManager;
SC_HANDLE schService;
TCHAR szPath[MAX_PATH];
if( !GetModuleFileName( NULL, szPath, MAX_PATH ) )
{
printf("Cannot install service (%d)\n", GetLastError());
return;
}
// Get a handle to the SCM database.
schSCManager = OpenSCManager(
NULL, // local computer
NULL, // ServicesActive database
SC_MANAGER_ALL_ACCESS); // full access rights
if (NULL == schSCManager)
{
printf("OpenSCManager failed (%d)\n", GetLastError());
return;
}
// Create the service.
schService = CreateService(
schSCManager, // SCM database
SERVICENAME, // name of service
SERVICENAME, // service name to display
SERVICE_ALL_ACCESS, // desired access
SERVICE_WIN32_OWN_PROCESS, // service type
SERVICE_DEMAND_START, // start type
SERVICE_ERROR_NORMAL, // error control type
szPath, // path to service's binary
NULL, // no load ordering group
NULL, // no tag identifier
NULL, // no dependencies
NULL, // LocalSystem account
NULL); // no password
if (schService == NULL)
{
printf("CreateService failed (%d)\n", GetLastError());
CloseServiceHandle(schSCManager);
return;
}
else printf("Service installed successfully\n");
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
}
//
// Purpose:
// Starts the service.
//
// Parameters:
// None
//
// Return value:
// None
//
VOID StartSvc()
{
SERVICE_STATUS_PROCESS ssStatus;
DWORD dwOldCheckPoint;
DWORD dwStartTickCount;
DWORD dwWaitTime;
DWORD dwBytesNeeded;
// Get a handle to the SCM database.
schSCManager = OpenSCManager(
NULL, // local computer
NULL, // ServicesActive database
SC_MANAGER_ALL_ACCESS); // full access rights
if (NULL == schSCManager)
{
printf("OpenSCManager failed (%d)\n", GetLastError());
return;
}
// Get a handle to the service
schService = OpenService(
schSCManager, // SCM database
szSvcName, // name of service
SERVICE_ALL_ACCESS); // full access
if (schService == NULL)
{
printf("OpenService failed (%d)\n", GetLastError());
CloseServiceHandle(schSCManager);
return;
}
// Attempt to start the service.
if (!StartService(
schService, // handle to service
0, // number of arguments
NULL) ) // no arguments
{
printf("StartService failed (%d)\n", GetLastError());
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
return;
}
else printf("Service start pending...\n");
// Check the status until the service is no longer start pending.
if (!QueryServiceStatusEx(
schService, // handle to service
SC_STATUS_PROCESS_INFO, // info level
(LPBYTE) &ssStatus, // address of structure
sizeof(SERVICE_STATUS_PROCESS), // size of structure
&dwBytesNeeded ) ) // if buffer too small
{
return;
}
// Save the tick count and initial checkpoint.
dwStartTickCount = GetTickCount();
dwOldCheckPoint = ssStatus.dwCheckPoint;
while (ssStatus.dwCurrentState == SERVICE_START_PENDING)
{
// Do not wait longer than the wait hint. A good interval is
// one-tenth the wait hint, but no less than 1 second and no
// more than 10 seconds.
dwWaitTime = ssStatus.dwWaitHint / 10;
if( dwWaitTime < 1000 )
dwWaitTime = 1000;
else if ( dwWaitTime > 10000 )
dwWaitTime = 10000;
Sleep( dwWaitTime );
// Check the status again.
if (!QueryServiceStatusEx(
schService, // handle to service
SC_STATUS_PROCESS_INFO, // info level
(LPBYTE) &ssStatus, // address of structure
sizeof(SERVICE_STATUS_PROCESS), // size of structure
&dwBytesNeeded ) ) // if buffer too small
break;
if ( ssStatus.dwCheckPoint > dwOldCheckPoint )
{
// The service is making progress.
dwStartTickCount = GetTickCount();
dwOldCheckPoint = ssStatus.dwCheckPoint;
}
else
{
if(GetTickCount()-dwStartTickCount > ssStatus.dwWaitHint)
{
// No progress made within the wait hint.
break;
}
}
}
// Determine whether the service is running.
if (ssStatus.dwCurrentState == SERVICE_RUNNING)
{
printf("Service started successfully.\n");
}
else
{
printf("Service not started. \n");
printf(" Current State: %d\n", ssStatus.dwCurrentState);
printf(" Exit Code: %d\n", ssStatus.dwWin32ExitCode);
printf(" Check Point: %d\n", ssStatus.dwCheckPoint);
printf(" Wait Hint: %d\n", ssStatus.dwWaitHint);
}
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
}
void uninstallSvc()
{
if( schSCManager == 0 )
{
schSCManager
= ::OpenSCManager( NULL, // local
computer
NULL, // ServicesActive database
SC_MANAGER_ALL_ACCESS); // full access rights
if (NULL == schSCManager)
{
//_tprintf(_T("OpenSCManager failed,
error: %u
\n"), ::GetLastError() );
return;
}
}
if( schService == 0 )
{
schService
= ::OpenService( schSCManager,SERVICENAME ,
SC_MANAGER_ALL_ACCESS );
if( schService == 0 )
{
//_tprintf( SERVICENAME _T(" not found.
\n") );
}
}
if( schService != 0 )
{
BOOL ok = ::DeleteService( schService );
if( ok )
{
_tprintf( _T("Service deleted\n") );
}
else
{
DWORD dwError = ::GetLastError();
_tprintf( _T("DeleteService failed,
error: %u\n"), dwError );
}
::CloseServiceHandle( schService );
schService = 0;
}
if( schSCManager != 0 )
::CloseServiceHandle( schSCManager );
}
Thanks in advance.
Rs.
i have created a simple Service by reading the MSDN. i have installed
the service successfully, but when i try to start the service its
throwing the exception as below:
Windows could not start the MYService on Local Computer.
Error 1053: The service did not respond to the start or control
request in a timely fashion.
i have pasted my code in below, kindly can anybody help me where i
did
wrong.
i am working on Windows 7 OS and VS 2010.
TCHAR szCommand[10];
TCHAR szSvcName[80];
#define SERVICENAME _T("MYService")
SC_HANDLE schSCManager;
SC_HANDLE schService;
VOID SvcInstall();
VOID DisplayUsage(void);
VOID StartSvc(void);
VOID uninstallSvc();
VOID Run();
VOID Run_Main(DWORD dwArgCount, LPTSTR args[]);
SERVICE_TABLE_ENTRY s_DispatchTable[] =
{
{ (LPWSTR)SERVICENAME, (LPSERVICE_MAIN_FUNCTION)Run_Main },
{ NULL, NULL } };
void _tmain(int argc, TCHAR *argv[])
{
StringCchCopy(szCommand, 10, argv[1]);
if (lstrcmpi( szCommand, TEXT("install")) == 0 )
SvcInstall();
else if (lstrcmpi( szCommand, TEXT("start")) == 0 )
StartSvc();
else if (lstrcmpi( szCommand, TEXT("uninstall")) == 0 )
uninstallSvc();
else
{
Run();
_tprintf(TEXT("Unknown command (%s)\n\n"),
szCommand);
DisplayUsage();
}
}
VOID Run()
{
BOOL started
= ::StartServiceCtrlDispatcher( s_DispatchTable );
if( !started)
{
}
}
void Run_Main( DWORD dwArgCount, LPTSTR args[] )
{
;
}
VOID DisplayUsage()
{
printf("Description:\n");
printf("\tCommand-line tool that controls a service.\n\n");
printf("Usage:\n");
printf("\tsvccontrol [command] [service_name]\n\n");
printf("\t[command]\n");
printf("\t start\n");
printf("\t dacl\n");
printf("\t stop\n");
}
VOID SvcInstall()
{
SC_HANDLE schSCManager;
SC_HANDLE schService;
TCHAR szPath[MAX_PATH];
if( !GetModuleFileName( NULL, szPath, MAX_PATH ) )
{
printf("Cannot install service (%d)\n", GetLastError());
return;
}
// Get a handle to the SCM database.
schSCManager = OpenSCManager(
NULL, // local computer
NULL, // ServicesActive database
SC_MANAGER_ALL_ACCESS); // full access rights
if (NULL == schSCManager)
{
printf("OpenSCManager failed (%d)\n", GetLastError());
return;
}
// Create the service.
schService = CreateService(
schSCManager, // SCM database
SERVICENAME, // name of service
SERVICENAME, // service name to display
SERVICE_ALL_ACCESS, // desired access
SERVICE_WIN32_OWN_PROCESS, // service type
SERVICE_DEMAND_START, // start type
SERVICE_ERROR_NORMAL, // error control type
szPath, // path to service's binary
NULL, // no load ordering group
NULL, // no tag identifier
NULL, // no dependencies
NULL, // LocalSystem account
NULL); // no password
if (schService == NULL)
{
printf("CreateService failed (%d)\n", GetLastError());
CloseServiceHandle(schSCManager);
return;
}
else printf("Service installed successfully\n");
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
}
//
// Purpose:
// Starts the service.
//
// Parameters:
// None
//
// Return value:
// None
//
VOID StartSvc()
{
SERVICE_STATUS_PROCESS ssStatus;
DWORD dwOldCheckPoint;
DWORD dwStartTickCount;
DWORD dwWaitTime;
DWORD dwBytesNeeded;
// Get a handle to the SCM database.
schSCManager = OpenSCManager(
NULL, // local computer
NULL, // ServicesActive database
SC_MANAGER_ALL_ACCESS); // full access rights
if (NULL == schSCManager)
{
printf("OpenSCManager failed (%d)\n", GetLastError());
return;
}
// Get a handle to the service
schService = OpenService(
schSCManager, // SCM database
szSvcName, // name of service
SERVICE_ALL_ACCESS); // full access
if (schService == NULL)
{
printf("OpenService failed (%d)\n", GetLastError());
CloseServiceHandle(schSCManager);
return;
}
// Attempt to start the service.
if (!StartService(
schService, // handle to service
0, // number of arguments
NULL) ) // no arguments
{
printf("StartService failed (%d)\n", GetLastError());
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
return;
}
else printf("Service start pending...\n");
// Check the status until the service is no longer start pending.
if (!QueryServiceStatusEx(
schService, // handle to service
SC_STATUS_PROCESS_INFO, // info level
(LPBYTE) &ssStatus, // address of structure
sizeof(SERVICE_STATUS_PROCESS), // size of structure
&dwBytesNeeded ) ) // if buffer too small
{
return;
}
// Save the tick count and initial checkpoint.
dwStartTickCount = GetTickCount();
dwOldCheckPoint = ssStatus.dwCheckPoint;
while (ssStatus.dwCurrentState == SERVICE_START_PENDING)
{
// Do not wait longer than the wait hint. A good interval is
// one-tenth the wait hint, but no less than 1 second and no
// more than 10 seconds.
dwWaitTime = ssStatus.dwWaitHint / 10;
if( dwWaitTime < 1000 )
dwWaitTime = 1000;
else if ( dwWaitTime > 10000 )
dwWaitTime = 10000;
Sleep( dwWaitTime );
// Check the status again.
if (!QueryServiceStatusEx(
schService, // handle to service
SC_STATUS_PROCESS_INFO, // info level
(LPBYTE) &ssStatus, // address of structure
sizeof(SERVICE_STATUS_PROCESS), // size of structure
&dwBytesNeeded ) ) // if buffer too small
break;
if ( ssStatus.dwCheckPoint > dwOldCheckPoint )
{
// The service is making progress.
dwStartTickCount = GetTickCount();
dwOldCheckPoint = ssStatus.dwCheckPoint;
}
else
{
if(GetTickCount()-dwStartTickCount > ssStatus.dwWaitHint)
{
// No progress made within the wait hint.
break;
}
}
}
// Determine whether the service is running.
if (ssStatus.dwCurrentState == SERVICE_RUNNING)
{
printf("Service started successfully.\n");
}
else
{
printf("Service not started. \n");
printf(" Current State: %d\n", ssStatus.dwCurrentState);
printf(" Exit Code: %d\n", ssStatus.dwWin32ExitCode);
printf(" Check Point: %d\n", ssStatus.dwCheckPoint);
printf(" Wait Hint: %d\n", ssStatus.dwWaitHint);
}
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
}
void uninstallSvc()
{
if( schSCManager == 0 )
{
schSCManager
= ::OpenSCManager( NULL, // local
computer
NULL, // ServicesActive database
SC_MANAGER_ALL_ACCESS); // full access rights
if (NULL == schSCManager)
{
//_tprintf(_T("OpenSCManager failed,
error: %u
\n"), ::GetLastError() );
return;
}
}
if( schService == 0 )
{
schService
= ::OpenService( schSCManager,SERVICENAME ,
SC_MANAGER_ALL_ACCESS );
if( schService == 0 )
{
//_tprintf( SERVICENAME _T(" not found.
\n") );
}
}
if( schService != 0 )
{
BOOL ok = ::DeleteService( schService );
if( ok )
{
_tprintf( _T("Service deleted\n") );
}
else
{
DWORD dwError = ::GetLastError();
_tprintf( _T("DeleteService failed,
error: %u\n"), dwError );
}
::CloseServiceHandle( schService );
schService = 0;
}
if( schSCManager != 0 )
::CloseServiceHandle( schSCManager );
}
Thanks in advance.
Rs.