Exemple #1
0
BOOL Start(LPCTSTR ServiceName, LPCTSTR *ServiceArgs, INT ArgCount)
{
    SC_HANDLE hSCManager = NULL;
    SC_HANDLE hSc = NULL;
    LPSERVICE_STATUS_PROCESS pServiceInfo = NULL;

#ifdef SCDBG
    LPCTSTR *TmpArgs = ServiceArgs;
    INT TmpCnt = ArgCount;
    _tprintf(_T("service to control - %s\n"), ServiceName);
    _tprintf(_T("Arguments:\n"));
    while (TmpCnt)
    {
        _tprintf(_T("  %s\n"), *TmpArgs);
        TmpArgs++;
        TmpCnt--;
    }
    _tprintf(_T("\n"));
#endif /* SCDBG */

    hSCManager = OpenSCManager(NULL,
                               NULL,
                               SC_MANAGER_CONNECT);
    if (hSCManager == NULL)
    {
        ReportLastError();
        return FALSE;
    }

    hSc = OpenService(hSCManager,
                      ServiceName,
                      SERVICE_START | SERVICE_QUERY_STATUS);

    if (hSc == NULL)
        goto fail;

    if (!ArgCount)
    {
        ServiceArgs = NULL;
    }

    if (! StartService(hSc,
                       ArgCount,
                       ServiceArgs))
    {
        _tprintf(_T("[SC] StartService FAILED %lu:\n\n"), GetLastError());
        goto fail;
    }

    pServiceInfo = QueryService(ServiceName);
    if (pServiceInfo != NULL)
    {
        PrintService(ServiceName,
                     pServiceInfo,
                     TRUE);
    }

    HeapFree(GetProcessHeap(), 0, pServiceInfo);
    CloseServiceHandle(hSc);
    CloseServiceHandle(hSCManager);

    return TRUE;

fail:
    ReportLastError();
    if (hSc) CloseServiceHandle(hSc);
    if (hSCManager) CloseServiceHandle(hSCManager);
    return FALSE;

}
Exemple #2
0
BOOL
Query(LPCTSTR *ServiceArgs,
      DWORD ArgCount,
      BOOL bExtended)
{
    LPENUM_SERVICE_STATUS_PROCESS pServiceStatus = NULL;
    DWORD NumServices = 0;
    //DWORD ServiceType;
    //DWORD ServiceState;
    BOOL bServiceName = TRUE;
    DWORD OptSize, i;

    LPCTSTR *TmpArgs;
    INT TmpCnt;

#ifdef SCDBG
    TmpArgs = ServiceArgs;
    TmpCnt = ArgCount;
    _tprintf(_T("Arguments:\n"));
    while (TmpCnt)
    {
        _tprintf(_T("  %s\n"), *TmpArgs);
        TmpArgs++;
        TmpCnt--;
    }
    _tprintf(_T("\n"));
#endif /* SCDBG */

    /* display all running services and drivers */
    if (ArgCount == 0)
    {
        NumServices = EnumServices(&pServiceStatus,
                                   SERVICE_WIN32,
                                   SERVICE_ACTIVE);

        if (NumServices != 0)
        {
            for (i=0; i < NumServices; i++)
            {
                PrintService(pServiceStatus[i].lpServiceName,
                             &pServiceStatus[i].ServiceStatusProcess,
                             bExtended);
            }

            _tprintf(_T("number : %lu\n"), NumServices);

            if (pServiceStatus)
                HeapFree(GetProcessHeap(), 0, pServiceStatus);

            return TRUE;
        }

        return FALSE;
    }

    TmpArgs = ServiceArgs;
    TmpCnt = ArgCount;
    OptSize = sizeof(QueryOpts) / sizeof(QueryOpts[0]);
    while (TmpCnt--)
    {
        for (i=0; i < OptSize; i++)
        {
            if (!lstrcmpi(*TmpArgs, QueryOpts[i]))
            {
                bServiceName = FALSE;
            }
        }
        TmpArgs++;
    }


    /* FIXME: parse options */


    /* print only the service requested */
    if (bServiceName)
    {
        LPSERVICE_STATUS_PROCESS pStatus;
        LPCTSTR ServiceName = *ServiceArgs;

        pStatus = QueryService(ServiceName);
        if (pStatus)
        {
            PrintService(ServiceName,
                         pStatus,
                         bExtended);
        }
    }

    return TRUE;
}