ReactOS  0.4.12-dev-102-g4b7f1e0
create.c
Go to the documentation of this file.
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 {
20 
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 = OpenSCManagerW(NULL,
34  NULL,
36  if (hSCManager)
37  {
38  hSc = CreateServiceW(hSCManager,
39  Data->ServiceName,
40  Data->DisplayName,
45  Data->BinPath,
46  NULL,
47  NULL,
48  NULL,
49  NULL,
50  NULL);
51 
52  if (hSc)
53  {
54  LPWSTR lpSuccess;
55 
56  /* Set the service description as CreateService
57  does not do this for us */
59  Data->Description);
60 
61  /* report success to user */
62  if (AllocAndLoadString(&lpSuccess,
63  hInstance,
65  {
66  DisplayString(lpSuccess);
67 
68  LocalFree(lpSuccess);
69  }
70 
71  CloseServiceHandle(hSc);
72  bRet = TRUE;
73  }
74 
75  CloseServiceHandle(hSCManager);
76  }
77 
78  return bRet;
79 }
80 
81 static LPWSTR
83  UINT id)
84 {
85  HWND hwnd;
86  LPWSTR lpString = NULL;
87  INT iLen = 0;
88 
89  hwnd = GetDlgItem(Data->hSelf,
90  id);
91  if (hwnd)
92  {
93  iLen = GetWindowTextLengthW(hwnd);
94  if (iLen)
95  {
96  lpString = (LPWSTR)HeapAlloc(ProcessHeap,
97  0,
98  (iLen + 1) * sizeof(WCHAR));
99  if (lpString)
100  {
101  GetWindowTextW(hwnd,
102  lpString,
103  iLen + 1);
104  }
105  }
106  }
107 
108  return lpString;
109 }
110 
111 static BOOL
112 GetDataFromDialog(PCREATE_DATA Data)
113 {
114  BOOL bRet = FALSE;
115 
117  {
119  {
120  if ((Data->BinPath = GetStringFromDialog(Data, IDC_CREATE_PATH)))
121  {
124 
125  bRet = TRUE;
126  }
127  }
128  }
129 
130  return bRet;
131 }
132 
133 static VOID
134 FreeMemory(PCREATE_DATA Data)
135 {
136  if (Data->ServiceName != NULL)
138  0,
139  Data->ServiceName);
140  if (Data->DisplayName != NULL)
142  0,
143  Data->DisplayName);
144  if (Data->BinPath != NULL)
146  0,
147  Data->BinPath);
148  if (Data->Description != NULL)
150  0,
151  Data->Description);
152  if (Data->Options != NULL)
154  0,
155  Data->Options);
156 
158  0,
159  Data);
160 }
161 
164  UINT message,
165  WPARAM wParam,
166  LPARAM lParam)
167 {
168  HWND hHelp;
169  HICON hIcon = NULL;
170  WCHAR Buf[1000];
171 
172  switch (message)
173  {
174  case WM_INITDIALOG:
175  {
176  hIcon = (HICON) LoadImageW(hInstance,
178  IMAGE_ICON,
179  16,
180  16,
181  0);
182 
183  SendMessageW(hDlg,
184  WM_SETICON,
185  ICON_SMALL,
186  (LPARAM)hIcon);
187 
188  hHelp = GetDlgItem(hDlg,
190 
193  Buf,
194  sizeof(Buf) / sizeof(WCHAR));
195 
196  SetWindowTextW(hHelp, Buf);
197 
198  return TRUE;
199  }
200 
201  case WM_COMMAND:
202  {
203  if ((LOWORD(wParam) == IDOK) || (LOWORD(wParam) == IDCANCEL))
204  {
205  bHelpOpen = FALSE;
206  DestroyIcon(hIcon);
207  DestroyWindow(hDlg);
208  return TRUE;
209  }
210  break;
211  }
212  }
213 
214  return FALSE;
215 }
216 
219  UINT message,
220  WPARAM wParam,
221  LPARAM lParam)
222 {
223  HICON hIcon = NULL;
224 
225  switch (message)
226  {
227  case WM_INITDIALOG:
228  {
229  hIcon = (HICON)LoadImage(hInstance,
231  IMAGE_ICON,
232  16,
233  16,
234  0);
235  if (hIcon)
236  {
237  SendMessage(hDlg,
238  WM_SETICON,
239  ICON_SMALL,
240  (LPARAM)hIcon);
241  DestroyIcon(hIcon);
242  }
243 
244  return TRUE;
245  }
246 
247  case WM_COMMAND:
248  {
249  switch (LOWORD(wParam))
250  {
251  case IDOK:
252  {
253  PCREATE_DATA Data;
254 
257  sizeof(CREATE_DATA));
258  if (Data)
259  {
260  Data->hSelf = hDlg;
261 
262  if (GetDataFromDialog(Data))
263  {
264  DoCreate(Data);
265  }
266  else
267  {
268  /* Something went wrong, leave the dialog
269  * open so they can try again */
270  FreeMemory(Data);
271  break;
272  }
273 
274  FreeMemory(Data);
275  }
276 
277  EndDialog(hDlg,
278  LOWORD(wParam));
279  return TRUE;
280  }
281 
282  case IDCANCEL:
283  {
284  EndDialog(hDlg,
285  LOWORD(wParam));
286  return TRUE;
287  }
288 
289  case ID_CREATE_HELP:
290  {
291  HWND hHelp;
292 
293  if (! bHelpOpen)
294  {
295  hHelp = CreateDialog(hInstance,
297  hDlg,
299  if(hHelp != NULL)
300  {
301  bHelpOpen = TRUE;
302  }
303  }
304  }
305  break;
306  }
307  }
308  }
309 
310  return FALSE;
311 }
static LPWSTR GetStringFromDialog(PCREATE_DATA Data, UINT id)
Definition: create.c:82
const DOCKBAR PVOID HWND HWND * hwnd
Definition: tooldock.h:22
#define MAKEINTRESOURCE
Definition: winuser.h:591
static VOID FreeMemory(PCREATE_DATA Data)
Definition: create.c:134
Definition: tftpd.h:59
static HICON
Definition: imagelist.c:84
#define SERVICE_ERROR_NORMAL
Definition: cmtypes.h:980
#define TRUE
Definition: types.h:120
#define IDOK
Definition: winuser.h:824
#define IMAGE_ICON
Definition: winuser.h:212
struct _CREATE_DATA * PCREATE_DATA
SC_HANDLE WINAPI CreateServiceW(SC_HANDLE hSCManager, LPCWSTR lpServiceName, LPCWSTR lpDisplayName, DWORD dwDesiredAccess, DWORD dwServiceType, DWORD dwStartType, DWORD dwErrorControl, LPCWSTR lpBinaryPathName, LPCWSTR lpLoadOrderGroup, LPDWORD lpdwTagId, LPCWSTR lpDependencies, LPCWSTR lpServiceStartName, LPCWSTR lpPassword)
Definition: scm.c:807
BOOL WINAPI DestroyIcon(_In_ HICON)
Definition: cursoricon.c:2022
__wchar_t WCHAR
Definition: xmlstorage.h:180
#define IDC_CREATE_DISPNAME
Definition: resource.h:185
#define IDC_CREATE_DESC
Definition: resource.h:187
#define CALLBACK
Definition: compat.h:27
LPWSTR Description
Definition: create.c:18
#define IDC_CREATE_SERVNAME
Definition: resource.h:184
BOOL SetServiceDescription(LPWSTR lpServiceName, LPWSTR lpDescription)
Definition: query.c:210
BOOL WINAPI SetWindowTextW(_In_ HWND, _In_opt_ LPCWSTR)
#define SERVICE_ALL_ACCESS
Definition: winsvc.h:62
int32_t INT_PTR
Definition: typedefs.h:62
#define ID_CREATE_HELP
Definition: resource.h:189
static INT AllocAndLoadString(OUT LPTSTR *lpTarget, IN HINSTANCE hInst, IN UINT uID)
Definition: misc.c:60
#define WCHAR
Definition: msvc.h:43
BOOL WINAPI DestroyWindow(_In_ HWND)
#define IDD_DLG_HELP_OPTIONS
Definition: resource.h:190
int WINAPI LoadStringW(_In_opt_ HINSTANCE hInstance, _In_ UINT uID, _Out_writes_to_(cchBufferMax, return+1) LPWSTR lpBuffer, _In_ int cchBufferMax)
int32_t INT
Definition: typedefs.h:56
BOOL WINAPI EndDialog(_In_ HWND, _In_ INT_PTR)
WPARAM wParam
Definition: combotst.c:138
LPWSTR Options
Definition: create.c:19
UINT_PTR WPARAM
Definition: windef.h:207
#define ICON_SMALL
Definition: tnclass.cpp:48
HINSTANCE hInstance
Definition: charmap.c:20
#define IDS_CREATE_SUCCESS
Definition: resource.h:193
HANDLE WINAPI LoadImageW(_In_opt_ HINSTANCE, _In_ LPCWSTR, _In_ UINT, _In_ int, _In_ int, _In_ UINT)
Definition: cursoricon.c:2172
LONG_PTR LPARAM
Definition: windef.h:208
#define IDC_CREATE_OPTIONS
Definition: resource.h:188
#define IDS_HELP_OPTIONS
Definition: resource.h:192
LPWSTR BinPath
Definition: create.c:17
BOOL WINAPI CloseServiceHandle(SC_HANDLE hSCObject)
Definition: scm.c:576
smooth NULL
Definition: ftsmooth.c:416
_In_ LPGUID _In_ PVOID Data
Definition: classpnp.h:778
#define SERVICE_WIN32_OWN_PROCESS
Definition: cmtypes.h:960
struct _CREATE_DATA CREATE_DATA
unsigned int BOOL
Definition: ntddk_ex.h:94
PVOID WINAPI HeapAlloc(HANDLE, DWORD, SIZE_T)
HWND hSelf
Definition: create.c:14
HWND WINAPI GetDlgItem(_In_opt_ HWND, _In_ int)
static BOOL GetDataFromDialog(PCREATE_DATA Data)
Definition: create.c:112
int WINAPI GetWindowTextLengthW(_In_ HWND)
unsigned int UINT
Definition: ndis.h:50
LPWSTR DisplayName
Definition: create.c:16
#define IDC_CREATE_HELP
Definition: resource.h:191
SC_HANDLE WINAPI OpenSCManagerW(LPCWSTR lpMachineName, LPCWSTR lpDatabaseName, DWORD dwDesiredAccess)
Definition: scm.c:2010
#define WM_COMMAND
Definition: winuser.h:1716
VOID DisplayString(LPWSTR Msg)
Definition: misc.c:211
SC_HANDLE hSCManager
Definition: sc.c:12
HANDLE ProcessHeap
Definition: servman.c:15
static BOOL DoCreate(PCREATE_DATA Data)
Definition: create.c:26
#define IDC_CREATE_PATH
Definition: resource.h:186
int WINAPI GetWindowTextW(HWND hWnd, LPWSTR lpString, int nMaxCount)
Definition: window.c:1378
HICON hIcon
Definition: msconfig.c:44
HLOCAL NTAPI LocalFree(HLOCAL hMem)
Definition: heapmem.c:1577
LRESULT WINAPI SendMessageW(_In_ HWND, _In_ UINT, _In_ WPARAM, _In_ LPARAM)
#define HEAP_ZERO_MEMORY
Definition: compat.h:123
INT_PTR CALLBACK CreateHelpDialogProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
Definition: create.c:163
#define SC_MANAGER_ALL_ACCESS
Definition: winsvc.h:13
#define IDI_SM_ICON
Definition: resource.h:66
#define SendMessage
Definition: winuser.h:5709
LPWSTR ServiceName
Definition: create.c:15
#define LoadImage
Definition: winuser.h:5681
INT_PTR CALLBACK CreateDialogProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
Definition: create.c:218
WCHAR * LPWSTR
Definition: xmlstorage.h:184
#define IDCANCEL
Definition: winuser.h:825
#define SERVICE_DEMAND_START
Definition: cmtypes.h:976
static BOOL bHelpOpen
Definition: create.c:23
#define WM_INITDIALOG
Definition: winuser.h:1715
LPARAM lParam
Definition: combotst.c:139
#define LOWORD(l)
Definition: pedump.c:82
#define HeapFree(x, y, z)
Definition: compat.h:394
#define CreateDialog
Definition: winuser.h:5615