Process Hacker
utils.c
Go to the documentation of this file.
1 /*
2  * Process Hacker Window Explorer -
3  * utility functions
4  *
5  * Copyright (C) 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 "wndexp.h"
24 
25 // WARNING: No functions from ProcessHacker.exe should be used in this file!
26 
28  _In_ PSTR Name
29  )
30 {
31  static PVOID imageBase = NULL;
32 
33  if (!imageBase)
34  imageBase = GetModuleHandle(L"ProcessHacker.exe");
35 
36  return (PVOID)GetProcAddress(imageBase, Name);
37 }
38 
40  _In_ PWSTR OriginalName,
41  _Inout_updates_(256) PWCHAR Buffer,
42  _Out_ PUNICODE_STRING ObjectName
43  )
44 {
45  SIZE_T length;
46  SIZE_T originalNameLength;
47 
48  // Sessions other than session 0 require SeCreateGlobalPrivilege.
49  if (NtCurrentPeb()->SessionId != 0)
50  {
51  memcpy(Buffer, L"\\Sessions\\", 10 * sizeof(WCHAR));
52  _ultow(NtCurrentPeb()->SessionId, Buffer + 10, 10);
53  length = wcslen(Buffer);
54  originalNameLength = wcslen(OriginalName);
55  memcpy(Buffer + length, OriginalName, (originalNameLength + 1) * sizeof(WCHAR));
56  length += originalNameLength;
57 
58  ObjectName->Buffer = Buffer;
59  ObjectName->MaximumLength = (ObjectName->Length = (USHORT)(length * sizeof(WCHAR))) + sizeof(WCHAR);
60  }
61  else
62  {
63  RtlInitUnicodeString(ObjectName, OriginalName);
64  }
65 }
66 
68  _In_ HWND hWnd
69  )
70 {
71  RECT rect;
72  HDC hdc;
73 
74  GetWindowRect(hWnd, &rect);
75  hdc = GetWindowDC(hWnd);
76 
77  if (hdc)
78  {
79  ULONG penWidth = GetSystemMetrics(SM_CXBORDER) * 3;
80  INT oldDc;
81  HPEN pen;
82  HBRUSH brush;
83 
84  oldDc = SaveDC(hdc);
85 
86  // Get an inversion effect.
87  SetROP2(hdc, R2_NOT);
88 
89  pen = CreatePen(PS_INSIDEFRAME, penWidth, RGB(0x00, 0x00, 0x00));
90  SelectObject(hdc, pen);
91 
92  brush = GetStockObject(NULL_BRUSH);
93  SelectObject(hdc, brush);
94 
95  // Draw the rectangle.
96  Rectangle(hdc, 0, 0, rect.right - rect.left, rect.bottom - rect.top);
97 
98  // Cleanup.
99  DeleteObject(pen);
100 
101  RestoreDC(hdc, oldDc);
102  ReleaseDC(hWnd, hdc);
103  }
104 }