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 LPWSTR ServiceName; 16 LPWSTR DisplayName; 17 LPWSTR BinPath; 18 LPWSTR Description; 19 LPWSTR 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 = OpenSCManagerW(NULL, 34 NULL, 35 SC_MANAGER_ALL_ACCESS); 36 if (hSCManager) 37 { 38 hSc = CreateServiceW(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 LPWSTR 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 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 82 GetStringFromDialog(PCREATE_DATA Data, 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 116 if ((Data->ServiceName = GetStringFromDialog(Data, IDC_CREATE_SERVNAME))) 117 { 118 if ((Data->DisplayName = GetStringFromDialog(Data, IDC_CREATE_DISPNAME))) 119 { 120 if ((Data->BinPath = GetStringFromDialog(Data, IDC_CREATE_PATH))) 121 { 122 Data->Description = GetStringFromDialog(Data, IDC_CREATE_DESC); 123 Data->Options = GetStringFromDialog(Data, IDC_CREATE_OPTIONS); 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) 137 HeapFree(ProcessHeap, 138 0, 139 Data->ServiceName); 140 if (Data->DisplayName != NULL) 141 HeapFree(ProcessHeap, 142 0, 143 Data->DisplayName); 144 if (Data->BinPath != NULL) 145 HeapFree(ProcessHeap, 146 0, 147 Data->BinPath); 148 if (Data->Description != NULL) 149 HeapFree(ProcessHeap, 150 0, 151 Data->Description); 152 if (Data->Options != NULL) 153 HeapFree(ProcessHeap, 154 0, 155 Data->Options); 156 157 HeapFree(ProcessHeap, 158 0, 159 Data); 160 } 161 162 INT_PTR CALLBACK 163 CreateHelpDialogProc(HWND hDlg, 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, 177 MAKEINTRESOURCE(IDI_SM_ICON), 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, 189 IDC_CREATE_HELP); 190 191 LoadStringW(hInstance, 192 IDS_HELP_OPTIONS, 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 217 INT_PTR CALLBACK 218 CreateDialogProc(HWND hDlg, 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, 230 MAKEINTRESOURCE(IDI_SM_ICON), 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 255 Data = (PCREATE_DATA)HeapAlloc(ProcessHeap, 256 HEAP_ZERO_MEMORY, 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, 296 MAKEINTRESOURCE(IDD_DLG_HELP_OPTIONS), 297 hDlg, 298 CreateHelpDialogProc); 299 if(hHelp != NULL) 300 { 301 bHelpOpen = TRUE; 302 } 303 } 304 } 305 break; 306 } 307 } 308 } 309 310 return FALSE; 311 } 312