1 /*
2  * PROJECT:     ReactOS Services
3  * LICENSE:     GPL - See COPYING in the top level directory
4  * FILE:        base/applications/mscutils/servman/delete.c
5  * PURPOSE:     Delete an existing service
6  * COPYRIGHT:   Copyright 2006-2007 Ged Murphy <gedmurphy@reactos.org>
7  *
8  */
9 
10 #include "precomp.h"
11 
12 static BOOL
13 DoDeleteService(PMAIN_WND_INFO Info,
14                 HWND hDlg)
15 {
16     SC_HANDLE hSCManager;
17     SC_HANDLE hSc;
18     BOOL bRet = FALSE;
19 
20     hSCManager = OpenSCManagerW(NULL,
21                                 NULL,
22                                 SC_MANAGER_ALL_ACCESS);
23     if (hSCManager)
24     {
25         hSc = OpenServiceW(hSCManager,
26                            Info->pCurrentService->lpServiceName,
27                            DELETE);
28         if (hSc)
29         {
30             if (DeleteService(hSc))
31             {
32                 LPWSTR lpSuccess;
33 
34                 /* report success to user */
35                 if (AllocAndLoadString(&lpSuccess,
36                                        hInstance,
37                                        IDS_DELETE_SUCCESS))
38                 {
39                     DisplayString(lpSuccess);
40 
41                     LocalFree(lpSuccess);
42                 }
43 
44                 bRet = TRUE;
45             }
46 
47             CloseServiceHandle(hSc);
48         }
49 
50         CloseServiceHandle(hSCManager);
51     }
52 
53     return bRet;
54 }
55 
56 INT_PTR CALLBACK
57 DeleteDialogProc(HWND hDlg,
58                  UINT message,
59                  WPARAM wParam,
60                  LPARAM lParam)
61 {
62     PMAIN_WND_INFO Info = NULL;
63     HICON hIcon = NULL;
64 
65     /* Get the window context */
66     Info = (PMAIN_WND_INFO)GetWindowLongPtr(hDlg,
67                                             GWLP_USERDATA);
68     if (Info == NULL && message != WM_INITDIALOG)
69     {
70         return FALSE;
71     }
72 
73     switch (message)
74     {
75         case WM_INITDIALOG:
76         {
77             LPWSTR lpDescription;
78 
79             Info = (PMAIN_WND_INFO)lParam;
80             if (Info != NULL)
81             {
82                 SetWindowLongPtrW(hDlg,
83                                   GWLP_USERDATA,
84                                   (LONG_PTR)Info);
85 
86                 hIcon = (HICON)LoadImageW(hInstance,
87                                           MAKEINTRESOURCE(IDI_SM_ICON),
88                                           IMAGE_ICON,
89                                           16,
90                                           16,
91                                           0);
92                 if (hIcon)
93                 {
94                     SendMessageW(hDlg,
95                                  WM_SETICON,
96                                  ICON_SMALL,
97                                  (LPARAM)hIcon);
98                     DestroyIcon(hIcon);
99                 }
100 
101                 SendDlgItemMessageW(hDlg,
102                                     IDC_DEL_NAME,
103                                     WM_SETTEXT,
104                                     0,
105                                     (LPARAM)Info->pCurrentService->lpDisplayName);
106 
107                 lpDescription = GetServiceDescription(Info->pCurrentService->lpServiceName);
108                 if (lpDescription)
109                 {
110                     SendDlgItemMessageW(hDlg,
111                                         IDC_DEL_DESC,
112                                         WM_SETTEXT,
113                                         0,
114                                         (LPARAM)lpDescription);
115                     HeapFree(ProcessHeap,
116                              0,
117                              lpDescription);
118                 }
119 
120                 return TRUE;
121             }
122 
123             return FALSE;
124         }
125 
126         case WM_COMMAND:
127         {
128             switch (LOWORD(wParam))
129             {
130                 case IDOK:
131                 {
132                     if (DoDeleteService(Info, hDlg))
133                     {
134                         (void)ListView_DeleteItem(Info->hListView,
135                                                   Info->SelectedItem);
136                         UpdateServiceCount(Info);
137                     }
138                     EndDialog(hDlg,
139                               LOWORD(wParam));
140                     return TRUE;
141                 }
142 
143                 case IDCANCEL:
144                 {
145                     EndDialog(hDlg,
146                               LOWORD(wParam));
147                     return TRUE;
148                 }
149             }
150         }
151     }
152 
153     return FALSE;
154 }
155