ReactOS  0.4.12-dev-714-gfaac916
cmdStart.c
Go to the documentation of this file.
1 /*
2  * COPYRIGHT: See COPYING in the top level directory
3  * PROJECT: ReactOS net command
4  * FILE: base/applications/network/net/cmdStart.c
5  * PURPOSE:
6  *
7  * PROGRAMMERS: Magnus Olsen (greatlord@reactos.org)
8  */
9 
10 #include "net.h"
11 
12 /* Enumerate all running services */
13 static
14 INT
16 {
17  SC_HANDLE hManager = NULL;
18  SC_HANDLE hService = NULL;
19  DWORD dwBufferSize = 0;
20  DWORD dwServiceCount;
21  DWORD dwResumeHandle = 0;
22  LPENUM_SERVICE_STATUSW lpServiceBuffer = NULL;
23  INT i;
24  INT nError = 0;
25  DWORD dwError = ERROR_SUCCESS;
26 
27  hManager = OpenSCManagerW(NULL,
30  if (hManager == NULL)
31  {
32  dwError = GetLastError();
33  nError = 1;
34  goto done;
35  }
36 
37  EnumServicesStatusW(hManager,
40  NULL,
41  0,
42  &dwBufferSize,
43  &dwServiceCount,
44  &dwResumeHandle);
45 
46  if (dwBufferSize != 0)
47  {
48  lpServiceBuffer = HeapAlloc(GetProcessHeap(), 0, dwBufferSize);
49  if (lpServiceBuffer != NULL)
50  {
51  if (EnumServicesStatusW(hManager,
54  lpServiceBuffer,
55  dwBufferSize,
56  &dwBufferSize,
57  &dwServiceCount,
58  &dwResumeHandle))
59  {
60  ConPuts(StdOut, L"The following services have been started:\n\n");
61 
62  for (i = 0; i < dwServiceCount; i++)
63  {
64  ConPrintf(StdOut, L" %s\n", lpServiceBuffer[i].lpDisplayName);
65  }
66  }
67 
68  HeapFree(GetProcessHeap(), 0, lpServiceBuffer);
69  }
70  }
71 
72 done:
73  if (hService != NULL)
74  CloseServiceHandle(hService);
75 
76  if (hManager != NULL)
77  CloseServiceHandle(hManager);
78 
79  if (dwError != ERROR_SUCCESS)
80  {
81  /* FIXME: Print proper error message */
82  ConPrintf(StdErr, L"Error: %lu\n", dwError);
83  }
84 
85  return nError;
86 }
87 
88 /* Start the service argv[2] */
89 static
90 INT
92 {
93  SC_HANDLE hManager = NULL;
94  SC_HANDLE hService = NULL;
95  LPCWSTR *lpArgVectors = NULL;
96  DWORD dwError = ERROR_SUCCESS;
97  INT nError = 0;
98  INT i;
99 
100  hManager = OpenSCManagerW(NULL,
103  if (hManager == NULL)
104  {
105  dwError = GetLastError();
106  nError = 1;
107  goto done;
108  }
109 
110  hService = OpenServiceW(hManager,
111  argv[2],
112  SERVICE_START);
113  if (hService == NULL)
114  {
115  dwError = GetLastError();
116  nError = 1;
117  goto done;
118  }
119 
120  lpArgVectors = HeapAlloc(GetProcessHeap(),
121  0,
122  (argc - 2) * sizeof(LPCWSTR));
123  if (lpArgVectors == NULL)
124  {
125  dwError = GetLastError();
126  nError = 1;
127  goto done;
128  }
129 
130  for (i = 2; i < argc; i++)
131  {
132  lpArgVectors[i - 2] = argv[i];
133  }
134 
135  if (!StartServiceW(hService,
136  (DWORD)argc - 2,
137  lpArgVectors))
138  {
139  dwError = GetLastError();
140  nError = 1;
141  }
142 
143 done:
144  if (lpArgVectors != NULL)
145  HeapFree(GetProcessHeap(), 0, (LPVOID)lpArgVectors);
146 
147  if (hService != NULL)
148  CloseServiceHandle(hService);
149 
150  if (hManager != NULL)
151  CloseServiceHandle(hManager);
152 
153  if (dwError != ERROR_SUCCESS)
154  {
155  /* FIXME: Print proper error message */
156  ConPrintf(StdErr, L"Error: %lu\n", dwError);
157  }
158 
159  return nError;
160 }
161 
162 INT
164 {
165  INT i;
166 
167  if (argc == 2)
168  {
169  return EnumerateRunningServices();
170  }
171 
172  for (i = 2; i < argc; i++)
173  {
174  if (_wcsicmp(argv[i], L"/help") == 0)
175  {
177  PrintNetMessage(MSG_START_SYNTAX);
178  PrintNetMessage(MSG_START_HELP);
179  return 1;
180  }
181  }
182 
183  return StartOneService(argc, argv);
184 }
static int argc
Definition: ServiceArgs.c:12
#define SC_MANAGER_ENUMERATE_SERVICE
Definition: winsvc.h:16
#define ERROR_SUCCESS
Definition: deptool.c:10
#define SERVICES_ACTIVE_DATABASE
Definition: winsvc.h:564
__wchar_t WCHAR
Definition: xmlstorage.h:180
const WCHAR * LPCWSTR
Definition: xmlstorage.h:185
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1059
INT cmdStart(INT argc, WCHAR **argv)
Definition: cmdStart.c:163
static INT StartOneService(INT argc, WCHAR **argv)
Definition: cmdStart.c:91
VOID PrintNetMessage(DWORD dwMessage)
Definition: main.c:149
#define argv
Definition: mplay32.c:18
int32_t INT
Definition: typedefs.h:56
BOOL WINAPI EnumServicesStatusW(SC_HANDLE hSCManager, DWORD dwServiceType, DWORD dwServiceState, LPENUM_SERVICE_STATUSW lpServices, DWORD cbBufSize, LPDWORD pcbBytesNeeded, LPDWORD lpServicesReturned, LPDWORD lpResumeHandle)
Definition: scm.c:1311
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
#define IDS_GENERIC_SYNTAX
Definition: resource.h:118
SC_HANDLE WINAPI OpenServiceW(SC_HANDLE hSCManager, LPCWSTR lpServiceName, DWORD dwDesiredAccess)
Definition: scm.c:2103
INT __cdecl ConPrintf(IN PCON_STREAM Stream, IN LPWSTR szStr,...)
Definition: outstream.c:520
BOOL WINAPI CloseServiceHandle(SC_HANDLE hSCObject)
Definition: scm.c:577
smooth NULL
Definition: ftsmooth.c:416
BOOL WINAPI StartServiceW(SC_HANDLE hService, DWORD dwNumServiceArgs, LPCWSTR *lpServiceArgVectors)
Definition: scm.c:2923
#define GetProcessHeap()
Definition: compat.h:395
PVOID WINAPI HeapAlloc(HANDLE, DWORD, SIZE_T)
#define SERVICE_ACTIVE
Definition: winsvc.h:50
unsigned long DWORD
Definition: ntddk_ex.h:95
#define StdErr
Definition: stream.h:77
INT ConResPuts(IN PCON_STREAM Stream, IN UINT uID)
Definition: outstream.c:610
static const WCHAR L[]
Definition: oid.c:1250
SC_HANDLE WINAPI OpenSCManagerW(LPCWSTR lpMachineName, LPCWSTR lpDatabaseName, DWORD dwDesiredAccess)
Definition: scm.c:2011
static INT EnumerateRunningServices(VOID)
Definition: cmdStart.c:15
#define SERVICE_START
Definition: winsvc.h:57
#define SERVICE_WIN32
Definition: cmtypes.h:962
INT ConPuts(IN PCON_STREAM Stream, IN LPWSTR szStr)
Definition: outstream.c:427
#define StdOut
Definition: stream.h:76
_In_ LPCSTR _Out_writes_to_opt_ cchDisplayName LPSTR lpDisplayName
Definition: winbase.h:2730
#define HeapFree(x, y, z)
Definition: compat.h:394
_Check_return_ _CRTIMP int __cdecl _wcsicmp(_In_z_ const wchar_t *_Str1, _In_z_ const wchar_t *_Str2)