ReactOS  0.4.11-dev-946-g431643b
start.c
Go to the documentation of this file.
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,
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,
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)
81 
82  if (hSc)
83  CloseServiceHandle(hSc);
84 
85  if (hSCManager)
86  CloseServiceHandle(hSCManager);
87 
88  return bResult;
89 }
#define StartService
Definition: winsvc.h:585
#define _tprintf
Definition: tchar.h:506
#define TRUE
Definition: types.h:120
const CHAR * LPCTSTR
Definition: xmlstorage.h:193
#define SC_MANAGER_CONNECT
Definition: winsvc.h:14
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1059
int32_t INT
Definition: typedefs.h:56
#define OpenService
Definition: winsvc.h:576
BOOL WINAPI CloseServiceHandle(SC_HANDLE hSCObject)
Definition: scm.c:576
smooth NULL
Definition: ftsmooth.c:416
#define SERVICE_QUERY_STATUS
Definition: winsvc.h:55
BOOL Start(LPCTSTR ServiceName, LPCTSTR *ServiceArgs, INT ArgCount)
Definition: start.c:12
#define OpenSCManager
Definition: winsvc.h:575
#define _T(x)
Definition: vfdio.h:22
unsigned int BOOL
Definition: ntddk_ex.h:94
LPTSTR ServiceName
Definition: ServiceMain.c:15
#define GetProcessHeap()
Definition: compat.h:395
SC_HANDLE hSCManager
Definition: sc.c:12
#define SERVICE_START
Definition: winsvc.h:57
VOID ReportLastError(VOID)
Definition: sc.c:15
LPSERVICE_STATUS_PROCESS QueryService(LPCTSTR ServiceName)
Definition: query.c:13
#define HeapFree(x, y, z)
Definition: compat.h:394
VOID PrintService(LPCTSTR lpServiceName, LPCTSTR lpDisplayName, LPSERVICE_STATUS_PROCESS pStatus, BOOL bExtended)
Definition: print.c:13