xref: /reactos/base/applications/sc/create.c (revision 40462c92)
1 /*
2  * PROJECT:     ReactOS Services
3  * LICENSE:     GPL - See COPYING in the top level directory
4  * FILE:        base/applications/sc/create.c
5  * PURPOSE:     Create a service
6  * COPYRIGHT:   Copyright 2005 - 2006 Ged Murphy <gedmurphy@gmail.com>
7  *              Roel Messiant <roelmessiant@gmail.com>
8  *
9  */
10 
11 #include "sc.h"
12 
13 BOOL Create(LPCTSTR *ServiceArgs, INT ArgCount)
14 {
15     SC_HANDLE hSCManager = NULL;
16     SC_HANDLE hService = NULL;
17     BOOL bRet = TRUE;
18 
19     INT i;
20     INT Length;
21     LPTSTR lpBuffer = NULL;
22     SERVICE_CREATE_INFO ServiceInfo;
23 
24     if (!ParseCreateConfigArguments(ServiceArgs, ArgCount, FALSE, &ServiceInfo))
25     {
26         CreateUsage();
27         return FALSE;
28     }
29 
30     if (!ServiceInfo.dwServiceType)
31         ServiceInfo.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
32 
33     if (!ServiceInfo.dwStartType)
34         ServiceInfo.dwStartType = SERVICE_DEMAND_START;
35 
36     if (!ServiceInfo.dwErrorControl)
37         ServiceInfo.dwErrorControl = SERVICE_ERROR_NORMAL;
38 
39     if (ServiceInfo.lpDependencies)
40     {
41         Length = lstrlen(ServiceInfo.lpDependencies);
42 
43         lpBuffer = HeapAlloc(GetProcessHeap(),
44                              0,
45                             (Length + 2) * sizeof(TCHAR));
46 
47         for (i = 0; i < Length; i++)
48             if (ServiceInfo.lpDependencies[i] == _T('/'))
49                 lpBuffer[i] = 0;
50             else
51                 lpBuffer[i] = ServiceInfo.lpDependencies[i];
52 
53         lpBuffer[Length] = 0;
54         lpBuffer[Length + 1] = 0;
55 
56         ServiceInfo.lpDependencies = lpBuffer;
57     }
58 
59 #ifdef SCDBG
60     _tprintf(_T("service name - %s\n"), ServiceInfo.lpServiceName);
61     _tprintf(_T("display name - %s\n"), ServiceInfo.lpDisplayName);
62     _tprintf(_T("service type - %lu\n"), ServiceInfo.dwServiceType);
63     _tprintf(_T("start type - %lu\n"), ServiceInfo.dwStartType);
64     _tprintf(_T("error control - %lu\n"), ServiceInfo.dwErrorControl);
65     _tprintf(_T("Binary path - %s\n"), ServiceInfo.lpBinaryPathName);
66     _tprintf(_T("load order group - %s\n"), ServiceInfo.lpLoadOrderGroup);
67     _tprintf(_T("tag - %lu\n"), ServiceInfo.dwTagId);
68     _tprintf(_T("dependencies - %s\n"), ServiceInfo.lpDependencies);
69     _tprintf(_T("account start name - %s\n"), ServiceInfo.lpServiceStartName);
70     _tprintf(_T("account password - %s\n"), ServiceInfo.lpPassword);
71 #endif
72 
73     hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CREATE_SERVICE);
74     if (hSCManager == NULL)
75     {
76         _tprintf(_T("[SC] OpenSCManager FAILED %lu:\n\n"), GetLastError());
77         bRet = FALSE;
78         goto done;
79     }
80 
81     hService = CreateService(hSCManager,
82                              ServiceInfo.lpServiceName,
83                              ServiceInfo.lpDisplayName,
84                              SERVICE_ALL_ACCESS,
85                              ServiceInfo.dwServiceType,
86                              ServiceInfo.dwStartType,
87                              ServiceInfo.dwErrorControl,
88                              ServiceInfo.lpBinaryPathName,
89                              ServiceInfo.lpLoadOrderGroup,
90                              ServiceInfo.bTagId ? &ServiceInfo.dwTagId : NULL,
91                              ServiceInfo.lpDependencies,
92                              ServiceInfo.lpServiceStartName,
93                              ServiceInfo.lpPassword);
94     if (hService == NULL)
95     {
96         _tprintf(_T("[SC] CreateService FAILED %lu:\n\n"), GetLastError());
97         bRet = FALSE;
98         goto done;
99     }
100 
101     _tprintf(_T("[SC] CreateService SUCCESS\n\n"));
102 
103 done:
104     if (bRet == FALSE)
105         ReportLastError();
106 
107     if (hService)
108         CloseServiceHandle(hService);
109 
110     if (hSCManager)
111         CloseServiceHandle(hSCManager);
112 
113     if (lpBuffer != NULL)
114         HeapFree(GetProcessHeap(), 0, lpBuffer);
115 
116     return bRet;
117 }
118