如题

解决方案 »

  1.   

    Installing a ServiceA service configuration program uses the CreateService function to install a service in a SCM database. The application-defined schSCManager handle must have SC_MANAGER_CREATE_SERVICE access to the SCManager object. The following example shows how to install a service.
    VOID CreateSampleService() 

        LPCTSTR lpszBinaryPathName = 
            "%SystemRoot%\\system\\testserv.exe"; 
     
        schService = CreateService( 
            schSCManager,              // SCManager database 
            "Sample_Srv",              // name of service 
            lpszDisplayName,           // 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 
            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) 
            MyErrorExit("CreateService"); 
        else 
            printf("CreateService SUCCESS.\n"); 
     
        CloseServiceHandle(schService); 
    }
      

  2.   

    Deleting a ServiceIn the following example, a service configuration program uses the OpenService function to get a handle with DELETE access to an installed service object. The program then uses the service object handle in the DeleteService function to remove the service from the SCM database.
    VOID DeleteSampleService() 

        schService = OpenService( 
            schSCManager,       // SCManager database 
            "Sample_Srv",       // name of service 
            DELETE);            // only need DELETE access 
     
        if (schService == NULL) 
            MyErrorExit("OpenService"); 
     
        if (! DeleteService(schService) ) 
            MyErrorExit("DeleteService"); 
        else 
            printf("DeleteService SUCCESS\n"); 
     
        CloseServiceHandle(schService); 
    }
      

  3.   

    Starting a ServiceTo start a service, the following example opens a handle to an installed database and then specifies the handle in a call to the StartService function. It can be used to start either a service or a driver service, but this example assumes that a service is being started. After starting the service, the program uses the members of the SERVICE_STATUS structure returned by the QueryServiceStatus function to track the progress of the service.
    DWORD StartSampleService() 

        SERVICE_STATUS ssStatus; 
        DWORD dwOldCheckPoint; 
        DWORD dwStartTickCount;
        DWORD dwWaitTime;
        DWORD dwStatus;
     
        schService = OpenService( 
            schSCManager,          // SCM database 
            "Sample_Srv",          // service name
            SERVICE_ALL_ACCESS); 
     
        if (schService == NULL) 
        { 
            MyErrorExit("OpenService"); 
        }
     
        if (!StartService(
                schService,  // handle to service 
                0,           // number of arguments 
                NULL) )      // no arguments 
        {
            MyErrorExit("StartService"); 
        }
        else 
        {
            printf("Service start pending.\n"); 
        }
     
        // Check the status until the service is no longer start pending. 
     
        if (!QueryServiceStatus( 
                schService,   // handle to service 
                &ssStatus) )  // address of status information structure
        {
            MyErrorExit("QueryServiceStatus"); 
        }
     
        // 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 (!QueryServiceStatus( 
                    schService,   // handle to service 
                    &ssStatus) )  // address of structure
                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;
                }
            }
        }     if (ssStatus.dwCurrentState == SERVICE_RUNNING) 
        {
            printf("StartService SUCCESS.\n"); 
            dwStatus = NO_ERROR;
        }
        else 
        { 
            printf("\nService not started. \n");
            printf("  Current State: %d\n", ssStatus.dwCurrentState); 
            printf("  Exit Code: %d\n", ssStatus.dwWin32ExitCode); 
            printf("  Service Specific Exit Code: %d\n", 
                ssStatus.dwServiceSpecificExitCode); 
            printf("  Check Point: %d\n", ssStatus.dwCheckPoint); 
            printf("  Wait Hint: %d\n", ssStatus.dwWaitHint); 
            dwStatus = GetLastError();
        } 
     
        CloseServiceHandle(schService); 
        return dwStatus;
    }
      

  4.   

    恩~
    具体可以参看MSDN里面的Service Functions~~~