Process Hacker
srvprgrs.c
Go to the documentation of this file.
1 /*
2  * Process Hacker Extended Services -
3  * progress dialog
4  *
5  * Copyright (C) 2010 wj32
6  *
7  * This file is part of Process Hacker.
8  *
9  * Process Hacker is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * Process Hacker is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with Process Hacker. If not, see <http://www.gnu.org/licenses/>.
21  */
22 
23 #include <phdk.h>
24 #include <windowsx.h>
25 #include "extsrv.h"
26 #include "resource.h"
27 
28 typedef struct _RESTART_SERVICE_CONTEXT
29 {
30  PPH_SERVICE_ITEM ServiceItem;
31  SC_HANDLE ServiceHandle;
32  BOOLEAN Starting;
33  BOOLEAN DisableTimer;
35 
36 INT_PTR CALLBACK EspRestartServiceDlgProc(
37  _In_ HWND hwndDlg,
38  _In_ UINT uMsg,
39  _In_ WPARAM wParam,
40  _In_ LPARAM lParam
41  );
42 
44  _In_ HWND hWnd,
45  _In_ PPH_SERVICE_ITEM ServiceItem,
46  _In_ SC_HANDLE ServiceHandle
47  )
48 {
50 
51  context.ServiceItem = ServiceItem;
52  context.ServiceHandle = ServiceHandle;
53  context.Starting = FALSE;
54  context.DisableTimer = FALSE;
55 
56  DialogBoxParam(
58  MAKEINTRESOURCE(IDD_SRVPROGRESS),
59  hWnd,
61  (LPARAM)&context
62  );
63 }
64 
65 INT_PTR CALLBACK EspRestartServiceDlgProc(
66  _In_ HWND hwndDlg,
67  _In_ UINT uMsg,
68  _In_ WPARAM wParam,
69  _In_ LPARAM lParam
70  )
71 {
73 
74  if (uMsg == WM_INITDIALOG)
75  {
76  context = (PRESTART_SERVICE_CONTEXT)lParam;
77  SetProp(hwndDlg, L"Context", (HANDLE)context);
78  }
79  else
80  {
81  context = (PRESTART_SERVICE_CONTEXT)GetProp(hwndDlg, L"Context");
82 
83  if (uMsg == WM_DESTROY)
84  RemoveProp(hwndDlg, L"Context");
85  }
86 
87  if (!context)
88  return FALSE;
89 
90  switch (uMsg)
91  {
92  case WM_INITDIALOG:
93  {
94  PhCenterWindow(hwndDlg, GetParent(hwndDlg));
95 
96  // TODO: Use the progress information.
97  PhSetWindowStyle(GetDlgItem(hwndDlg, IDC_PROGRESS), PBS_MARQUEE, PBS_MARQUEE);
98  SendMessage(GetDlgItem(hwndDlg, IDC_PROGRESS), PBM_SETMARQUEE, TRUE, 75);
99 
100  SetDlgItemText(hwndDlg, IDC_MESSAGE, PhaFormatString(L"Attempting to stop %s...", context->ServiceItem->Name->Buffer)->Buffer);
101 
102  if (PhUiStopService(hwndDlg, context->ServiceItem))
103  {
104  SetTimer(hwndDlg, 1, 250, NULL);
105  }
106  else
107  {
108  EndDialog(hwndDlg, IDCANCEL);
109  }
110  }
111  break;
112  case WM_COMMAND:
113  {
114  switch (LOWORD(wParam))
115  {
116  case IDCANCEL:
117  {
118  EndDialog(hwndDlg, IDCANCEL);
119  }
120  break;
121  }
122  }
123  break;
124  case WM_TIMER:
125  {
126  if (wParam == 1 && !context->DisableTimer)
127  {
128  SERVICE_STATUS serviceStatus;
129 
130  if (QueryServiceStatus(context->ServiceHandle, &serviceStatus))
131  {
132  if (!context->Starting && serviceStatus.dwCurrentState == SERVICE_STOPPED)
133  {
134  // The service is stopped, so start the service now.
135 
136  SetDlgItemText(hwndDlg, IDC_MESSAGE,
137  PhaFormatString(L"Attempting to start %s...", context->ServiceItem->Name->Buffer)->Buffer);
138  context->DisableTimer = TRUE;
139 
140  if (PhUiStartService(hwndDlg, context->ServiceItem))
141  {
142  context->DisableTimer = FALSE;
143  context->Starting = TRUE;
144  }
145  else
146  {
147  EndDialog(hwndDlg, IDCANCEL);
148  }
149  }
150  else if (context->Starting && serviceStatus.dwCurrentState == SERVICE_RUNNING)
151  {
152  EndDialog(hwndDlg, IDOK);
153  }
154  }
155  }
156  }
157  break;
158  }
159 
160  return FALSE;
161 }