Process Hacker
main.c
Go to the documentation of this file.
1 #include <phdk.h>
2 
3 #define ID_SAMPLE_MENU_ITEM 1
4 #define ID_SHOW_ME_SOME_OBJECTS 2
5 
7  __in_opt PVOID Parameter,
8  __in_opt PVOID Context
9  );
10 
12  __in_opt PVOID Parameter,
13  __in_opt PVOID Context
14  );
15 
17  __in_opt PVOID Parameter,
18  __in_opt PVOID Context
19  );
20 
22  __in_opt PVOID Parameter,
23  __in_opt PVOID Context
24  );
25 
27  __in_opt PVOID Parameter,
28  __in_opt PVOID Context
29  );
30 
32  __in_opt PVOID Parameter,
33  __in_opt PVOID Context
34  );
35 
43 
45  __in HINSTANCE Instance,
46  __in ULONG Reason,
47  __reserved PVOID Reserved
48  )
49 {
50  switch (Reason)
51  {
52  case DLL_PROCESS_ATTACH:
53  {
55 
56  // Register your plugin with a unique name, otherwise it will fail.
57  PluginInstance = PhRegisterPlugin(L"ProcessHacker.SamplePlugin", Instance, &info);
58 
59  if (!PluginInstance)
60  return FALSE;
61 
62  info->DisplayName = L"Sample Plugin";
63  info->Author = L"Someone";
64  info->Description = L"Description goes here";
65  info->HasOptions = TRUE;
66 
68  PhGetPluginCallback(PluginInstance, PluginCallbackLoad),
70  NULL,
71  &PluginLoadCallbackRegistration
72  );
76  NULL,
77  &PluginShowOptionsCallbackRegistration
78  );
82  NULL,
83  &PluginMenuItemCallbackRegistration
84  );
85 
89  NULL,
90  &MainWindowShowingCallbackRegistration
91  );
95  NULL,
96  &GetProcessHighlightingColorCallbackRegistration
97  );
101  NULL,
102  &GetProcessTooltipTextCallbackRegistration
103  );
104 
105  // Add some settings. Note that we cannot access these settings
106  // in DllMain. Settings must be added in DllMain.
107  {
108  static PH_SETTING_CREATE settings[] =
109  {
110  // You must prepend your plugin name to the setting names.
111  { IntegerSettingType, L"ProcessHacker.SamplePlugin.SomeInteger", L"1234" },
112  { StringSettingType, L"ProcessHacker.SamplePlugin.SomeString", L"my string" }
113  };
114 
115  PhAddSettings(settings, sizeof(settings) / sizeof(PH_SETTING_CREATE));
116  }
117  }
118  break;
119  }
120 
121  return TRUE;
122 }
123 
125  __in_opt PVOID Parameter,
126  __in_opt PVOID Context
127  )
128 {
129  ULONG myInteger;
130  PPH_STRING myString;
131 
132  myInteger = PhGetIntegerSetting(L"ProcessHacker.SamplePlugin.SomeInteger");
133  // Do stuff to the integer. Possibly modify the setting.
134  PhSetIntegerSetting(L"ProcessHacker.SamplePlugin.SomeInteger", myInteger + 100);
135 
136  myString = PhGetStringSetting(L"ProcessHacker.SamplePlugin.SomeString");
137  // Do stuff to the string.
138  // Dereference the string when you're done, or memory will be leaked.
139  PhDereferenceObject(myString);
140 }
141 
143  __in_opt PVOID Parameter,
144  __in_opt PVOID Context
145  )
146 {
147  PhShowError((HWND)Parameter, L"Show some options here.");
148 }
149 
151  __in PPH_STRINGREF Name,
152  __in PPH_STRINGREF TypeName,
153  __in_opt PVOID Context
154  )
155 {
156  INT result;
157  PPH_STRING name;
158  PPH_STRING typeName;
159 
160  name = PhCreateString2(Name);
161  typeName = PhCreateString2(TypeName);
162  result = PhShowMessage(
164  MB_ICONINFORMATION | MB_OKCANCEL,
165  L"%s: %s",
166  name->Buffer,
167  typeName->Buffer
168  );
169  PhDereferenceObject(name);
170  PhDereferenceObject(typeName);
171 
172  return result == IDOK;
173 }
174 
176  __in_opt PVOID Parameter,
177  __in_opt PVOID Context
178  )
179 {
180  PPH_PLUGIN_MENU_ITEM menuItem = Parameter;
181 
182  switch (menuItem->Id)
183  {
184  case ID_SAMPLE_MENU_ITEM:
185  {
186  PhShowInformation(PhMainWndHandle, L"You clicked the sample menu item!");
187  }
188  break;
190  {
191  NTSTATUS status;
192  HANDLE directoryHandle;
194  UNICODE_STRING name;
195 
196  // Use the Native API seamlessly alongside Win32.
197  RtlInitUnicodeString(&name, L"\\");
198  InitializeObjectAttributes(&oa, &name, 0, NULL, NULL);
199 
200  if (NT_SUCCESS(status = NtOpenDirectoryObject(&directoryHandle, DIRECTORY_QUERY, &oa)))
201  {
203  NtClose(directoryHandle);
204  }
205  }
206  break;
207  }
208 }
209 
211  __in_opt PVOID Parameter,
212  __in_opt PVOID Context
213  )
214 {
215  // $ won't match anything, so the menu item will get added to the end.
216  PhPluginAddMenuItem(PluginInstance, PH_MENU_ITEM_LOCATION_TOOLS, L"$",
217  ID_SAMPLE_MENU_ITEM, L"Sample menu item", NULL);
218  PhPluginAddMenuItem(PluginInstance, PH_MENU_ITEM_LOCATION_TOOLS, L"$",
219  ID_SHOW_ME_SOME_OBJECTS, L"Show me some objects", NULL);
220 }
221 
223  __in_opt PVOID Parameter,
224  __in_opt PVOID Context
225  )
226 {
227  PPH_PLUGIN_GET_HIGHLIGHTING_COLOR getHighlightingColor = Parameter;
228  PPH_PROCESS_ITEM processItem;
229 
230  processItem = getHighlightingColor->Parameter;
231 
232  // Optional: if another plugin handled the highlighting, don't override it.
233  if (getHighlightingColor->Handled)
234  return;
235 
236  // Set the background color of svchost.exe processes to black.
237  if (PhEqualString2(processItem->ProcessName, L"svchost.exe", TRUE))
238  {
239  getHighlightingColor->BackColor = RGB(0x00, 0x00, 0x00);
240  getHighlightingColor->Cache = TRUE;
241  getHighlightingColor->Handled = TRUE;
242  }
243 }
244 
246  __in_opt PVOID Parameter,
247  __in_opt PVOID Context
248  )
249 {
250  PPH_PLUGIN_GET_TOOLTIP_TEXT getTooltipText = Parameter;
251  PPH_PROCESS_ITEM processItem;
252 
253  processItem = getTooltipText->Parameter;
254 
255  // Put some text into the tooltip. This will go in just before the Notes section.
257  getTooltipText->StringBuilder,
258  L"Sample plugin:\n The process name is: %s\n",
259  processItem->ProcessName->Buffer
260  );
261 }