Process Hacker
modsrv.c
Go to the documentation of this file.
1 /*
2  * Process Hacker Extended Tools -
3  * services referencing module
4  *
5  * Copyright (C) 2010-2011 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 "exttools.h"
24 #include "resource.h"
25 #include <winmisc.h>
26 
27 typedef struct _MODULE_SERVICES_CONTEXT
28 {
29  HANDLE ProcessId;
30  PWSTR ModuleName;
32 
33 INT_PTR CALLBACK EtpModuleServicesDlgProc(
34  _In_ HWND hwndDlg,
35  _In_ UINT uMsg,
36  _In_ WPARAM wParam,
37  _In_ LPARAM lParam
38  );
39 
41  _In_ HWND ParentWindowHandle,
42  _In_ HANDLE ProcessId,
43  _In_ PWSTR ModuleName
44  )
45 {
47 
48  context.ProcessId = ProcessId;
49  context.ModuleName = ModuleName;
50 
51  DialogBoxParam(
53  MAKEINTRESOURCE(IDD_MODSERVICES),
54  ParentWindowHandle,
56  (LPARAM)&context
57  );
58 }
59 
60 INT_PTR CALLBACK EtpModuleServicesDlgProc(
61  _In_ HWND hwndDlg,
62  _In_ UINT uMsg,
63  _In_ WPARAM wParam,
64  _In_ LPARAM lParam
65  )
66 {
67  switch (uMsg)
68  {
69  case WM_INITDIALOG:
70  {
72  ULONG win32Result;
74  TAG_INFO_NAMES_REFERENCING_MODULE namesReferencingModule;
75  PPH_LIST serviceList;
76  PPH_SERVICE_ITEM *serviceItems;
77  HWND serviceListHandle;
78  RECT rect;
79  PPH_PROCESS_ITEM processItem;
80  PPH_STRING message;
81 
82  PhCenterWindow(hwndDlg, GetParent(hwndDlg));
83 
84  I_QueryTagInformation = PhGetModuleProcAddress(L"advapi32.dll", "I_QueryTagInformation");
85 
86  if (!I_QueryTagInformation)
87  {
88  PhShowError(hwndDlg, L"Unable to query services because the feature is not supported by the operating system.");
89  EndDialog(hwndDlg, IDCANCEL);
90  return FALSE;
91  }
92 
93  memset(&namesReferencingModule, 0, sizeof(TAG_INFO_NAMES_REFERENCING_MODULE));
94  namesReferencingModule.InParams.dwPid = HandleToUlong(context->ProcessId);
95  namesReferencingModule.InParams.pszModule = context->ModuleName;
96 
97  win32Result = I_QueryTagInformation(NULL, eTagInfoLevelNamesReferencingModule, &namesReferencingModule);
98 
99  if (win32Result == ERROR_NO_MORE_ITEMS)
100  win32Result = 0;
101 
102  if (win32Result != 0)
103  {
104  PhShowStatus(hwndDlg, L"Unable to query services", 0, win32Result);
105  EndDialog(hwndDlg, IDCANCEL);
106  return FALSE;
107  }
108 
109  serviceList = PhCreateList(16);
110 
111  if (namesReferencingModule.OutParams.pmszNames)
112  {
113  PPH_SERVICE_ITEM serviceItem;
114  PWSTR serviceName;
115  ULONG nameLength;
116 
117  serviceName = namesReferencingModule.OutParams.pmszNames;
118 
119  while (TRUE)
120  {
121  nameLength = (ULONG)PhCountStringZ(serviceName);
122 
123  if (nameLength == 0)
124  break;
125 
126  if (serviceItem = PhReferenceServiceItem(serviceName))
127  PhAddItemList(serviceList, serviceItem);
128 
129  serviceName += nameLength + 1;
130  }
131 
132  LocalFree(namesReferencingModule.OutParams.pmszNames);
133  }
134 
135  serviceItems = PhAllocateCopy(serviceList->Items, serviceList->Count * sizeof(PPH_SERVICE_ITEM));
136  PhDereferenceObject(serviceList);
137  serviceListHandle = PhCreateServiceListControl(hwndDlg, serviceItems, serviceList->Count);
138 
139  // Position the control.
140  GetWindowRect(GetDlgItem(hwndDlg, IDC_SERVICES_LAYOUT), &rect);
141  MapWindowPoints(NULL, hwndDlg, (POINT *)&rect, 2);
142  MoveWindow(serviceListHandle, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, FALSE);
143 
144  ShowWindow(serviceListHandle, SW_SHOW);
145 
146  if (processItem = PhReferenceProcessItem(context->ProcessId))
147  {
148  message = PhFormatString(L"Services referencing %s in %s:", context->ModuleName, processItem->ProcessName->Buffer);
149  PhDereferenceObject(processItem);
150  }
151  else
152  {
153  message = PhFormatString(L"Services referencing %s:", context->ModuleName);
154  }
155 
156  SetDlgItemText(hwndDlg, IDC_MESSAGE, message->Buffer);
157  PhDereferenceObject(message);
158  }
159  break;
160  case WM_COMMAND:
161  {
162  switch (LOWORD(wParam))
163  {
164  case IDCANCEL:
165  case IDOK:
166  EndDialog(hwndDlg, IDOK);
167  break;
168  }
169  }
170  break;
171  }
172 
173  return FALSE;
174 }