ReactOS  0.4.11-dev-946-g431643b
stop_dependencies.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/stop_dependencies.c
5  * PURPOSE: Routines related to stopping dependent services
6  * COPYRIGHT: Copyright 2006-2015 Ged Murphy <gedmurphy@reactos.org>
7  *
8  */
9 
10 #include "precomp.h"
11 
12 typedef struct _STOP_DATA
13 {
17 
19 
20 static LPWSTR
21 AddServiceToList(LPWSTR *lpServiceList,
22  LPWSTR lpServiceToAdd)
23 {
24  LPWSTR lpNewList = NULL;
25  LPWSTR ptr;
26  DWORD dwToAddSize;
27  DWORD dwCurSize;
28 
29  dwToAddSize = wcslen(lpServiceToAdd) + 1;
30 
31  /* Is this is the first in the list? */
32  if (!*lpServiceList)
33  {
34  /* Add another char for double null */
35  dwToAddSize++;
36 
37  lpNewList = HeapAlloc(GetProcessHeap(),
38  0,
39  dwToAddSize * sizeof(WCHAR));
40  if (lpNewList)
41  {
42  /* Copy the service name */
43  StringCchCopy(lpNewList,
44  dwToAddSize,
45  lpServiceToAdd);
46 
47  /* Add the double null char */
48  lpNewList[dwToAddSize - 1] = L'\0';
49  }
50  }
51  else
52  {
53  ptr = *lpServiceList;
54  dwCurSize = 0;
55 
56  /* Get the list size */
57  for (;;)
58  {
59  /* Break when we hit the double null */
60  if (*ptr == L'\0' && *(ptr + 1) == L'\0')
61  break;
62 
63  ptr++;
64  dwCurSize++;
65  }
66  dwCurSize++;
67 
68  /* Add another char for double null */
69  dwCurSize++;
70 
71  /* Extend the list size */
72  lpNewList = HeapReAlloc(GetProcessHeap(),
73  0,
74  *lpServiceList,
75  (dwCurSize + dwToAddSize) * sizeof(WCHAR));
76  if (lpNewList)
77  {
78  /* Copy the service name */
79  StringCchCopy(&lpNewList[dwCurSize - 1],
80  dwToAddSize,
81  lpServiceToAdd);
82 
83  /* Add the double null char */
84  lpNewList[dwCurSize + dwToAddSize - 1] = L'\0';
85  }
86  }
87 
88  return lpNewList;
89 }
90 
91 static BOOL
93  LPWSTR lpServiceName)
94 {
95  LPENUM_SERVICE_STATUS lpServiceStatus;
96  DWORD dwCount, i;
97  BOOL bRet = FALSE;
98 
99  /* Get a list of service dependents */
100  lpServiceStatus = TV2_GetDependants(lpServiceName, &dwCount);
101  if (lpServiceStatus)
102  {
103  for (i = 0; i < dwCount; i++)
104  {
105  /* Does this service need stopping? */
106  if (lpServiceStatus[i].ServiceStatus.dwCurrentState != SERVICE_STOPPED &&
107  lpServiceStatus[i].ServiceStatus.dwCurrentState != SERVICE_STOP_PENDING)
108  {
109  /* Add the service to the list */
110  *lpServiceList = AddServiceToList(lpServiceList, lpServiceStatus[i].lpServiceName);
111 
112  /* We've got one */
113  bRet = TRUE;
114  }
115  }
116 
118  0,
119  lpServiceStatus);
120  }
121 
122  return bRet;
123 }
124 
125 LPWSTR
127 {
128  LPWSTR lpServiceList = NULL;
129 
130  /* Call recursive function to get our list */
131  if (BuildListOfServicesToStop(&lpServiceList, lpServiceName))
132  return lpServiceList;
133  else
134  return NULL;
135 }
136 
137 static VOID
138 AddServiceNamesToStop(HWND hServiceListBox,
139  LPWSTR lpServiceList)
140 {
141  LPQUERY_SERVICE_CONFIG lpServiceConfig;
142  LPWSTR lpStr;
143 
144  lpStr = lpServiceList;
145 
146  /* Loop through all the services in the list */
147  for (;;)
148  {
149  /* Break when we hit the double null */
150  if (*lpStr == L'\0' && *(lpStr + 1) == L'\0')
151  break;
152 
153  /* If this isn't our first time in the loop we'll
154  have been left on a null char */
155  if (*lpStr == L'\0')
156  lpStr++;
157 
158  /* Get the service's display name */
159  lpServiceConfig = GetServiceConfig(lpStr);
160  if (lpServiceConfig)
161  {
162  /* Add the service to the listbox */
163  SendMessageW(hServiceListBox,
164  LB_ADDSTRING,
165  0,
166  (LPARAM)lpServiceConfig->lpDisplayName);
167 
168  HeapFree(GetProcessHeap(), 0, lpServiceConfig);
169  }
170 
171  /* Move onto the next string */
172  while (*lpStr != L'\0')
173  lpStr++;
174  }
175 }
176 
177 static BOOL
179  UINT Message,
180  WPARAM wParam,
181  LPARAM lParam)
182 {
183  PSTOP_DATA StopData;
184  HWND hServiceListBox;
185  LPWSTR lpPartialStr, lpStr;
186  DWORD fullLen;
187  HICON hIcon = NULL;
188  BOOL bRet = FALSE;
189 
190  StopData = (PSTOP_DATA)lParam;
191 
192 
193  /* Load the icon for the window */
194  hIcon = (HICON)LoadImageW(hInstance,
196  IMAGE_ICON,
199  0);
200  if (hIcon)
201  {
202  /* Set it */
203  SendMessageW(hDlg,
204  WM_SETICON,
205  ICON_SMALL,
206  (LPARAM)hIcon);
207  DestroyIcon(hIcon);
208  }
209 
210  /* Load the stop depends note */
211  if (AllocAndLoadString(&lpPartialStr,
212  hInstance,
214  {
215  /* Get the length required */
216  fullLen = wcslen(lpPartialStr) + wcslen(StopData->DisplayName) + 1;
217 
218  lpStr = HeapAlloc(ProcessHeap,
219  0,
220  fullLen * sizeof(WCHAR));
221  if (lpStr)
222  {
223  /* Add the service name to the depends note */
224  _snwprintf(lpStr,
225  fullLen,
226  lpPartialStr,
227  StopData->DisplayName);
228 
229  /* Add the string to the dialog */
230  SendDlgItemMessageW(hDlg,
232  WM_SETTEXT,
233  0,
234  (LPARAM)lpStr);
235 
237  0,
238  lpStr);
239 
240  bRet = TRUE;
241  }
242 
243  LocalFree(lpPartialStr);
244  }
245 
246  /* Display the list of services which need stopping */
247  hServiceListBox = GetDlgItem(hDlg, IDC_STOP_DEPENDS_LB);
248  if (hServiceListBox)
249  {
250  AddServiceNamesToStop(hServiceListBox,
251  (LPWSTR)StopData->ServiceList);
252  }
253 
254  return bRet;
255 }
256 
259  UINT Message,
260  WPARAM wParam,
261  LPARAM lParam)
262 {
263 
264  switch (Message)
265  {
266  case WM_INITDIALOG:
267  {
268  return InitDialog(hDlg,
269  Message,
270  wParam,
271  lParam);
272  }
273 
274  case WM_COMMAND:
275  {
276  switch (LOWORD(wParam))
277  {
278  case IDOK:
279  case IDCANCEL:
280  {
281  EndDialog(hDlg,
282  LOWORD(wParam));
283  return TRUE;
284  }
285  }
286  }
287  }
288 
289  return FALSE;
290 }
291 
292 BOOL
297 {
298  STOP_DATA StopData;
299  INT_PTR Result;
300 
301  StopData.ServiceName = ServiceName;
302  StopData.DisplayName = DisplayName;
303  StopData.ServiceList = ServiceList;
304 
305  Result = DialogBoxParamW(hInstance,
307  hParent,
309  (LPARAM)&StopData);
310  if (Result == IDOK)
311  return TRUE;
312 
313  return FALSE;
314 }
#define MAKEINTRESOURCE
Definition: winuser.h:591
LPWSTR DisplayName
static HICON
Definition: imagelist.c:84
#define TRUE
Definition: types.h:120
#define IDOK
Definition: winuser.h:824
#define IMAGE_ICON
Definition: winuser.h:212
INT_PTR WINAPI DialogBoxParamW(_In_opt_ HINSTANCE, _In_ LPCWSTR, _In_opt_ HWND, _In_opt_ DLGPROC, _In_ LPARAM)
DWORD dwCurrentState
Definition: winsvc.h:100
#define IDC_STOP_DEPENDS_LB
Definition: resource.h:218
#define LB_ADDSTRING
Definition: winuser.h:1992
BOOL WINAPI DestroyIcon(_In_ HICON)
Definition: cursoricon.c:2022
__wchar_t WCHAR
Definition: xmlstorage.h:180
#define IDD_DLG_DEPEND_STOP
Definition: resource.h:215
#define CALLBACK
Definition: compat.h:27
SERVICE_STATUS ServiceStatus
Definition: winsvc.h:127
int32_t INT_PTR
Definition: typedefs.h:62
static INT AllocAndLoadString(OUT LPTSTR *lpTarget, IN HINSTANCE hInst, IN UINT uID)
Definition: misc.c:60
BOOL WINAPI EndDialog(_In_ HWND, _In_ INT_PTR)
WPARAM wParam
Definition: combotst.c:138
LPENUM_SERVICE_STATUS TV2_GetDependants(LPWSTR lpServiceName, LPDWORD lpdwCount)
UINT_PTR WPARAM
Definition: windef.h:207
#define ICON_SMALL
Definition: tnclass.cpp:48
LPWSTR GetListOfServicesToStop(LPWSTR lpServiceName)
GLenum GLclampf GLint i
Definition: glfuncs.h:14
HINSTANCE hInstance
Definition: charmap.c:20
#define SERVICE_STOPPED
Definition: winsvc.h:21
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
static PVOID ptr
Definition: dispmode.c:27
int _snwprintf(wchar_t *buffer, size_t count, const wchar_t *format,...)
smooth NULL
Definition: ftsmooth.c:416
_At_(*)(_In_ PWSK_CLIENT Client, _In_opt_ PUNICODE_STRING NodeName, _In_opt_ PUNICODE_STRING ServiceName, _In_opt_ ULONG NameSpace, _In_opt_ GUID *Provider, _In_opt_ PADDRINFOEXW Hints, _Outptr_ PADDRINFOEXW *Result, _In_opt_ PEPROCESS OwningProcess, _In_opt_ PETHREAD OwningThread, _Inout_ PIRP Irp Result)(Mem)) NTSTATUS(WSKAPI *PFN_WSK_GET_ADDRESS_INFO
Definition: wsk.h:426
#define WM_SETTEXT
Definition: winuser.h:1599
unsigned int BOOL
Definition: ntddk_ex.h:94
#define GetProcessHeap()
Definition: compat.h:395
PVOID WINAPI HeapAlloc(HANDLE, DWORD, SIZE_T)
#define IDS_STOP_DEPENDS
Definition: resource.h:217
HWND WINAPI GetDlgItem(_In_opt_ HWND, _In_ int)
unsigned int UINT
Definition: ndis.h:50
int WINAPI GetSystemMetrics(_In_ int)
unsigned long DWORD
Definition: ntddk_ex.h:95
LRESULT WINAPI SendDlgItemMessageW(_In_ HWND, _In_ int, _In_ UINT, _In_ WPARAM, _In_ LPARAM)
struct _STOP_DATA STOP_DATA
#define SM_CXSMICON
Definition: winuser.h:1002
CHAR Message[80]
Definition: alive.c:5
static const WCHAR L[]
Definition: oid.c:1087
#define StringCchCopy
Definition: strsafe.h:139
LPQUERY_SERVICE_CONFIG GetServiceConfig(LPWSTR lpServiceName)
Definition: query.c:29
const DOCKBAR PVOID HWND hParent
Definition: tooldock.h:22
static VOID AddServiceNamesToStop(HWND hServiceListBox, LPWSTR lpServiceList)
#define WM_COMMAND
Definition: winuser.h:1716
static BOOL InitDialog(HWND hDlg, UINT Message, WPARAM wParam, LPARAM lParam)
HANDLE ProcessHeap
Definition: servman.c:15
static BOOL BuildListOfServicesToStop(LPWSTR *lpServiceList, LPWSTR lpServiceName)
INT_PTR CALLBACK StopDependsDialogProc(HWND hDlg, UINT Message, WPARAM wParam, LPARAM lParam)
BOOL CreateStopDependsDialog(HWND hParent, LPWSTR ServiceName, LPWSTR DisplayName, LPWSTR ServiceList)
LPWSTR ServiceName
struct _STOP_DATA * PSTOP_DATA
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 HeapReAlloc
Definition: compat.h:393
#define IDC_STOP_DEPENDS
Definition: resource.h:216
static LPWSTR AddServiceToList(LPWSTR *lpServiceList, LPWSTR lpServiceToAdd)
SERVICE_STATUS ServiceStatus
Definition: dhcpcsvc.c:18
#define IDI_SM_ICON
Definition: resource.h:66
#define MAKEINTRESOURCEW(i)
Definition: winuser.h:582
WCHAR * LPWSTR
Definition: xmlstorage.h:184
#define IDCANCEL
Definition: winuser.h:825
LPWSTR ServiceList
#define WM_INITDIALOG
Definition: winuser.h:1715
#define SERVICE_STOP_PENDING
Definition: winsvc.h:23
LPARAM lParam
Definition: combotst.c:139
#define LOWORD(l)
Definition: pedump.c:82
size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)
#define HeapFree(x, y, z)
Definition: compat.h:394