xref: /reactos/base/applications/sc/start.c (revision 8a978a17)
1 /*
2  * PROJECT:     ReactOS Services
3  * LICENSE:     GPL - See COPYING in the top level directory
4  * FILE:        base/applications/sc/start.c
5  * PURPOSE:     Start a service
6  * COPYRIGHT:   Copyright 2005 - 2006 Ged Murphy <gedmurphy@gmail.com>
7  *
8  */
9 
10 #include "sc.h"
11 
12 BOOL Start(LPCTSTR ServiceName, LPCTSTR *ServiceArgs, INT ArgCount)
13 {
14     SC_HANDLE hSCManager = NULL;
15     SC_HANDLE hSc = NULL;
16     LPSERVICE_STATUS_PROCESS pServiceInfo = NULL;
17     BOOL bResult = TRUE;
18 
19 #ifdef SCDBG
20     LPCTSTR *TmpArgs = ServiceArgs;
21     INT TmpCnt = ArgCount;
22     _tprintf(_T("service to control - %s\n"), ServiceName);
23     _tprintf(_T("Arguments:\n"));
24     while (TmpCnt)
25     {
26         _tprintf(_T("  %s\n"), *TmpArgs);
27         TmpArgs++;
28         TmpCnt--;
29     }
30     _tprintf(_T("\n"));
31 #endif /* SCDBG */
32 
33     hSCManager = OpenSCManager(NULL,
34                                NULL,
35                                SC_MANAGER_CONNECT);
36     if (hSCManager == NULL)
37     {
38         _tprintf(_T("[SC] OpenSCManager FAILED %lu:\n\n"), GetLastError());
39         bResult = FALSE;
40         goto done;
41     }
42 
43     hSc = OpenService(hSCManager,
44                       ServiceName,
45                       SERVICE_START | SERVICE_QUERY_STATUS);
46     if (hSc == NULL)
47     {
48         _tprintf(_T("[SC] OpenService FAILED %lu:\n\n"), GetLastError());
49         bResult = FALSE;
50         goto done;
51     }
52 
53     if (!ArgCount)
54     {
55         ServiceArgs = NULL;
56     }
57 
58     if (!StartService(hSc,
59                       ArgCount,
60                       ServiceArgs))
61     {
62         _tprintf(_T("[SC] StartService FAILED %lu:\n\n"), GetLastError());
63         bResult = FALSE;
64         goto done;
65     }
66 
67     pServiceInfo = QueryService(ServiceName);
68     if (pServiceInfo != NULL)
69     {
70         PrintService(ServiceName,
71                      NULL,
72                      pServiceInfo,
73                      TRUE);
74 
75         HeapFree(GetProcessHeap(), 0, pServiceInfo);
76     }
77 
78 done:
79     if (bResult == FALSE)
80         ReportLastError();
81 
82     if (hSc)
83         CloseServiceHandle(hSc);
84 
85     if (hSCManager)
86         CloseServiceHandle(hSCManager);
87 
88     return bResult;
89 }
90