Process Hacker
chproc.c
Go to the documentation of this file.
1 /*
2  * Process Hacker -
3  * choose process 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 <phapp.h>
24 
25 typedef struct _CHOOSE_PROCESS_DIALOG_CONTEXT
26 {
27  PWSTR Message;
28  HANDLE ProcessId;
29 
30  PH_LAYOUT_MANAGER LayoutManager;
31  RECT MinimumSize;
32  HIMAGELIST ImageList;
33  HWND ListViewHandle;
35 
36 INT_PTR CALLBACK PhpChooseProcessDlgProc(
37  _In_ HWND hwndDlg,
38  _In_ UINT uMsg,
39  _In_ WPARAM wParam,
40  _In_ LPARAM lParam
41  );
42 
44  _In_ HWND ParentWindowHandle,
45  _In_ PWSTR Message,
46  _Out_ PHANDLE ProcessId
47  )
48 {
50 
51  context.Message = Message;
52  context.ProcessId = NULL;
53 
54  if (DialogBoxParam(
56  MAKEINTRESOURCE(IDD_CHOOSEPROCESS),
57  ParentWindowHandle,
59  (LPARAM)&context
60  ) == IDOK)
61  {
62  *ProcessId = context.ProcessId;
63 
64  return TRUE;
65  }
66  else
67  {
68  return FALSE;
69  }
70 }
71 
72 static VOID PhpRefreshProcessList(
73  _In_ HWND hwndDlg,
75  )
76 {
77  NTSTATUS status;
78  HWND lvHandle;
79  PVOID processes;
81 
82  lvHandle = Context->ListViewHandle;
83 
84  ListView_DeleteAllItems(lvHandle);
85  ImageList_RemoveAll(Context->ImageList);
86 
87  if (!NT_SUCCESS(status = PhEnumProcesses(&processes)))
88  {
89  PhShowStatus(hwndDlg, L"Unable to enumerate processes", status, 0);
90  return;
91  }
92 
94 
95  process = PH_FIRST_PROCESS(processes);
96 
97  do
98  {
99  INT lvItemIndex;
100  PPH_STRING name;
101  HANDLE processHandle;
102  PPH_STRING fileName = NULL;
103  HICON icon = NULL;
104  WCHAR processIdString[PH_INT32_STR_LEN_1];
105  PPH_STRING userName = NULL;
106  INT imageIndex;
107 
108  if (process->UniqueProcessId != SYSTEM_IDLE_PROCESS_ID)
109  name = PhCreateStringFromUnicodeString(&process->ImageName);
110  else
112 
113  lvItemIndex = PhAddListViewItem(lvHandle, MAXINT, name->Buffer, process->UniqueProcessId);
114  PhDereferenceObject(name);
115 
116  if (NT_SUCCESS(PhOpenProcess(&processHandle, ProcessQueryAccess, process->UniqueProcessId)))
117  {
118  HANDLE tokenHandle;
119  PTOKEN_USER user;
120 
122  PhGetProcessImageFileName(processHandle, &fileName);
123 
124  if (NT_SUCCESS(PhOpenProcessToken(&tokenHandle, TOKEN_QUERY, processHandle)))
125  {
126  if (NT_SUCCESS(PhGetTokenUser(tokenHandle, &user)))
127  {
128  userName = PhGetSidFullName(user->User.Sid, TRUE, NULL);
129  PhFree(user);
130  }
131 
132  NtClose(tokenHandle);
133  }
134 
135  NtClose(processHandle);
136  }
137 
138  if (process->UniqueProcessId == SYSTEM_IDLE_PROCESS_ID && !userName && PhLocalSystemName)
139  PhSetReference(&userName, PhLocalSystemName);
140 
143 
144  if (process->UniqueProcessId == SYSTEM_PROCESS_ID)
145  fileName = PhGetKernelFileName();
146 
147  if (fileName)
148  PhMoveReference(&fileName, PhGetFileName(fileName));
149 
150  icon = PhGetFileShellIcon(PhGetString(fileName), L".exe", FALSE);
151 
152  // Icon
153  if (icon)
154  {
155  imageIndex = ImageList_AddIcon(Context->ImageList, icon);
156  PhSetListViewItemImageIndex(Context->ListViewHandle, lvItemIndex, imageIndex);
157  DestroyIcon(icon);
158  }
159 
160  // PID
161  PhPrintUInt32(processIdString, (ULONG)process->UniqueProcessId);
162  PhSetListViewSubItem(Context->ListViewHandle, lvItemIndex, 1, processIdString);
163 
164  // User Name
165  PhSetListViewSubItem(Context->ListViewHandle, lvItemIndex, 2, PhGetString(userName));
166 
167  if (userName) PhDereferenceObject(userName);
168  if (fileName) PhDereferenceObject(fileName);
169  } while (process = PH_NEXT_PROCESS(process));
170 
171  PhFree(processes);
172 
173  ExtendedListView_SortItems(lvHandle);
174  ExtendedListView_SetRedraw(lvHandle, TRUE);
175 }
176 
177 INT_PTR CALLBACK PhpChooseProcessDlgProc(
178  _In_ HWND hwndDlg,
179  _In_ UINT uMsg,
180  _In_ WPARAM wParam,
181  _In_ LPARAM lParam
182  )
183 {
184  PCHOOSE_PROCESS_DIALOG_CONTEXT context = NULL;
185 
186  if (uMsg == WM_INITDIALOG)
187  {
188  context = (PCHOOSE_PROCESS_DIALOG_CONTEXT)lParam;
189  SetProp(hwndDlg, PhMakeContextAtom(), (HANDLE)context);
190  }
191  else
192  {
193  context = (PCHOOSE_PROCESS_DIALOG_CONTEXT)GetProp(hwndDlg, PhMakeContextAtom());
194 
195  if (uMsg == WM_DESTROY)
196  {
197  RemoveProp(hwndDlg, PhMakeContextAtom());
198  }
199  }
200 
201  if (!context)
202  return FALSE;
203 
204  switch (uMsg)
205  {
206  case WM_INITDIALOG:
207  {
208  HWND lvHandle;
209 
210  PhCenterWindow(hwndDlg, GetParent(hwndDlg));
211 
212  SetDlgItemText(hwndDlg, IDC_MESSAGE, context->Message);
213 
214  PhInitializeLayoutManager(&context->LayoutManager, hwndDlg);
215  PhAddLayoutItem(&context->LayoutManager, GetDlgItem(hwndDlg, IDC_MESSAGE), NULL,
217  PhAddLayoutItem(&context->LayoutManager, GetDlgItem(hwndDlg, IDC_LIST), NULL,
218  PH_ANCHOR_ALL);
219  PhAddLayoutItem(&context->LayoutManager, GetDlgItem(hwndDlg, IDOK), NULL,
221  PhAddLayoutItem(&context->LayoutManager, GetDlgItem(hwndDlg, IDCANCEL), NULL,
223  PhAddLayoutItem(&context->LayoutManager, GetDlgItem(hwndDlg, IDC_REFRESH), NULL,
225  PhLayoutManagerLayout(&context->LayoutManager);
226 
227  context->MinimumSize.left = 0;
228  context->MinimumSize.top = 0;
229  context->MinimumSize.right = 280;
230  context->MinimumSize.bottom = 170;
231  MapDialogRect(hwndDlg, &context->MinimumSize);
232 
233  context->ListViewHandle = lvHandle = GetDlgItem(hwndDlg, IDC_LIST);
234  context->ImageList = ImageList_Create(PhSmallIconSize.X, PhSmallIconSize.Y, ILC_COLOR32 | ILC_MASK, 0, 40);
235 
236  PhSetListViewStyle(lvHandle, FALSE, TRUE);
237  PhSetControlTheme(lvHandle, L"explorer");
238  PhAddListViewColumn(lvHandle, 0, 0, 0, LVCFMT_LEFT, 180, L"Name");
239  PhAddListViewColumn(lvHandle, 1, 1, 1, LVCFMT_LEFT, 60, L"PID");
240  PhAddListViewColumn(lvHandle, 2, 2, 2, LVCFMT_LEFT, 160, L"User Name");
241  PhSetExtendedListView(lvHandle);
242 
243  ListView_SetImageList(lvHandle, context->ImageList, LVSIL_SMALL);
244 
245  PhpRefreshProcessList(hwndDlg, context);
246 
247  EnableWindow(GetDlgItem(hwndDlg, IDOK), FALSE);
248  }
249  break;
250  case WM_DESTROY:
251  {
252  ImageList_Destroy(context->ImageList);
253  PhDeleteLayoutManager(&context->LayoutManager);
254  }
255  break;
256  case WM_COMMAND:
257  {
258  switch (LOWORD(wParam))
259  {
260  case IDCANCEL:
261  {
262  EndDialog(hwndDlg, IDCANCEL);
263  }
264  break;
265  case IDOK:
266  {
267  if (ListView_GetSelectedCount(context->ListViewHandle) == 1)
268  {
269  context->ProcessId = (HANDLE)PhGetSelectedListViewItemParam(context->ListViewHandle);
270  EndDialog(hwndDlg, IDOK);
271  }
272  }
273  break;
274  case IDC_REFRESH:
275  {
276  PhpRefreshProcessList(hwndDlg, context);
277  }
278  break;
279  }
280  }
281  break;
282  case WM_NOTIFY:
283  {
284  LPNMHDR header = (LPNMHDR)lParam;
285 
286  switch (header->code)
287  {
288  case LVN_ITEMCHANGED:
289  {
290  EnableWindow(GetDlgItem(hwndDlg, IDOK), ListView_GetSelectedCount(context->ListViewHandle) == 1);
291  }
292  break;
293  case NM_DBLCLK:
294  {
295  SendMessage(hwndDlg, WM_COMMAND, IDOK, 0);
296  }
297  break;
298  }
299  }
300  break;
301  case WM_SIZE:
302  {
303  PhLayoutManagerLayout(&context->LayoutManager);
304  }
305  break;
306  case WM_SIZING:
307  {
308  PhResizingMinimumSize((PRECT)lParam, wParam, context->MinimumSize.right, context->MinimumSize.bottom);
309  }
310  break;
311  }
312 
313  return FALSE;
314 }