2 * PROJECT: ReactOS Services
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: base/system/sc/create.c
5 * PURPOSE: Create a service
6 * COPYRIGHT: Copyright 2005 - 2006 Ged Murphy <gedmurphy@gmail.com>
7 * Roel Messiant <roelmessiant@gmail.com>
21 LPCTSTR lpServiceName
;
22 LPCTSTR lpDisplayName
;
26 LPCTSTR lpBinaryPathName
;
27 LPCTSTR lpLoadOrderGroup
;
29 LPCTSTR lpDependencies
;
30 LPCTSTR lpServiceStartName
;
34 } SERVICE_CREATE_INFO
, *LPSERVICE_CREATE_INFO
;
37 static const OPTION_INFO TypeOpts
[] =
39 { _T("own"), SERVICE_WIN32_OWN_PROCESS
},
40 { _T("share"), SERVICE_WIN32_SHARE_PROCESS
},
41 { _T("interact"), SERVICE_INTERACTIVE_PROCESS
},
42 { _T("kernel"), SERVICE_KERNEL_DRIVER
},
43 { _T("filesys"), SERVICE_FILE_SYSTEM_DRIVER
},
44 { _T("rec"), SERVICE_RECOGNIZER_DRIVER
}
47 static const OPTION_INFO StartOpts
[] =
49 { _T("boot"), SERVICE_BOOT_START
},
50 { _T("system"), SERVICE_SYSTEM_START
},
51 { _T("auto"), SERVICE_AUTO_START
},
52 { _T("demand"), SERVICE_DEMAND_START
},
53 { _T("disabled"), SERVICE_DISABLED
}
56 static const OPTION_INFO ErrorOpts
[] =
58 { _T("normal"), SERVICE_ERROR_NORMAL
},
59 { _T("severe"), SERVICE_ERROR_SEVERE
},
60 { _T("critical"), SERVICE_ERROR_CRITICAL
},
61 { _T("ignore"), SERVICE_ERROR_IGNORE
}
64 static const OPTION_INFO TagOpts
[] =
71 static BOOL
ParseCreateArguments(
74 OUT LPSERVICE_CREATE_INFO lpServiceInfo
82 ZeroMemory(lpServiceInfo
, sizeof(SERVICE_CREATE_INFO
));
84 lpServiceInfo
->lpServiceName
= ServiceArgs
[0];
90 if (!lstrcmpi(ServiceArgs
[ArgIndex
], _T("type=")))
92 for (i
= 0; i
< sizeof(TypeOpts
) / sizeof(TypeOpts
[0]); i
++)
93 if (!lstrcmpi(ServiceArgs
[ArgIndex
+ 1], TypeOpts
[i
].lpOption
))
95 lpServiceInfo
->dwServiceType
|= TypeOpts
[i
].dwValue
;
99 if (i
== sizeof(TypeOpts
) / sizeof(TypeOpts
[0]))
102 else if (!lstrcmpi(ServiceArgs
[ArgIndex
], _T("start=")))
104 for (i
= 0; i
< sizeof(StartOpts
) / sizeof(StartOpts
[0]); i
++)
105 if (!lstrcmpi(ServiceArgs
[ArgIndex
+ 1], StartOpts
[i
].lpOption
))
107 lpServiceInfo
->dwStartType
= StartOpts
[i
].dwValue
;
111 if (i
== sizeof(StartOpts
) / sizeof(StartOpts
[0]))
114 else if (!lstrcmpi(ServiceArgs
[ArgIndex
], _T("error=")))
116 for (i
= 0; i
< sizeof(ErrorOpts
) / sizeof(ErrorOpts
[0]); i
++)
117 if (!lstrcmpi(ServiceArgs
[ArgIndex
+ 1], ErrorOpts
[i
].lpOption
))
119 lpServiceInfo
->dwErrorControl
= ErrorOpts
[i
].dwValue
;
123 if (i
== sizeof(ErrorOpts
) / sizeof(ErrorOpts
[0]))
126 else if (!lstrcmpi(ServiceArgs
[ArgIndex
], _T("tag=")))
128 for (i
= 0; i
< sizeof(TagOpts
) / sizeof(TagOpts
[0]); i
++)
129 if (!lstrcmpi(ServiceArgs
[ArgIndex
+ 1], TagOpts
[i
].lpOption
))
131 lpServiceInfo
->bTagId
= TagOpts
[i
].dwValue
;
135 if (i
== sizeof(TagOpts
) / sizeof(TagOpts
[0]))
138 else if (!lstrcmpi(ServiceArgs
[ArgIndex
], _T("binpath=")))
140 lpServiceInfo
->lpBinaryPathName
= ServiceArgs
[ArgIndex
+ 1];
142 else if (!lstrcmpi(ServiceArgs
[ArgIndex
], _T("group=")))
144 lpServiceInfo
->lpLoadOrderGroup
= ServiceArgs
[ArgIndex
+ 1];
146 else if (!lstrcmpi(ServiceArgs
[ArgIndex
], _T("depend=")))
148 lpServiceInfo
->lpDependencies
= ServiceArgs
[ArgIndex
+ 1];
150 else if (!lstrcmpi(ServiceArgs
[ArgIndex
], _T("obj=")))
152 lpServiceInfo
->lpServiceStartName
= ServiceArgs
[ArgIndex
+ 1];
154 else if (!lstrcmpi(ServiceArgs
[ArgIndex
], _T("displayname=")))
156 lpServiceInfo
->lpDisplayName
= ServiceArgs
[ArgIndex
+ 1];
158 else if (!lstrcmpi(ServiceArgs
[ArgIndex
], _T("password=")))
160 lpServiceInfo
->lpPassword
= ServiceArgs
[ArgIndex
+ 1];
167 return (ArgCount
== 0);
170 BOOL
Create(LPCTSTR
*ServiceArgs
, INT ArgCount
)
172 SC_HANDLE hSCManager
;
178 LPTSTR lpBuffer
= NULL
;
179 SERVICE_CREATE_INFO ServiceInfo
;
181 if (!ParseCreateArguments(ServiceArgs
, ArgCount
, &ServiceInfo
))
187 if (!ServiceInfo
.dwServiceType
)
188 ServiceInfo
.dwServiceType
= SERVICE_WIN32_OWN_PROCESS
;
190 if (!ServiceInfo
.dwStartType
)
191 ServiceInfo
.dwStartType
= SERVICE_DEMAND_START
;
193 if (!ServiceInfo
.dwErrorControl
)
194 ServiceInfo
.dwErrorControl
= SERVICE_ERROR_NORMAL
;
196 if (ServiceInfo
.lpDependencies
)
198 Length
= lstrlen(ServiceInfo
.lpDependencies
);
200 lpBuffer
= HeapAlloc(GetProcessHeap(),
202 (Length
+ 2) * sizeof(TCHAR
));
204 for (i
= 0; i
< Length
; i
++)
205 if (ServiceInfo
.lpDependencies
[i
] == _T('/'))
208 lpBuffer
[i
] = ServiceInfo
.lpDependencies
[i
];
210 lpBuffer
[Length
] = 0;
211 lpBuffer
[Length
+ 1] = 0;
213 ServiceInfo
.lpDependencies
= lpBuffer
;
217 _tprintf(_T("service name - %s\n"), ServiceInfo
.lpServiceName
);
218 _tprintf(_T("display name - %s\n"), ServiceInfo
.lpDisplayName
);
219 _tprintf(_T("service type - %lu\n"), ServiceInfo
.dwServiceType
);
220 _tprintf(_T("start type - %lu\n"), ServiceInfo
.dwStartType
);
221 _tprintf(_T("error control - %lu\n"), ServiceInfo
.dwErrorControl
);
222 _tprintf(_T("Binary path - %s\n"), ServiceInfo
.lpBinaryPathName
);
223 _tprintf(_T("load order group - %s\n"), ServiceInfo
.lpLoadOrderGroup
);
224 _tprintf(_T("tag - %lu\n"), ServiceInfo
.dwTagId
);
225 _tprintf(_T("dependencies - %s\n"), ServiceInfo
.lpDependencies
);
226 _tprintf(_T("account start name - %s\n"), ServiceInfo
.lpServiceStartName
);
227 _tprintf(_T("account password - %s\n"), ServiceInfo
.lpPassword
);
230 hSCManager
= OpenSCManager(NULL
, NULL
, SC_MANAGER_CREATE_SERVICE
);
232 if (hSCManager
!= NULL
)
234 hSc
= CreateService(hSCManager
,
235 ServiceInfo
.lpServiceName
,
236 ServiceInfo
.lpDisplayName
,
238 ServiceInfo
.dwServiceType
,
239 ServiceInfo
.dwStartType
,
240 ServiceInfo
.dwErrorControl
,
241 ServiceInfo
.lpBinaryPathName
,
242 ServiceInfo
.lpLoadOrderGroup
,
243 ServiceInfo
.bTagId
? &ServiceInfo
.dwTagId
: NULL
,
244 ServiceInfo
.lpDependencies
,
245 ServiceInfo
.lpServiceStartName
,
246 ServiceInfo
.lpPassword
);
250 _tprintf(_T("[SC] CreateService SUCCESS\n"));
252 CloseServiceHandle(hSc
);
258 CloseServiceHandle(hSCManager
);
263 if (lpBuffer
!= NULL
)
264 HeapFree(GetProcessHeap(), 0, lpBuffer
);