ReactOS  0.4.15-dev-3163-gf17c2c0
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  BOOL ret;
27 
28  hManager = OpenSCManagerW(NULL,
31  if (hManager == NULL)
32  {
33  dwError = GetLastError();
34  nError = 1;
35  goto done;
36  }
37 
38  ret = EnumServicesStatusW(hManager,
41  NULL,
42  0,
43  &dwBufferSize,
44  &dwServiceCount,
45  &dwResumeHandle);
46  if (ret)
47  {
48  /* Nothing to enumerate ?! */
49  goto done;
50  }
51  dwError = GetLastError();
52  if (dwError != ERROR_INSUFFICIENT_BUFFER)
53  goto done;
54 
55  if (dwBufferSize != 0)
56  {
57  lpServiceBuffer = HeapAlloc(GetProcessHeap(), 0, dwBufferSize);
58  if (lpServiceBuffer != NULL)
59  {
60  if (EnumServicesStatusW(hManager,
63  lpServiceBuffer,
64  dwBufferSize,
65  &dwBufferSize,
66  &dwServiceCount,
67  &dwResumeHandle))
68  {
69  ConPuts(StdOut, L"The following services have been started:\n\n");
70 
71  for (i = 0; i < dwServiceCount; i++)
72  {
73  ConPrintf(StdOut, L" %s\n", lpServiceBuffer[i].lpDisplayName);
74  }
75  }
76 
77  HeapFree(GetProcessHeap(), 0, lpServiceBuffer);
78  }
79  }
80 
81 done:
82  if (hService != NULL)
83  CloseServiceHandle(hService);
84 
85  if (hManager != NULL)
86  CloseServiceHandle(hManager);
87 
88  if (dwError != ERROR_SUCCESS)
89  {
90  /* FIXME: Print proper error message */
91  ConPrintf(StdErr, L"Error: %lu\n", dwError);
92  }
93 
94  return nError;
95 }
96 
97 /* Start the service argv[2] */
98 static
99 INT
101 {
102  SC_HANDLE hManager = NULL;
103  SC_HANDLE hService = NULL;
104  LPCWSTR *lpArgVectors = NULL;
105  DWORD dwError = ERROR_SUCCESS;
106  INT nError = 0;
107  INT i;
108 
109  hManager = OpenSCManagerW(NULL,
112  if (hManager == NULL)
113  {
114  dwError = GetLastError();
115  nError = 1;
116  goto done;
117  }
118 
119  hService = OpenServiceW(hManager,
120  argv[2],
121  SERVICE_START);
122  if (hService == NULL)
123  {
124  dwError = GetLastError();
125  nError = 1;
126  goto done;
127  }
128 
129  lpArgVectors = HeapAlloc(GetProcessHeap(),
130  0,
131  (argc - 2) * sizeof(LPCWSTR));
132  if (lpArgVectors == NULL)
133  {
134  dwError = GetLastError();
135  nError = 1;
136  goto done;
137  }
138 
139  for (i = 2; i < argc; i++)
140  {
141  lpArgVectors[i - 2] = argv[i];
142  }
143 
144  if (!StartServiceW(hService,
145  (DWORD)argc - 2,
146  lpArgVectors))
147  {
148  dwError = GetLastError();
149  nError = 1;
150  }
151 
152 done:
153  if (lpArgVectors != NULL)
154  HeapFree(GetProcessHeap(), 0, (LPVOID)lpArgVectors);
155 
156  if (hService != NULL)
157  CloseServiceHandle(hService);
158 
159  if (hManager != NULL)
160  CloseServiceHandle(hManager);
161 
162  if (dwError != ERROR_SUCCESS)
163  {
164  /* FIXME: Print proper error message */
165  ConPrintf(StdErr, L"Error: %lu\n", dwError);
166  }
167 
168  return nError;
169 }
170 
171 INT
173 {
174  INT i;
175 
176  if (argc == 2)
177  {
178  return EnumerateRunningServices();
179  }
180 
181  for (i = 2; i < argc; i++)
182  {
183  if (_wcsicmp(argv[i], L"/help") == 0)
184  {
185  PrintMessageString(4381);
186  ConPuts(StdOut, L"\n");
187  PrintNetMessage(MSG_START_SYNTAX);
188  PrintNetMessage(MSG_START_HELP);
189  return 1;
190  }
191  }
192 
193  return StartOneService(argc, argv);
194 }
static int argc
Definition: ServiceArgs.c:12
#define SC_MANAGER_ENUMERATE_SERVICE
Definition: winsvc.h:16
#define ERROR_SUCCESS
Definition: deptool.c:10
const WCHAR * LPCWSTR
Definition: xmlstorage.h:185
#define SERVICES_ACTIVE_DATABASE
Definition: winsvc.h:564
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1040
INT cmdStart(INT argc, WCHAR **argv)
Definition: cmdStart.c:172
static INT StartOneService(INT argc, WCHAR **argv)
Definition: cmdStart.c:100
VOID PrintNetMessage(DWORD dwMessage)
Definition: main.c:225
#define argv
Definition: mplay32.c:18
int32_t INT
Definition: typedefs.h:58
void ConPuts(FILE *fp, LPCWSTR psz)
Definition: fc.c:16
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:1316
unsigned int BOOL
Definition: ntddk_ex.h:94
SC_HANDLE WINAPI OpenServiceW(SC_HANDLE hSCManager, LPCWSTR lpServiceName, DWORD dwDesiredAccess)
Definition: scm.c:2108
BOOL WINAPI CloseServiceHandle(SC_HANDLE hSCObject)
Definition: scm.c:580
BOOL WINAPI StartServiceW(SC_HANDLE hService, DWORD dwNumServiceArgs, LPCWSTR *lpServiceArgVectors)
Definition: scm.c:2928
#define GetProcessHeap()
Definition: compat.h:595
PVOID WINAPI HeapAlloc(HANDLE, DWORD, SIZE_T)
__wchar_t WCHAR
Definition: xmlstorage.h:180
#define SERVICE_ACTIVE
Definition: winsvc.h:50
unsigned long DWORD
Definition: ntddk_ex.h:95
int ret
static const WCHAR L[]
Definition: oid.c:1250
SC_HANDLE WINAPI OpenSCManagerW(LPCWSTR lpMachineName, LPCWSTR lpDatabaseName, DWORD dwDesiredAccess)
Definition: scm.c:2016
static INT EnumerateRunningServices(VOID)
Definition: cmdStart.c:15
#define SERVICE_START
Definition: winsvc.h:57
void ConPrintf(FILE *fp, LPCWSTR psz,...)
Definition: fc.c:20
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
VOID PrintMessageString(DWORD dwMessage)
Definition: main.c:106
#define SERVICE_WIN32
Definition: cmtypes.h:964
#define NULL
Definition: types.h:112
#define StdOut
Definition: fc.c:14
_In_ LPCSTR _Out_writes_to_opt_ cchDisplayName LPSTR lpDisplayName
Definition: winbase.h:2769
#define HeapFree(x, y, z)
Definition: compat.h:594
_Check_return_ _CRTIMP int __cdecl _wcsicmp(_In_z_ const wchar_t *_Str1, _In_z_ const wchar_t *_Str2)
#define StdErr
Definition: fc.c:15
#define ERROR_INSUFFICIENT_BUFFER
Definition: dderror.h:10