CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 2010
    Posts
    172

    Exclamation Service error 1053 windows 7

    Why will my service not run in windows 7 it produces error 1053 everytime I try to run it

  2. #2
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Service error 1053 windows 7

    Error 1053: The service did not respond to the start or control request in a timely fashion.
    This is why.
    Best regards,
    Igor

  3. #3
    Join Date
    Apr 2010
    Posts
    172

    Re: Service error 1053 windows 7

    do you mind if you explain why the code is doing that please?
    Code:
    #include <windows.h>
    #include "iostream"
    #include <tchar.h>
    
    TCHAR* serviceName = TEXT("Service1");
    SERVICE_STATUS serviceStatus;
    SERVICE_STATUS_HANDLE serviceStatusHandle = 0;
    HANDLE stopServiceEvent = 0;
    
    void WINAPI ServiceControlHandler( DWORD controlCode )
    {
    	switch ( controlCode )
    	{
    		case SERVICE_CONTROL_INTERROGATE:
    			break;
    
    		case SERVICE_CONTROL_SHUTDOWN:
    		case SERVICE_CONTROL_STOP:
    			serviceStatus.dwCurrentState = SERVICE_STOP_PENDING;
    			SetServiceStatus( serviceStatusHandle, &serviceStatus );
    
    			SetEvent( stopServiceEvent );
    			return;
    
    		case SERVICE_CONTROL_PAUSE:
    			break;
    
    		case SERVICE_CONTROL_CONTINUE:
    			break;
    
    		default:
    			if ( controlCode >= 128 && controlCode <= 255 )
    				// user defined control code
    				break;
    			else
    				// unrecognised control code
    				break;
    	}
    
    	SetServiceStatus( serviceStatusHandle, &serviceStatus );
    }
    
    void WINAPI ServiceMain( DWORD /*argc*/, TCHAR* /*argv*/[] )
    {
    	// initialise service status
    	serviceStatus.dwServiceType = SERVICE_WIN32;
    	serviceStatus.dwCurrentState = SERVICE_STOPPED;
    	serviceStatus.dwControlsAccepted = 0;
    	serviceStatus.dwWin32ExitCode = NO_ERROR;
    	serviceStatus.dwServiceSpecificExitCode = NO_ERROR;
    	serviceStatus.dwCheckPoint = 0;
    	serviceStatus.dwWaitHint = 0;
    
    	serviceStatusHandle = RegisterServiceCtrlHandler( serviceName, ServiceControlHandler );
    
    	if ( serviceStatusHandle )
    	{
    		// service is starting
    		serviceStatus.dwCurrentState = SERVICE_START_PENDING;
    		SetServiceStatus( serviceStatusHandle, &serviceStatus );
    
    		// do initialisation here
    		stopServiceEvent = CreateEvent( 0, FALSE, FALSE, 0 );
    
    		// running
    		serviceStatus.dwControlsAccepted |= (SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN);
    		serviceStatus.dwCurrentState = SERVICE_RUNNING;
    		SetServiceStatus( serviceStatusHandle, &serviceStatus );
    
    		do
    		{
    			//Beep( 1000, 100 );
    		}
    		while ( WaitForSingleObject( stopServiceEvent, 5000 ) == WAIT_TIMEOUT );
    
    		// service was stopped
    		serviceStatus.dwCurrentState = SERVICE_STOP_PENDING;
    		SetServiceStatus( serviceStatusHandle, &serviceStatus );
    
    		// do cleanup here
    		CloseHandle( stopServiceEvent );
    		stopServiceEvent = 0;
    
    		// service is now stopped
    		serviceStatus.dwControlsAccepted &= ~(SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN);
    		serviceStatus.dwCurrentState = SERVICE_STOPPED;
    		SetServiceStatus( serviceStatusHandle, &serviceStatus );
    	}
    }
    
    void RunService()
    {
    	SERVICE_TABLE_ENTRY serviceTable[] =
    	{
    		{ serviceName, ServiceMain },
    		{ 0, 0 }
    	};
    
    	if(!StartServiceCtrlDispatcher( serviceTable ))
    	{
    		std::cout <<GetLastError();Also so produces 1063
    	}
    	
    }
    
    void InstallService()
    {
    	SC_HANDLE serviceControlManager = OpenSCManager( 0, 0, SC_MANAGER_CREATE_SERVICE );
    
    	if ( serviceControlManager )
    	{
    		TCHAR path[ _MAX_PATH + 1 ];
    		if ( GetModuleFileName( 0, path, sizeof(path)/sizeof(path[0]) ) > 0 )
    		{
    			SC_HANDLE service = CreateService( serviceControlManager,
    							serviceName, serviceName,
    							SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
    							SERVICE_AUTO_START, SERVICE_ERROR_IGNORE, path,
    							0, 0, 0, 0, 0 );
    			if ( service )
    				CloseServiceHandle( service );
    		}
    
    		CloseServiceHandle( serviceControlManager );
    	}
    }
    
    void UninstallService()
    {
    	SC_HANDLE serviceControlManager = OpenSCManager( 0, 0, SC_MANAGER_CONNECT );
    
    	if ( serviceControlManager )
    	{
    		SC_HANDLE service = OpenService( serviceControlManager,
    			serviceName, SERVICE_QUERY_STATUS | DELETE );
    		if ( service )
    		{
    			SERVICE_STATUS serviceStatus;
    			if ( QueryServiceStatus( service, &serviceStatus ) )
    			{
    				if ( serviceStatus.dwCurrentState == SERVICE_STOPPED )
    					DeleteService( service );
    			}
    
    			CloseServiceHandle( service );
    		}
    
    		CloseServiceHandle( serviceControlManager );
    	}
    }
    
    int _tmain( int argc, TCHAR* argv[] )
    {
    	int choice = 0;
    	std::cout <<"Type a choice";
    	std::cin >> choice;
    	switch(choice)
    	{
    	case 0:
    		InstallService();
    	break;
    	case 1:
    		UninstallService();
    	break;
    	case 2:
    		RunService();
    	break;
    	}
    	return 0;
    }

  4. #4
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Service error 1053 windows 7

    Sure. The problem part is main().
    Code:
    int _tmain( int argc, TCHAR* argv[] )
    {
    	int choice = 0;
    	std::cout <<"Type a choice";
    	std::cin >> choice;
    	switch(choice)
    	{
    While it's okay to do this in user mode, service mode must not do that waiting on input. The thing is that service starts in isolated session user just has no chance to input to. So, service must start based either on no question, or on a command line argument.

    Here's your main() fixed. Your service runs just fine with it.
    Code:
    int _tmain( int argc, TCHAR* argv[] )
    {
    	int choice = 2;
    
    	if (argc > 1)
    	{
    		choice = _ttoi(argv[1]);
    	}
    
    	switch(choice)
    	{
    	case 0:
    		InstallService();
    	break;
    	case 1:
    		UninstallService();
    	break;
    	case 2:
    		RunService();
    	break;
    	}
    	return 0;
    }
    BTW, as you can see, the issue has nothing to do with Windows 7. Your original service is not able to run in any Windows.
    Last edited by Igor Vartanov; January 28th, 2012 at 01:02 AM.
    Best regards,
    Igor

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured