Thread: running my program as service

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    6

    running my program as service

    Hi all,

    i am trying to run my c++ program as service but it fails to when i install the service so i can not see it on service list. (i tried myprogram.exe install)

    NOTE that i am using visual studio 2005 on VISTA

    here's the code:

    Code:
    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
    		{
    			printf("blablabla");
    			/*while(true){
    				WCHAR name[20];
    				ctow(name,PROC_NAME);
    				unsigned long gwID    = GetTargetProcessIdFromProcname(name);
    				if (gwID >0){
    				//printf("found %s, pid: %d",PROC_NAME, gwID);
    				}
    				else{
    					printf("not found");
    					std::system("Gadget_Server_V1.27.333.exe");
    					Sleep(10000);
    				}*/
    			
    			//Beep( 1000, 100 );
    			
    
    		}
    		while ( WaitForSingleObject( stopServiceEvent, 1000 ) == 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 }
    	};
       BOOL success;
      	success = StartServiceCtrlDispatcher( serviceTable );
       if (!success )
       {
    	   printf("Service Error");
        	
        }
    }
    
    void InstallService()
    {
    	SC_HANDLE serviceControlManager = OpenSCManager( 0, 0, SC_MANAGER_CREATE_SERVICE );
    
    	if ( serviceControlManager )
    	{
    		char path[ _MAX_PATH + 1 ];
    		WCHAR pa[20];
    		ctow(pa,path);
    		if ( GetModuleFileName( 0, pa, 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, pa,
    							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[] )
    {
    	if ( argc > 1 && lstrcmpi( argv[1], TEXT("install") ) == 0 )
    	{
    		InstallService();
    	}
    	else if ( argc > 1 && lstrcmpi( argv[1], TEXT("uninstall") ) == 0 )
    	{
    		UninstallService();
    	}
    	else
    	{
    		RunService();
    	}
    
    	return 0;
    }
    and here is the system lof file:

    An unhandled win32 exception occurred in Watchdog.exe [7524]. Just-In-Time debugging this exception failed with the following error: Either a required impersonation level was not provided, or the provided impersonation level is invalid. Check the documentation index for 'Just-in-time debugging, errors' for more information.
    42050780


    thanks in advance for your help

    Best Regards

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Quote Originally Posted by Obvious Error is Obvious
    Either a required impersonation level was not provided, or the provided impersonation level is invalid.
    So, I would guess this is a Vista permissions issue. You probably need to be an Administrator on the system to install it.

  3. #3
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Why are you doing service installation stuff in ServiceMain for one, and it looks lik eyou are using a default security descriptor (or whatever they call it on VISTA, impersonation thingy), which means you have to be the admin to install it.

  4. #4
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    He isn't actually doing installation stuff in ServiceMain(), that register thing is supposed to be called in run-time.

    But yeah, it's another one of these "Run as an administrator" problems. :P
    Last edited by maxorator; 09-05-2008 at 10:26 AM.

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Just have your program throw up a "Run as an administrator" annoying boxes that ........es everyone who uses Vista off.

  6. #6
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Personally I just use the example straight from MSDN. It does still need to be installed from an admin accoutn though.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Running program in unix
    By Cpro in forum Linux Programming
    Replies: 2
    Last Post: 02-10-2008, 09:28 PM
  2. Running my program in the background
    By shoobsie in forum Windows Programming
    Replies: 4
    Last Post: 10-09-2005, 02:38 AM
  3. Replies: 3
    Last Post: 09-05-2005, 08:57 AM
  4. Why is my program running away?
    By badkitty in forum C++ Programming
    Replies: 4
    Last Post: 09-19-2001, 04:27 PM
  5. Running program
    By muffin in forum C Programming
    Replies: 5
    Last Post: 08-30-2001, 10:57 AM