ReactOS Fundraising Campaign 2012
 
€ 4,410 / € 30,000

Information | Donate

Home | Info | Community | Development | myReactOS | Contact Us

  1. Home
  2. Community
  3. Development
  4. myReactOS
  5. Fundraiser 2012

  1. Main Page
  2. Alphabetical List
  3. Data Structures
  4. Directories
  5. File List
  6. Data Fields
  7. Globals
  8. Related Pages

ReactOS Development > Doxygen

create.c
Go to the documentation of this file.
00001 /*
00002  * PROJECT:     ReactOS Services
00003  * LICENSE:     GPL - See COPYING in the top level directory
00004  * FILE:        base/applications/mscutils/servman/create.c
00005  * PURPOSE:     Create a new service
00006  * COPYRIGHT:   Copyright 2006-2007 Ged Murphy <gedmurphy@reactos.org>
00007  *
00008  */
00009 
00010 #include "precomp.h"
00011 
00012 typedef struct _CREATE_DATA
00013 {
00014     HWND hSelf;
00015     LPTSTR ServiceName;
00016     LPTSTR DisplayName;
00017     LPTSTR BinPath;
00018     LPTSTR Description;
00019     LPTSTR Options;
00020 
00021 } CREATE_DATA, *PCREATE_DATA;
00022 
00023 static BOOL bHelpOpen = FALSE;
00024 
00025 static BOOL
00026 DoCreate(PCREATE_DATA Data)
00027 {
00028     SC_HANDLE hSCManager;
00029     SC_HANDLE hSc;
00030     BOOL bRet = FALSE;
00031 
00032     /* open handle to the SCM */
00033     hSCManager = OpenSCManager(NULL,
00034                                NULL,
00035                                SC_MANAGER_ALL_ACCESS);
00036     if (hSCManager)
00037     {
00038         hSc = CreateService(hSCManager,
00039                             Data->ServiceName,
00040                             Data->DisplayName,
00041                             SERVICE_ALL_ACCESS,
00042                             SERVICE_WIN32_OWN_PROCESS,
00043                             SERVICE_DEMAND_START,
00044                             SERVICE_ERROR_NORMAL,
00045                             Data->BinPath,
00046                             NULL,
00047                             NULL,
00048                             NULL,
00049                             NULL,
00050                             NULL);
00051 
00052         if (hSc)
00053         {
00054             LPTSTR lpSuccess;
00055 
00056             /* Set the service description as CreateService
00057                does not do this for us */
00058             SetServiceDescription(Data->ServiceName,
00059                                   Data->Description);
00060 
00061             /* report success to user */
00062             if (AllocAndLoadString(&lpSuccess,
00063                                    hInstance,
00064                                    IDS_CREATE_SUCCESS))
00065             {
00066                 DisplayString(lpSuccess);
00067 
00068                 HeapFree(ProcessHeap,
00069                          0,
00070                          lpSuccess);
00071             }
00072 
00073             CloseServiceHandle(hSc);
00074             bRet = TRUE;
00075         }
00076 
00077         CloseServiceHandle(hSCManager);
00078     }
00079 
00080     return bRet;
00081 }
00082 
00083 
00084 static LPTSTR
00085 GetStringFromDialog(PCREATE_DATA Data,
00086                     UINT id)
00087 {
00088     HWND hwnd;
00089     LPTSTR lpString = NULL;
00090     INT iLen = 0;
00091 
00092     hwnd = GetDlgItem(Data->hSelf,
00093                       id);
00094     if (hwnd)
00095     {
00096         iLen = GetWindowTextLength(hwnd);
00097         if (iLen)
00098         {
00099             lpString = (LPTSTR)HeapAlloc(ProcessHeap,
00100                                          0,
00101                                          (iLen + 1) * sizeof(TCHAR));
00102             if (lpString)
00103             {
00104                 GetWindowText(hwnd,
00105                               lpString,
00106                               iLen + 1);
00107             }
00108         }
00109     }
00110 
00111     return lpString;
00112 }
00113 
00114 
00115 static BOOL
00116 GetDataFromDialog(PCREATE_DATA Data)
00117 {
00118     BOOL bRet = FALSE;
00119 
00120     if ((Data->ServiceName = GetStringFromDialog(Data, IDC_CREATE_SERVNAME)))
00121     {
00122         if ((Data->DisplayName = GetStringFromDialog(Data, IDC_CREATE_DISPNAME)))
00123         {
00124             if ((Data->BinPath = GetStringFromDialog(Data, IDC_CREATE_PATH)))
00125             {
00126                 Data->Description = GetStringFromDialog(Data, IDC_CREATE_DESC);
00127                 Data->Options = GetStringFromDialog(Data, IDC_CREATE_OPTIONS);
00128 
00129                 bRet = TRUE;
00130             }
00131         }
00132     }
00133 
00134     return bRet;
00135 }
00136 
00137 static VOID
00138 FreeMemory(PCREATE_DATA Data)
00139 {
00140     if (Data->ServiceName != NULL)
00141         HeapFree(ProcessHeap,
00142                  0,
00143                  Data->ServiceName);
00144     if (Data->DisplayName != NULL)
00145         HeapFree(ProcessHeap,
00146                  0,
00147                  Data->DisplayName);
00148     if (Data->BinPath != NULL)
00149         HeapFree(ProcessHeap,
00150                  0,
00151                  Data->BinPath);
00152     if (Data->Description != NULL)
00153         HeapFree(ProcessHeap,
00154                  0,
00155                  Data->Description);
00156     if (Data->Options != NULL)
00157         HeapFree(ProcessHeap,
00158                  0,
00159                  Data->Options);
00160 
00161     HeapFree(ProcessHeap,
00162              0,
00163              Data);
00164 }
00165 
00166 
00167 INT_PTR CALLBACK
00168 CreateHelpDialogProc(HWND hDlg,
00169                      UINT message,
00170                      WPARAM wParam,
00171                      LPARAM lParam)
00172 {
00173     HWND hHelp;
00174     HICON hIcon = NULL;
00175     TCHAR Buf[1000];
00176 
00177     switch (message)
00178     {
00179         case WM_INITDIALOG:
00180         {
00181             hIcon = (HICON) LoadImage(hInstance,
00182                               MAKEINTRESOURCE(IDI_SM_ICON),
00183                               IMAGE_ICON,
00184                               16,
00185                               16,
00186                               0);
00187 
00188             SendMessage(hDlg,
00189                         WM_SETICON,
00190                         ICON_SMALL,
00191                         (LPARAM)hIcon);
00192 
00193             hHelp = GetDlgItem(hDlg,
00194                                IDC_CREATE_HELP);
00195 
00196             LoadString(hInstance,
00197                        IDS_HELP_OPTIONS,
00198                        Buf,
00199                        sizeof(Buf) / sizeof(TCHAR));
00200 
00201             SetWindowText(hHelp,
00202                           Buf);
00203 
00204             return TRUE;
00205         }
00206 
00207         case WM_COMMAND:
00208         {
00209             if ((LOWORD(wParam) == IDOK) || (LOWORD(wParam) == IDCANCEL))
00210             {
00211                 bHelpOpen = FALSE;
00212                 DestroyIcon(hIcon);
00213                 DestroyWindow(hDlg);
00214                 return TRUE;
00215             }
00216             break;
00217         }
00218     }
00219 
00220     return FALSE;
00221 }
00222 
00223 
00224 INT_PTR CALLBACK
00225 CreateDialogProc(HWND hDlg,
00226                  UINT message,
00227                  WPARAM wParam,
00228                  LPARAM lParam)
00229 {
00230     HICON hIcon = NULL;
00231 
00232     switch (message)
00233     {
00234         case WM_INITDIALOG:
00235         {
00236             hIcon = (HICON)LoadImage(hInstance,
00237                                      MAKEINTRESOURCE(IDI_SM_ICON),
00238                                      IMAGE_ICON,
00239                                      16,
00240                                      16,
00241                                      0);
00242             if (hIcon)
00243             {
00244                 SendMessage(hDlg,
00245                             WM_SETICON,
00246                             ICON_SMALL,
00247                             (LPARAM)hIcon);
00248                 DestroyIcon(hIcon);
00249             }
00250 
00251             return TRUE;
00252         }
00253 
00254         case WM_COMMAND:
00255         {
00256             switch (LOWORD(wParam))
00257             {
00258                 case IDOK:
00259                 {
00260                     PCREATE_DATA Data;
00261 
00262                     Data = (PCREATE_DATA)HeapAlloc(ProcessHeap,
00263                                                    HEAP_ZERO_MEMORY,
00264                                                    sizeof(CREATE_DATA));
00265                     if (Data)
00266                     {
00267                         Data->hSelf = hDlg;
00268 
00269                         if (GetDataFromDialog(Data))
00270                         {
00271                             DoCreate(Data);
00272                         }
00273                         else
00274                         {
00275                             /* Something went wrong, leave the dialog
00276                              * open so they can try again */
00277                             FreeMemory(Data);
00278                             break;
00279                         }
00280 
00281                         FreeMemory(Data);
00282                     }
00283 
00284                     EndDialog(hDlg,
00285                               LOWORD(wParam));
00286                     return TRUE;
00287                 }
00288 
00289                 case IDCANCEL:
00290                 {
00291                     EndDialog(hDlg,
00292                               LOWORD(wParam));
00293                     return TRUE;
00294                 }
00295 
00296                 case ID_CREATE_HELP:
00297                 {
00298                     HWND hHelp;
00299 
00300                     if (! bHelpOpen)
00301                     {
00302                         hHelp = CreateDialog(hInstance,
00303                                              MAKEINTRESOURCE(IDD_DLG_HELP_OPTIONS),
00304                                              hDlg,
00305                                              CreateHelpDialogProc);
00306                         if(hHelp != NULL)
00307                         {
00308                             bHelpOpen = TRUE;
00309                         }
00310                     }
00311                 }
00312                 break;
00313             }
00314         }
00315     }
00316 
00317     return FALSE;
00318 }

Generated on Sun May 27 2012 04:16:58 for ReactOS by doxygen 1.7.6.1

ReactOS is a registered trademark or a trademark of ReactOS Foundation in the United States and other countries.