Sync with trunk r62754.
[reactos.git] / base / applications / sc / start.c
1 /*
2 * PROJECT: ReactOS Services
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: base/system/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
18 #ifdef SCDBG
19 LPCTSTR *TmpArgs = ServiceArgs;
20 INT TmpCnt = ArgCount;
21 _tprintf(_T("service to control - %s\n"), ServiceName);
22 _tprintf(_T("Arguments:\n"));
23 while (TmpCnt)
24 {
25 _tprintf(_T(" %s\n"), *TmpArgs);
26 TmpArgs++;
27 TmpCnt--;
28 }
29 _tprintf(_T("\n"));
30 #endif /* SCDBG */
31
32 hSCManager = OpenSCManager(NULL,
33 NULL,
34 SC_MANAGER_CONNECT);
35 if (hSCManager == NULL)
36 {
37 ReportLastError();
38 return FALSE;
39 }
40
41 hSc = OpenService(hSCManager,
42 ServiceName,
43 SERVICE_START | SERVICE_QUERY_STATUS);
44
45 if (hSc == NULL)
46 goto fail;
47
48 if (!ArgCount)
49 {
50 ServiceArgs = NULL;
51 }
52
53 if (! StartService(hSc,
54 ArgCount,
55 ServiceArgs))
56 {
57 _tprintf(_T("[SC] StartService FAILED %lu:\n\n"), GetLastError());
58 goto fail;
59 }
60
61 pServiceInfo = QueryService(ServiceName);
62 if (pServiceInfo != NULL)
63 {
64 PrintService(ServiceName,
65 pServiceInfo,
66 TRUE);
67 }
68
69 HeapFree(GetProcessHeap(), 0, pServiceInfo);
70 CloseServiceHandle(hSc);
71 CloseServiceHandle(hSCManager);
72
73 return TRUE;
74
75 fail:
76 ReportLastError();
77 if (hSc) CloseServiceHandle(hSc);
78 if (hSCManager) CloseServiceHandle(hSCManager);
79 return FALSE;
80
81 }