- Add the list of dependencies to the listbox so we know what else will be stopping

- Restructure the stop code

svn path=/trunk/; revision=40728
This commit is contained in:
Ged Murphy 2009-04-29 09:53:10 +00:00
parent e6400c3f8b
commit f0794d4f2c
4 changed files with 115 additions and 91 deletions

View file

@ -154,7 +154,7 @@ STYLE DS_SHELLFONT | WS_BORDER | WS_DLGFRAME | DS_MODALFRAME
BEGIN BEGIN
ICON IDI_WARNING, IDC_STATIC, 10, 8, 24, 22 ICON IDI_WARNING, IDC_STATIC, 10, 8, 24, 22
LTEXT "", IDC_STOP_DEPENDS, 40, 8, 170, 25 LTEXT "", IDC_STOP_DEPENDS, 40, 8, 170, 25
EDITTEXT IDC_DEL_DESC, 15, 40, 210, 60, WS_CHILD | WS_VISIBLE | WS_EX_STATICEDGE | ES_MULTILINE | ES_READONLY LISTBOX IDC_STOP_DEPENDS_LB, 15, 40, 210, 70, WS_CHILD | WS_VISIBLE | WS_EX_STATICEDGE | LBS_NOSEL
LTEXT "Do you want to stop these services?",IDC_STATIC, 15, 110, 150, 10 LTEXT "Do you want to stop these services?",IDC_STATIC, 15, 110, 150, 10
DEFPUSHBUTTON "Yes", IDOK, 60, 129, 54, 14 DEFPUSHBUTTON "Yes", IDOK, 60, 129, 54, 14
PUSHBUTTON "No", IDCANCEL, 120, 129, 54, 14 PUSHBUTTON "No", IDCANCEL, 120, 129, 54, 14

View file

@ -178,4 +178,5 @@
/* stop dependencies */ /* stop dependencies */
#define IDD_DLG_DEPEND_STOP 12000 #define IDD_DLG_DEPEND_STOP 12000
#define IDC_STOP_DEPENDS 12001 #define IDC_STOP_DEPENDS 12001
#define IDS_STOP_DEPENDS 12001 #define IDS_STOP_DEPENDS 12002
#define IDC_STOP_DEPENDS_LB 12003

View file

@ -23,7 +23,7 @@ DoStartService(PMAIN_WND_INFO Info,
DWORD dwMaxWait; DWORD dwMaxWait;
BOOL bRet = FALSE; BOOL bRet = FALSE;
hSCManager = OpenSCManagerW(NULL, hSCManager = OpenSCManager(NULL,
NULL, NULL,
SC_MANAGER_ALL_ACCESS); SC_MANAGER_ALL_ACCESS);
if (!hSCManager) if (!hSCManager)
@ -31,12 +31,12 @@ DoStartService(PMAIN_WND_INFO Info,
return FALSE; return FALSE;
} }
hService = OpenServiceW(hSCManager, hService = OpenService(hSCManager,
Info->pCurrentService->lpServiceName, Info->pCurrentService->lpServiceName,
SERVICE_START | SERVICE_QUERY_STATUS); SERVICE_START | SERVICE_QUERY_STATUS);
if (hService) if (hService)
{ {
bRet = StartServiceW(hService, bRet = StartService(hService,
0, 0,
NULL); NULL);
if (!bRet && GetLastError() == ERROR_SERVICE_ALREADY_RUNNING) if (!bRet && GetLastError() == ERROR_SERVICE_ALREADY_RUNNING)

View file

@ -9,8 +9,16 @@
#include "precomp.h" #include "precomp.h"
typedef struct _STOP_INFO
{
PMAIN_WND_INFO pInfo;
SC_HANDLE hSCManager;
SC_HANDLE hMainService;
} STOP_INFO, *PSTOP_INFO;
static BOOL static BOOL
StopService(PMAIN_WND_INFO pInfo, StopService(PSTOP_INFO pStopInfo,
SC_HANDLE hService) SC_HANDLE hService)
{ {
SERVICE_STATUS_PROCESS ServiceStatus; SERVICE_STATUS_PROCESS ServiceStatus;
@ -23,8 +31,8 @@ StopService(PMAIN_WND_INFO pInfo,
dwStartTime = GetTickCount(); dwStartTime = GetTickCount();
dwTimeout = 30000; // 30 secs dwTimeout = 30000; // 30 secs
hProgDlg = CreateProgressDialog(pInfo->hMainWnd, hProgDlg = CreateProgressDialog(pStopInfo->pInfo->hMainWnd,
pInfo->pCurrentService->lpServiceName, pStopInfo->pInfo->pCurrentService->lpServiceName,
IDS_PROGRESS_INFO_STOP); IDS_PROGRESS_INFO_STOP);
if (hProgDlg) if (hProgDlg)
{ {
@ -67,8 +75,7 @@ StopService(PMAIN_WND_INFO pInfo,
} }
static LPENUM_SERVICE_STATUS static LPENUM_SERVICE_STATUS
GetDependentServices(PMAIN_WND_INFO pInfo, GetDependentServices(SC_HANDLE hService,
SC_HANDLE hService,
LPDWORD lpdwCount) LPDWORD lpdwCount)
{ {
LPENUM_SERVICE_STATUS lpDependencies; LPENUM_SERVICE_STATUS lpDependencies;
@ -88,7 +95,7 @@ GetDependentServices(PMAIN_WND_INFO pInfo,
else else
{ {
if (GetLastError() != ERROR_MORE_DATA) if (GetLastError() != ERROR_MORE_DATA)
return NULL; // Unexpected error return NULL; /* Unexpected error */
lpDependencies = (LPENUM_SERVICE_STATUS)HeapAlloc(GetProcessHeap(), lpDependencies = (LPENUM_SERVICE_STATUS)HeapAlloc(GetProcessHeap(),
0, 0,
@ -120,8 +127,7 @@ GetDependentServices(PMAIN_WND_INFO pInfo,
} }
static BOOL static BOOL
StopDependentServices(PMAIN_WND_INFO pInfo, StopDependentServices(PSTOP_INFO pStopInfo,
SC_HANDLE hSCManager,
SC_HANDLE hService) SC_HANDLE hService)
{ {
LPENUM_SERVICE_STATUS lpDependencies; LPENUM_SERVICE_STATUS lpDependencies;
@ -129,7 +135,7 @@ StopDependentServices(PMAIN_WND_INFO pInfo,
DWORD dwCount; DWORD dwCount;
BOOL bRet = FALSE; BOOL bRet = FALSE;
lpDependencies = GetDependentServices(pInfo, hService, &dwCount); lpDependencies = GetDependentServices(hService, &dwCount);
if (lpDependencies) if (lpDependencies)
{ {
LPENUM_SERVICE_STATUS lpEnumServiceStatus; LPENUM_SERVICE_STATUS lpEnumServiceStatus;
@ -139,12 +145,12 @@ StopDependentServices(PMAIN_WND_INFO pInfo,
{ {
lpEnumServiceStatus = &lpDependencies[i]; lpEnumServiceStatus = &lpDependencies[i];
hDepService = OpenService(hSCManager, hDepService = OpenService(pStopInfo->hSCManager,
lpEnumServiceStatus->lpServiceName, lpEnumServiceStatus->lpServiceName,
SERVICE_STOP | SERVICE_QUERY_STATUS); SERVICE_STOP | SERVICE_QUERY_STATUS);
if (hDepService) if (hDepService)
{ {
bRet = StopService(pInfo, hDepService); bRet = StopService(pStopInfo, hDepService);
CloseServiceHandle(hDepService); CloseServiceHandle(hDepService);
@ -165,21 +171,11 @@ StopDependentServices(PMAIN_WND_INFO pInfo,
} }
static BOOL static BOOL
HasDependentServices(PMAIN_WND_INFO pInfo) HasDependentServices(SC_HANDLE hService)
{ {
SC_HANDLE hSCManager;
SC_HANDLE hService;
DWORD dwBytesNeeded, dwCount; DWORD dwBytesNeeded, dwCount;
BOOL bRet = FALSE; BOOL bRet = FALSE;
hSCManager = OpenSCManagerW(NULL,
NULL,
SC_MANAGER_ALL_ACCESS);
if (hSCManager)
{
hService = OpenServiceW(hSCManager,
pInfo->pCurrentService->lpServiceName,
SERVICE_ENUMERATE_DEPENDENTS);
if (hService) if (hService)
{ {
if (!EnumDependentServices(hService, if (!EnumDependentServices(hService,
@ -192,30 +188,29 @@ HasDependentServices(PMAIN_WND_INFO pInfo)
if (GetLastError() == ERROR_MORE_DATA) if (GetLastError() == ERROR_MORE_DATA)
bRet = TRUE; bRet = TRUE;
} }
CloseServiceHandle(hService);
}
CloseServiceHandle(hSCManager);
} }
return bRet; return bRet;
} }
static BOOL static BOOL
DoInitDependsDialog(PMAIN_WND_INFO pInfo, DoInitDependsDialog(PSTOP_INFO pStopInfo,
HWND hDlg) HWND hDlg)
{ {
SC_HANDLE hSCManager;
SC_HANDLE hService;
LPENUM_SERVICE_STATUS lpDependencies;
DWORD dwCount;
LPTSTR lpPartialStr, lpStr; LPTSTR lpPartialStr, lpStr;
DWORD fullLen; DWORD fullLen;
HICON hIcon = NULL; HICON hIcon = NULL;
BOOL bRet = FALSE; BOOL bRet = FALSE;
if (pInfo) if (pStopInfo)
{ {
SetWindowLongPtr(hDlg, SetWindowLongPtr(hDlg,
GWLP_USERDATA, GWLP_USERDATA,
(LONG_PTR)pInfo); (LONG_PTR)pStopInfo);
hIcon = (HICON)LoadImage(hInstance, hIcon = (HICON)LoadImage(hInstance,
MAKEINTRESOURCE(IDI_SM_ICON), MAKEINTRESOURCE(IDI_SM_ICON),
@ -232,18 +227,19 @@ DoInitDependsDialog(PMAIN_WND_INFO pInfo,
DestroyIcon(hIcon); DestroyIcon(hIcon);
} }
/* Add the label */
if (AllocAndLoadString(&lpPartialStr, if (AllocAndLoadString(&lpPartialStr,
hInstance, hInstance,
IDS_STOP_DEPENDS)) IDS_STOP_DEPENDS))
{ {
fullLen = _tcslen(lpPartialStr) + _tcslen(pInfo->pCurrentService->lpDisplayName) + 1; fullLen = _tcslen(lpPartialStr) + _tcslen(pStopInfo->pInfo->pCurrentService->lpDisplayName) + 1;
lpStr = HeapAlloc(ProcessHeap, lpStr = HeapAlloc(ProcessHeap,
0, 0,
fullLen * sizeof(TCHAR)); fullLen * sizeof(TCHAR));
if (lpStr) if (lpStr)
{ {
_sntprintf(lpStr, fullLen, lpPartialStr, pInfo->pCurrentService->lpDisplayName); _sntprintf(lpStr, fullLen, lpPartialStr, pStopInfo->pInfo->pCurrentService->lpDisplayName);
SendDlgItemMessage(hDlg, SendDlgItemMessage(hDlg,
IDC_STOP_DEPENDS, IDC_STOP_DEPENDS,
@ -262,6 +258,26 @@ DoInitDependsDialog(PMAIN_WND_INFO pInfo,
0, 0,
lpPartialStr); lpPartialStr);
} }
/* Get the list of dependencies */
lpDependencies = GetDependentServices(pStopInfo->hMainService, &dwCount);
if (lpDependencies)
{
LPENUM_SERVICE_STATUS lpEnumServiceStatus;
DWORD i;
for (i = 0; i < dwCount; i++)
{
lpEnumServiceStatus = &lpDependencies[i];
/* Add the service to the listbox */
SendDlgItemMessage(hDlg,
IDC_STOP_DEPENDS_LB,
LB_ADDSTRING,
0,
(LPARAM)lpEnumServiceStatus->lpDisplayName);
}
}
} }
return bRet; return bRet;
@ -274,13 +290,12 @@ StopDependsDialogProc(HWND hDlg,
WPARAM wParam, WPARAM wParam,
LPARAM lParam) LPARAM lParam)
{ {
PMAIN_WND_INFO pInfo = NULL; PSTOP_INFO pStopInfo = NULL;
/* Get the window context */ /* Get the window context */
pInfo = (PMAIN_WND_INFO)GetWindowLongPtr(hDlg, pStopInfo = (PSTOP_INFO)GetWindowLongPtr(hDlg,
GWLP_USERDATA); GWLP_USERDATA);
if (pInfo == NULL && message != WM_INITDIALOG) if (pStopInfo == NULL && message != WM_INITDIALOG)
{ {
return FALSE; return FALSE;
} }
@ -291,10 +306,10 @@ StopDependsDialogProc(HWND hDlg,
{ {
BOOL bRet = FALSE; BOOL bRet = FALSE;
pInfo = (PMAIN_WND_INFO)lParam; pStopInfo = (PSTOP_INFO)lParam;
if (pInfo != NULL) if (pStopInfo != NULL)
{ {
bRet = DoInitDependsDialog(pInfo, hDlg); bRet = DoInitDependsDialog(pStopInfo, hDlg);
} }
return bRet; return bRet;
@ -322,22 +337,15 @@ StopDependsDialogProc(HWND hDlg,
BOOL BOOL
DoStop(PMAIN_WND_INFO pInfo) DoStop(PMAIN_WND_INFO pInfo)
{ {
SC_HANDLE hSCManager = NULL; STOP_INFO stopInfo;
SC_HANDLE hSCManager;
SC_HANDLE hService; SC_HANDLE hService;
BOOL bHasDepends; BOOL bHasDepends;
BOOL bRet = FALSE; BOOL bRet = FALSE;
bHasDepends = HasDependentServices(pInfo); if (pInfo)
if (bHasDepends)
{ {
INT ret = DialogBoxParam(hInstance, stopInfo.pInfo = pInfo;
MAKEINTRESOURCE(IDD_DLG_DEPEND_STOP),
pInfo->hMainWnd,
StopDependsDialogProc,
(LPARAM)pInfo);
if (ret != IDOK)
return FALSE;
}
hSCManager = OpenSCManager(NULL, hSCManager = OpenSCManager(NULL,
NULL, NULL,
@ -349,20 +357,35 @@ DoStop(PMAIN_WND_INFO pInfo)
SERVICE_STOP | SERVICE_QUERY_STATUS | SERVICE_ENUMERATE_DEPENDENTS); SERVICE_STOP | SERVICE_QUERY_STATUS | SERVICE_ENUMERATE_DEPENDENTS);
if (hService) if (hService)
{ {
if (bHasDepends) stopInfo.hSCManager = hSCManager;
{ stopInfo.hMainService = hService;
StopDependentServices(pInfo,
hSCManager,
hService);
}
bRet = StopService(pInfo, hService); if (HasDependentServices(hService))
{
INT ret = DialogBoxParam(hInstance,
MAKEINTRESOURCE(IDD_DLG_DEPEND_STOP),
pInfo->hMainWnd,
StopDependsDialogProc,
(LPARAM)&stopInfo);
if (ret == IDOK)
{
if (StopDependentServices(&stopInfo, hService))
{
bRet = StopService(&stopInfo, hService);
}
}
}
else
{
bRet = StopService(&stopInfo, hService);
}
CloseServiceHandle(hService); CloseServiceHandle(hService);
} }
CloseServiceHandle(hSCManager); CloseServiceHandle(hSCManager);
} }
}
return bRet; return bRet;
} }