Repository navigation
Expand file tree
/
Copy pathNative.cs
More file actions
130 lines (111 loc) · 7.16 KB
/
Copy pathNative.cs
File metadata and controls
130 lines (111 loc) · 7.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
using System.Runtime.InteropServices;
using System.Text;
namespace Argus;
/// <summary>Win32 declarations used by Argus.</summary>
internal static class Native
{
public const int GWL_EXSTYLE = -20;
public const long WS_EX_TOPMOST = 0x00000008;
public const long WS_EX_TRANSPARENT = 0x00000020;
public const long WS_EX_TOOLWINDOW = 0x00000080;
public const long WS_EX_LAYERED = 0x00080000;
public const long WS_EX_NOACTIVATE = 0x08000000;
public const uint LWA_ALPHA = 0x2;
public const uint WM_CLOSE = 0x0010;
public const uint WM_HOTKEY = 0x0312;
public const int SW_HIDE = 0;
public const int DWMWA_CLOAKED = 14;
public const uint GA_ROOT = 2;
public const uint GW_OWNER = 4;
public const uint SWP_NOSIZE = 0x1, SWP_NOMOVE = 0x2, SWP_NOZORDER = 0x4, SWP_NOACTIVATE = 0x10, SWP_FRAMECHANGED = 0x20;
public static readonly IntPtr HWND_TOPMOST = new(-1);
public const int WH_KEYBOARD_LL = 13, WH_MOUSE_LL = 14;
public const int WM_KEYDOWN = 0x100, WM_SYSKEYDOWN = 0x104;
public const int WM_MOUSEMOVE = 0x200, WM_LBUTTONDOWN = 0x201, WM_LBUTTONUP = 0x202, WM_RBUTTONDOWN = 0x204, WM_RBUTTONUP = 0x205;
public const int VK_ESCAPE = 0x1B;
public const uint PROCESS_QUERY_LIMITED_INFORMATION = 0x1000;
public delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);
public delegate IntPtr HookProc(int nCode, IntPtr wParam, IntPtr lParam);
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int Left, Top, Right, Bottom;
public readonly int Width => Right - Left;
public readonly int Height => Bottom - Top;
public readonly bool Contains(int x, int y) => x >= Left && x < Right && y >= Top && y < Bottom;
}
[StructLayout(LayoutKind.Sequential)] public struct POINT { public int X, Y; }
[StructLayout(LayoutKind.Sequential)] public struct FILETIME { public uint Low, High; public readonly ulong Value => ((ulong)High << 32) | Low; }
[StructLayout(LayoutKind.Sequential)]
public struct MSLLHOOKSTRUCT { public POINT pt; public uint mouseData, flags, time; public IntPtr dwExtraInfo; }
[StructLayout(LayoutKind.Sequential)]
public struct KBDLLHOOKSTRUCT { public uint vkCode, scanCode, flags, time; public IntPtr dwExtraInfo; }
[StructLayout(LayoutKind.Sequential)]
public struct PROCESS_BASIC_INFORMATION
{
public IntPtr ExitStatus, PebBaseAddress, AffinityMask, BasePriority, UniqueProcessId, InheritedFromUniqueProcessId;
}
[DllImport("user32.dll")] public static extern bool EnumWindows(EnumWindowsProc cb, IntPtr lParam);
[DllImport("user32.dll")] public static extern bool IsWindowVisible(IntPtr h);
[DllImport("user32.dll")] public static extern bool IsIconic(IntPtr h);
[DllImport("user32.dll")] public static extern bool IsHungAppWindow(IntPtr h);
[DllImport("user32.dll", EntryPoint = "GetWindowLongPtrW")] public static extern IntPtr GetWindowLongPtr(IntPtr h, int idx);
[DllImport("user32.dll", EntryPoint = "SetWindowLongPtrW")] public static extern IntPtr SetWindowLongPtr(IntPtr h, int idx, IntPtr value);
[DllImport("user32.dll", CharSet = CharSet.Unicode)] public static extern int GetWindowText(IntPtr h, StringBuilder sb, int max);
[DllImport("user32.dll", CharSet = CharSet.Unicode)] public static extern int GetClassName(IntPtr h, StringBuilder sb, int max);
[DllImport("user32.dll")] public static extern uint GetWindowThreadProcessId(IntPtr h, out uint pid);
[DllImport("user32.dll")] public static extern bool GetWindowRect(IntPtr h, out RECT r);
[DllImport("user32.dll")] public static extern bool GetLayeredWindowAttributes(IntPtr h, out uint key, out byte alpha, out uint flags);
[DllImport("user32.dll")] public static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")] public static extern IntPtr GetWindow(IntPtr h, uint cmd);
[DllImport("user32.dll")] public static extern IntPtr GetAncestor(IntPtr h, uint flags);
[DllImport("user32.dll")] public static extern bool PostMessage(IntPtr h, uint msg, IntPtr w, IntPtr l);
[DllImport("user32.dll")] public static extern bool ShowWindowAsync(IntPtr h, int cmd);
[DllImport("user32.dll")] public static extern bool SetWindowPos(IntPtr h, IntPtr after, int x, int y, int cx, int cy, uint flags);
[DllImport("user32.dll")] public static extern bool GetCursorPos(out POINT p);
[DllImport("user32.dll")] public static extern bool RegisterHotKey(IntPtr h, int id, uint mods, uint vk);
[DllImport("user32.dll")] public static extern bool UnregisterHotKey(IntPtr h, int id);
[DllImport("user32.dll")] public static extern IntPtr SetWindowsHookEx(int id, HookProc cb, IntPtr mod, uint thread);
[DllImport("user32.dll")] public static extern bool UnhookWindowsHookEx(IntPtr hook);
[DllImport("user32.dll")] public static extern IntPtr CallNextHookEx(IntPtr hook, int code, IntPtr w, IntPtr l);
[DllImport("kernel32.dll", CharSet = CharSet.Unicode)] public static extern IntPtr GetModuleHandle(string? name);
[DllImport("kernel32.dll")] public static extern IntPtr OpenProcess(uint access, bool inherit, uint pid);
[DllImport("kernel32.dll")] public static extern bool CloseHandle(IntPtr h);
[DllImport("kernel32.dll", CharSet = CharSet.Unicode)] public static extern bool QueryFullProcessImageName(IntPtr h, uint flags, StringBuilder sb, ref uint size);
[DllImport("kernel32.dll")] public static extern bool GetProcessTimes(IntPtr h, out FILETIME c, out FILETIME e, out FILETIME k, out FILETIME u);
[DllImport("dwmapi.dll")] public static extern int DwmGetWindowAttribute(IntPtr h, int attr, out int val, int size);
[DllImport("ntdll.dll")] public static extern int NtQueryInformationProcess(IntPtr h, int cls, ref PROCESS_BASIC_INFORMATION info, int len, out int ret);
// ---- helpers -------------------------------------------------------------------------
public static string? GetProcessPath(int pid)
{
IntPtr h = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, false, (uint)pid);
if (h == IntPtr.Zero) return null;
try
{
var sb = new StringBuilder(1024);
uint size = (uint)sb.Capacity;
return QueryFullProcessImageName(h, 0, sb, ref size) ? sb.ToString() : null;
}
finally { CloseHandle(h); }
}
/// <summary>Total (kernel+user) CPU time in 100ns ticks, or null if the process can't be opened.</summary>
public static ulong? GetCpuTicks(int pid)
{
IntPtr h = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, false, (uint)pid);
if (h == IntPtr.Zero) return null;
try { return GetProcessTimes(h, out _, out _, out var k, out var u) ? k.Value + u.Value : null; }
finally { CloseHandle(h); }
}
public static int ParentPid(int pid)
{
IntPtr h = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, false, (uint)pid);
if (h == IntPtr.Zero) return 0;
try
{
var pbi = new PROCESS_BASIC_INFORMATION();
return NtQueryInformationProcess(h, 0, ref pbi, Marshal.SizeOf<PROCESS_BASIC_INFORMATION>(), out _) == 0
? (int)pbi.InheritedFromUniqueProcessId : 0;
}
finally { CloseHandle(h); }
}
}