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>
22 LPCTSTR lpServiceName
;
23 LPCTSTR lpDisplayName
;
27 LPCTSTR lpBinaryPathName
;
28 LPCTSTR lpLoadOrderGroup
;
30 LPCTSTR lpDependencies
;
31 LPCTSTR lpServiceStartName
;
35 } SERVICE_CREATE_INFO
, *LPSERVICE_CREATE_INFO
;
38 static const OPTION_INFO TypeOpts
[] =
40 { _T("own"), SERVICE_WIN32_OWN_PROCESS
},
41 { _T("share"), SERVICE_WIN32_SHARE_PROCESS
},
42 { _T("interact"), SERVICE_INTERACTIVE_PROCESS
},
43 { _T("kernel"), SERVICE_KERNEL_DRIVER
},
44 { _T("filesys"), SERVICE_FILE_SYSTEM_DRIVER
},
45 { _T("rec"), SERVICE_RECOGNIZER_DRIVER
}
48 static const OPTION_INFO StartOpts
[] =
50 { _T("boot"), SERVICE_BOOT_START
},
51 { _T("system"), SERVICE_SYSTEM_START
},
52 { _T("auto"), SERVICE_AUTO_START
},
53 { _T("demand"), SERVICE_DEMAND_START
},
54 { _T("disabled"), SERVICE_DISABLED
}
57 static const OPTION_INFO ErrorOpts
[] =
59 { _T("normal"), SERVICE_ERROR_NORMAL
},
60 { _T("severe"), SERVICE_ERROR_SEVERE
},
61 { _T("critical"), SERVICE_ERROR_CRITICAL
},
62 { _T("ignore"), SERVICE_ERROR_IGNORE
}
65 static const OPTION_INFO TagOpts
[] =
72 static BOOL
ParseCreateArguments(
75 OUT LPSERVICE_CREATE_INFO lpServiceInfo
83 ZeroMemory(lpServiceInfo
, sizeof(SERVICE_CREATE_INFO
));
85 lpServiceInfo
->lpServiceName
= ServiceArgs
[0];
91 if (!lstrcmpi(ServiceArgs
[ArgIndex
], _T("type=")))
93 for (i
= 0; i
< sizeof(TypeOpts
) / sizeof(TypeOpts
[0]); i
++)
94 if (!lstrcmpi(ServiceArgs
[ArgIndex
+ 1], TypeOpts
[i
].lpOption
))
96 lpServiceInfo
->dwServiceType
|= TypeOpts
[i
].dwValue
;
100 if (i
== sizeof(TypeOpts
) / sizeof(TypeOpts
[0]))
103 else if (!lstrcmpi(ServiceArgs
[ArgIndex
], _T("start=")))
105 for (i
= 0; i
< sizeof(StartOpts
) / sizeof(StartOpts
[0]); i
++)
106 if (!lstrcmpi(ServiceArgs
[ArgIndex
+ 1], StartOpts
[i
].lpOption
))
108 lpServiceInfo
->dwStartType
= StartOpts
[i
].dwValue
;
112 if (i
== sizeof(StartOpts
) / sizeof(StartOpts
[0]))
115 else if (!lstrcmpi(ServiceArgs
[ArgIndex
], _T("error=")))
117 for (i
= 0; i
< sizeof(ErrorOpts
) / sizeof(ErrorOpts
[0]); i
++)
118 if (!lstrcmpi(ServiceArgs
[ArgIndex
+ 1], ErrorOpts
[i
].lpOption
))
120 lpServiceInfo
->dwErrorControl
= ErrorOpts
[i
].dwValue
;
124 if (i
== sizeof(ErrorOpts
) / sizeof(ErrorOpts
[0]))
127 else if (!lstrcmpi(ServiceArgs
[ArgIndex
], _T("tag=")))
129 for (i
= 0; i
< sizeof(TagOpts
) / sizeof(TagOpts
[0]); i
++)
130 if (!lstrcmpi(ServiceArgs
[ArgIndex
+ 1], TagOpts
[i
].lpOption
))
132 lpServiceInfo
->bTagId
= TagOpts
[i
].dwValue
;
136 if (i
== sizeof(TagOpts
) / sizeof(TagOpts
[0]))
139 else if (!lstrcmpi(ServiceArgs
[ArgIndex
], _T("binpath=")))
141 lpServiceInfo
->lpBinaryPathName
= ServiceArgs
[ArgIndex
+ 1];
143 else if (!lstrcmpi(ServiceArgs
[ArgIndex
], _T("group=")))
145 lpServiceInfo
->lpLoadOrderGroup
= ServiceArgs
[ArgIndex
+ 1];
147 else if (!lstrcmpi(ServiceArgs
[ArgIndex
], _T("depend=")))
149 lpServiceInfo
->lpDependencies
= ServiceArgs
[ArgIndex
+ 1];
151 else if (!lstrcmpi(ServiceArgs
[ArgIndex
], _T("obj=")))
153 lpServiceInfo
->lpServiceStartName
= ServiceArgs
[ArgIndex
+ 1];
155 else if (!lstrcmpi(ServiceArgs
[ArgIndex
], _T("displayname=")))
157 lpServiceInfo
->lpDisplayName
= ServiceArgs
[ArgIndex
+ 1];
159 else if (!lstrcmpi(ServiceArgs
[ArgIndex
], _T("password=")))
161 lpServiceInfo
->lpPassword
= ServiceArgs
[ArgIndex
+ 1];
168 return (ArgCount
== 0);
171 BOOL
Create(LPCTSTR
*ServiceArgs
, INT ArgCount
)
173 SC_HANDLE hSCManager
;
179 LPTSTR lpBuffer
= NULL
;
180 SERVICE_CREATE_INFO ServiceInfo
;
182 if (!ParseCreateArguments(ServiceArgs
, ArgCount
, &ServiceInfo
))
188 if (!ServiceInfo
.dwServiceType
)
189 ServiceInfo
.dwServiceType
= SERVICE_WIN32_OWN_PROCESS
;
191 if (!ServiceInfo
.dwStartType
)
192 ServiceInfo
.dwStartType
= SERVICE_DEMAND_START
;
194 if (!ServiceInfo
.dwErrorControl
)
195 ServiceInfo
.dwErrorControl
= SERVICE_ERROR_NORMAL
;
197 if (ServiceInfo
.lpDependencies
)
199 Length
= lstrlen(ServiceInfo
.lpDependencies
);
201 lpBuffer
= HeapAlloc(GetProcessHeap(),
203 (Length
+ 2) * sizeof(TCHAR
));
205 for (i
= 0; i
< Length
; i
++)
206 if (ServiceInfo
.lpDependencies
[i
] == _T('/'))
209 lpBuffer
[i
] = ServiceInfo
.lpDependencies
[i
];
211 lpBuffer
[Length
] = 0;
212 lpBuffer
[Length
+ 1] = 0;
214 ServiceInfo
.lpDependencies
= lpBuffer
;
218 _tprintf(_T("service name - %s\n"), ServiceInfo
.lpServiceName
);
219 _tprintf(_T("display name - %s\n"), ServiceInfo
.lpDisplayName
);
220 _tprintf(_T("service type - %lu\n"), ServiceInfo
.dwServiceType
);
221 _tprintf(_T("start type - %lu\n"), ServiceInfo
.dwStartType
);
222 _tprintf(_T("error control - %lu\n"), ServiceInfo
.dwErrorControl
);
223 _tprintf(_T("Binary path - %s\n"), ServiceInfo
.lpBinaryPathName
);
224 _tprintf(_T("load order group - %s\n"), ServiceInfo
.lpLoadOrderGroup
);
225 _tprintf(_T("tag - %lu\n"), ServiceInfo
.dwTagId
);
226 _tprintf(_T("dependencies - %s\n"), ServiceInfo
.lpDependencies
);
227 _tprintf(_T("account start name - %s\n"), ServiceInfo
.lpServiceStartName
);
228 _tprintf(_T("account password - %s\n"), ServiceInfo
.lpPassword
);
231 hSCManager
= OpenSCManager(NULL
, NULL
, SC_MANAGER_CREATE_SERVICE
);
233 if (hSCManager
!= NULL
)
235 hSc
= CreateService(hSCManager
,
236 ServiceInfo
.lpServiceName
,
237 ServiceInfo
.lpDisplayName
,
239 ServiceInfo
.dwServiceType
,
240 ServiceInfo
.dwStartType
,
241 ServiceInfo
.dwErrorControl
,
242 ServiceInfo
.lpBinaryPathName
,
243 ServiceInfo
.lpLoadOrderGroup
,
244 ServiceInfo
.bTagId
? &ServiceInfo
.dwTagId
: NULL
,
245 ServiceInfo
.lpDependencies
,
246 ServiceInfo
.lpServiceStartName
,
247 ServiceInfo
.lpPassword
);
251 _tprintf(_T("[SC] CreateService SUCCESS\n"));
253 CloseServiceHandle(hSc
);
259 CloseServiceHandle(hSCManager
);
264 if (lpBuffer
!= NULL
)
265 HeapFree(GetProcessHeap(), 0, lpBuffer
);