[NTDLL]
[reactos.git] / base / applications / mscutils / servman / create.c
1 /*
2 * PROJECT: ReactOS Services
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: base/applications/mscutils/servman/create.c
5 * PURPOSE: Create a new service
6 * COPYRIGHT: Copyright 2006-2007 Ged Murphy <gedmurphy@reactos.org>
7 *
8 */
9
10 #include "precomp.h"
11
12 typedef struct _CREATE_DATA
13 {
14 HWND hSelf;
15 LPTSTR ServiceName;
16 LPTSTR DisplayName;
17 LPTSTR BinPath;
18 LPTSTR Description;
19 LPTSTR Options;
20
21 } CREATE_DATA, *PCREATE_DATA;
22
23 static BOOL bHelpOpen = FALSE;
24
25 static BOOL
26 DoCreate(PCREATE_DATA Data)
27 {
28 SC_HANDLE hSCManager;
29 SC_HANDLE hSc;
30 BOOL bRet = FALSE;
31
32 /* open handle to the SCM */
33 hSCManager = OpenSCManager(NULL,
34 NULL,
35 SC_MANAGER_ALL_ACCESS);
36 if (hSCManager)
37 {
38 hSc = CreateService(hSCManager,
39 Data->ServiceName,
40 Data->DisplayName,
41 SERVICE_ALL_ACCESS,
42 SERVICE_WIN32_OWN_PROCESS,
43 SERVICE_DEMAND_START,
44 SERVICE_ERROR_NORMAL,
45 Data->BinPath,
46 NULL,
47 NULL,
48 NULL,
49 NULL,
50 NULL);
51
52 if (hSc)
53 {
54 LPTSTR lpSuccess;
55
56 /* Set the service description as CreateService
57 does not do this for us */
58 SetServiceDescription(Data->ServiceName,
59 Data->Description);
60
61 /* report success to user */
62 if (AllocAndLoadString(&lpSuccess,
63 hInstance,
64 IDS_CREATE_SUCCESS))
65 {
66 DisplayString(lpSuccess);
67
68 HeapFree(ProcessHeap,
69 0,
70 lpSuccess);
71 }
72
73 CloseServiceHandle(hSc);
74 bRet = TRUE;
75 }
76
77 CloseServiceHandle(hSCManager);
78 }
79
80 return bRet;
81 }
82
83
84 static LPTSTR
85 GetStringFromDialog(PCREATE_DATA Data,
86 UINT id)
87 {
88 HWND hwnd;
89 LPTSTR lpString = NULL;
90 INT iLen = 0;
91
92 hwnd = GetDlgItem(Data->hSelf,
93 id);
94 if (hwnd)
95 {
96 iLen = GetWindowTextLength(hwnd);
97 if (iLen)
98 {
99 lpString = (LPTSTR)HeapAlloc(ProcessHeap,
100 0,
101 (iLen + 1) * sizeof(TCHAR));
102 if (lpString)
103 {
104 GetWindowText(hwnd,
105 lpString,
106 iLen + 1);
107 }
108 }
109 }
110
111 return lpString;
112 }
113
114
115 static BOOL
116 GetDataFromDialog(PCREATE_DATA Data)
117 {
118 BOOL bRet = FALSE;
119
120 if ((Data->ServiceName = GetStringFromDialog(Data, IDC_CREATE_SERVNAME)))
121 {
122 if ((Data->DisplayName = GetStringFromDialog(Data, IDC_CREATE_DISPNAME)))
123 {
124 if ((Data->BinPath = GetStringFromDialog(Data, IDC_CREATE_PATH)))
125 {
126 Data->Description = GetStringFromDialog(Data, IDC_CREATE_DESC);
127 Data->Options = GetStringFromDialog(Data, IDC_CREATE_OPTIONS);
128
129 bRet = TRUE;
130 }
131 }
132 }
133
134 return bRet;
135 }
136
137 static VOID
138 FreeMemory(PCREATE_DATA Data)
139 {
140 if (Data->ServiceName != NULL)
141 HeapFree(ProcessHeap,
142 0,
143 Data->ServiceName);
144 if (Data->DisplayName != NULL)
145 HeapFree(ProcessHeap,
146 0,
147 Data->DisplayName);
148 if (Data->BinPath != NULL)
149 HeapFree(ProcessHeap,
150 0,
151 Data->BinPath);
152 if (Data->Description != NULL)
153 HeapFree(ProcessHeap,
154 0,
155 Data->Description);
156 if (Data->Options != NULL)
157 HeapFree(ProcessHeap,
158 0,
159 Data->Options);
160
161 HeapFree(ProcessHeap,
162 0,
163 Data);
164 }
165
166
167 INT_PTR CALLBACK
168 CreateHelpDialogProc(HWND hDlg,
169 UINT message,
170 WPARAM wParam,
171 LPARAM lParam)
172 {
173 HWND hHelp;
174 HICON hIcon = NULL;
175 TCHAR Buf[1000];
176
177 switch (message)
178 {
179 case WM_INITDIALOG:
180 {
181 hIcon = (HICON) LoadImage(hInstance,
182 MAKEINTRESOURCE(IDI_SM_ICON),
183 IMAGE_ICON,
184 16,
185 16,
186 0);
187
188 SendMessage(hDlg,
189 WM_SETICON,
190 ICON_SMALL,
191 (LPARAM)hIcon);
192
193 hHelp = GetDlgItem(hDlg,
194 IDC_CREATE_HELP);
195
196 LoadString(hInstance,
197 IDS_HELP_OPTIONS,
198 Buf,
199 sizeof(Buf) / sizeof(TCHAR));
200
201 SetWindowText(hHelp,
202 Buf);
203
204 return TRUE;
205 }
206
207 case WM_COMMAND:
208 {
209 if ((LOWORD(wParam) == IDOK) || (LOWORD(wParam) == IDCANCEL))
210 {
211 bHelpOpen = FALSE;
212 DestroyIcon(hIcon);
213 DestroyWindow(hDlg);
214 return TRUE;
215 }
216 break;
217 }
218 }
219
220 return FALSE;
221 }
222
223
224 INT_PTR CALLBACK
225 CreateDialogProc(HWND hDlg,
226 UINT message,
227 WPARAM wParam,
228 LPARAM lParam)
229 {
230 HICON hIcon = NULL;
231
232 switch (message)
233 {
234 case WM_INITDIALOG:
235 {
236 hIcon = (HICON)LoadImage(hInstance,
237 MAKEINTRESOURCE(IDI_SM_ICON),
238 IMAGE_ICON,
239 16,
240 16,
241 0);
242 if (hIcon)
243 {
244 SendMessage(hDlg,
245 WM_SETICON,
246 ICON_SMALL,
247 (LPARAM)hIcon);
248 DestroyIcon(hIcon);
249 }
250
251 return TRUE;
252 }
253
254 case WM_COMMAND:
255 {
256 switch (LOWORD(wParam))
257 {
258 case IDOK:
259 {
260 PCREATE_DATA Data;
261
262 Data = (PCREATE_DATA)HeapAlloc(ProcessHeap,
263 HEAP_ZERO_MEMORY,
264 sizeof(CREATE_DATA));
265 if (Data)
266 {
267 Data->hSelf = hDlg;
268
269 if (GetDataFromDialog(Data))
270 {
271 DoCreate(Data);
272 }
273 else
274 {
275 /* Something went wrong, leave the dialog
276 * open so they can try again */
277 FreeMemory(Data);
278 break;
279 }
280
281 FreeMemory(Data);
282 }
283
284 EndDialog(hDlg,
285 LOWORD(wParam));
286 return TRUE;
287 }
288
289 case IDCANCEL:
290 {
291 EndDialog(hDlg,
292 LOWORD(wParam));
293 return TRUE;
294 }
295
296 case ID_CREATE_HELP:
297 {
298 HWND hHelp;
299
300 if (! bHelpOpen)
301 {
302 hHelp = CreateDialog(hInstance,
303 MAKEINTRESOURCE(IDD_DLG_HELP_OPTIONS),
304 hDlg,
305 CreateHelpDialogProc);
306 if(hHelp != NULL)
307 {
308 bHelpOpen = TRUE;
309 }
310 }
311 }
312 break;
313 }
314 }
315 }
316
317 return FALSE;
318 }