Process Hacker
srvcr.c
Go to the documentation of this file.
1 /*
2  * Process Hacker -
3  * service creation dialog
4  *
5  * Copyright (C) 2010-2013 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 <phapp.h>
24 #include <phsvccl.h>
25 #include <windowsx.h>
26 
27 INT_PTR CALLBACK PhpCreateServiceDlgProc(
28  _In_ HWND hwndDlg,
29  _In_ UINT uMsg,
30  _In_ WPARAM wParam,
31  _In_ LPARAM lParam
32  );
33 
35  _In_ HWND ParentWindowHandle
36  )
37 {
38  DialogBox(
40  MAKEINTRESOURCE(IDD_CREATESERVICE),
41  ParentWindowHandle,
43  );
44 }
45 
46 INT_PTR CALLBACK PhpCreateServiceDlgProc(
47  _In_ HWND hwndDlg,
48  _In_ UINT uMsg,
49  _In_ WPARAM wParam,
50  _In_ LPARAM lParam
51  )
52 {
53  switch (uMsg)
54  {
55  case WM_INITDIALOG:
56  {
57  PhCenterWindow(hwndDlg, GetParent(hwndDlg));
58 
60  sizeof(PhServiceTypeStrings) / sizeof(WCHAR *));
62  sizeof(PhServiceStartTypeStrings) / sizeof(WCHAR *));
64  sizeof(PhServiceErrorControlStrings) / sizeof(WCHAR *));
65 
66  PhSelectComboBoxString(GetDlgItem(hwndDlg, IDC_TYPE), L"Own Process", FALSE);
67  PhSelectComboBoxString(GetDlgItem(hwndDlg, IDC_STARTTYPE), L"Demand Start", FALSE);
68  PhSelectComboBoxString(GetDlgItem(hwndDlg, IDC_ERRORCONTROL), L"Ignore", FALSE);
69 
70  if (!PhElevated)
71  {
72  SendMessage(GetDlgItem(hwndDlg, IDOK), BCM_SETSHIELD, 0, TRUE);
73  }
74 
75  SendMessage(hwndDlg, WM_NEXTDLGCTL, (WPARAM)GetDlgItem(hwndDlg, IDC_NAME), TRUE);
76  }
77  break;
78  case WM_COMMAND:
79  {
80  switch (LOWORD(wParam))
81  {
82  case IDCANCEL:
83  {
84  EndDialog(hwndDlg, IDCANCEL);
85  }
86  break;
87  case IDOK:
88  {
89  NTSTATUS status = 0;
90  BOOLEAN success = FALSE;
91  SC_HANDLE scManagerHandle;
92  SC_HANDLE serviceHandle;
93  ULONG win32Result = 0;
94  PPH_STRING serviceName;
95  PPH_STRING serviceDisplayName;
96  PPH_STRING serviceTypeString;
97  PPH_STRING serviceStartTypeString;
98  PPH_STRING serviceErrorControlString;
99  ULONG serviceType;
100  ULONG serviceStartType;
101  ULONG serviceErrorControl;
102  PPH_STRING serviceBinaryPath;
103 
104  serviceName = PhAutoDereferenceObject(PhGetWindowText(GetDlgItem(hwndDlg, IDC_NAME)));
105  serviceDisplayName = PhAutoDereferenceObject(PhGetWindowText(GetDlgItem(hwndDlg, IDC_DISPLAYNAME)));
106 
107  serviceTypeString = PhAutoDereferenceObject(PhGetWindowText(GetDlgItem(hwndDlg, IDC_TYPE)));
108  serviceStartTypeString = PhAutoDereferenceObject(PhGetWindowText(GetDlgItem(hwndDlg, IDC_STARTTYPE)));
109  serviceErrorControlString = PhAutoDereferenceObject(PhGetWindowText(GetDlgItem(hwndDlg, IDC_ERRORCONTROL)));
110  serviceType = PhGetServiceTypeInteger(serviceTypeString->Buffer);
111  serviceStartType = PhGetServiceStartTypeInteger(serviceStartTypeString->Buffer);
112  serviceErrorControl = PhGetServiceErrorControlInteger(serviceErrorControlString->Buffer);
113 
114  serviceBinaryPath = PhAutoDereferenceObject(PhGetWindowText(GetDlgItem(hwndDlg, IDC_BINARYPATH)));
115 
116  if (PhElevated)
117  {
118  if (scManagerHandle = OpenSCManager(NULL, NULL, SC_MANAGER_CREATE_SERVICE))
119  {
120  if (serviceHandle = CreateService(
121  scManagerHandle,
122  serviceName->Buffer,
123  serviceDisplayName->Buffer,
124  SERVICE_CHANGE_CONFIG,
125  serviceType,
126  serviceStartType,
127  serviceErrorControl,
128  serviceBinaryPath->Buffer,
129  NULL,
130  NULL,
131  NULL,
132  NULL,
133  L""
134  ))
135  {
136  EndDialog(hwndDlg, IDOK);
137  CloseServiceHandle(serviceHandle);
138  success = TRUE;
139  }
140  else
141  {
142  win32Result = GetLastError();
143  }
144 
145  CloseServiceHandle(scManagerHandle);
146  }
147  else
148  {
149  win32Result = GetLastError();
150  }
151  }
152  else
153  {
154  if (PhUiConnectToPhSvc(hwndDlg, FALSE))
155  {
156  status = PhSvcCallCreateService(
157  serviceName->Buffer,
158  serviceDisplayName->Buffer,
159  serviceType,
160  serviceStartType,
161  serviceErrorControl,
162  serviceBinaryPath->Buffer,
163  NULL,
164  NULL,
165  NULL,
166  NULL,
167  L""
168  );
170 
171  if (NT_SUCCESS(status))
172  {
173  EndDialog(hwndDlg, IDOK);
174  success = TRUE;
175  }
176  }
177  else
178  {
179  // User cancelled elevation.
180  success = TRUE;
181  }
182  }
183 
184  if (!success)
185  PhShowStatus(hwndDlg, L"Unable to create the service", status, win32Result);
186  }
187  break;
188  case IDC_BROWSE:
189  {
190  static PH_FILETYPE_FILTER filters[] =
191  {
192  { L"Executable files (*.exe;*.sys)", L"*.exe;*.sys" },
193  { L"All files (*.*)", L"*.*" }
194  };
195  PVOID fileDialog;
196  PPH_STRING fileName;
197 
198  fileDialog = PhCreateOpenFileDialog();
199  PhSetFileDialogFilter(fileDialog, filters, sizeof(filters) / sizeof(PH_FILETYPE_FILTER));
200 
201  fileName = PhGetFileName(PhaGetDlgItemText(hwndDlg, IDC_BINARYPATH));
202  PhSetFileDialogFileName(fileDialog, fileName->Buffer);
203  PhDereferenceObject(fileName);
204 
205  if (PhShowFileDialog(hwndDlg, fileDialog))
206  {
207  fileName = PhGetFileDialogFileName(fileDialog);
208  SetDlgItemText(hwndDlg, IDC_BINARYPATH, fileName->Buffer);
209  PhDereferenceObject(fileName);
210  }
211 
212  PhFreeFileDialog(fileDialog);
213  }
214  break;
215  }
216  }
217  break;
218  }
219 
220  return FALSE;
221 }